1- # ruff: noqa: D100, D101, D102, D103, D104, D105, D107
1+ """Redux autorun module."""
2+
23from __future__ import annotations
34
45import asyncio
3637
3738
3839class AwaitableWrapper (Generic [T ]):
40+ """A wrapper for a coroutine to track if it has been awaited."""
41+
3942 def __init__ (self , coro : Coroutine [None , None , T ]) -> None :
43+ """Initialize the AwaitableWrapper with a coroutine."""
4044 self .coro = coro
4145 self .awaited = False
4246
4347 def __await__ (self ) -> Generator [None , None , T ]:
48+ """Await the coroutine and set the awaited flag to True."""
4449 self .awaited = True
4550 return self .coro .__await__ ()
4651
4752 def close (self ) -> None :
53+ """Close the coroutine if it has not been awaited."""
4854 self .coro .close ()
4955
5056 def __repr__ (self ) -> str :
57+ """Return a string representation of the AwaitableWrapper."""
5158 return f'AwaitableWrapper({ self .coro } , awaited={ self .awaited } )'
5259
5360
@@ -62,6 +69,8 @@ class Autorun(
6269 Args ,
6370 ],
6471):
72+ """Run a wrapped function in response to specific state changes in the store."""
73+
6574 def __init__ (
6675 self : Autorun ,
6776 * ,
@@ -74,6 +83,7 @@ def __init__(
7483 ],
7584 options : AutorunOptions [ReturnType ],
7685 ) -> None :
86+ """Initialize the Autorun instance."""
7787 self .__name__ = func .__name__
7888 self ._store = store
7989 self ._selector = selector
@@ -124,7 +134,19 @@ def __init__(
124134 else :
125135 self ._unsubscribe = None
126136
127- def unsubscribe (self : Autorun , _ : weakref .ref | None = None ) -> None :
137+ def unsubscribe (
138+ self : Autorun [
139+ State ,
140+ Action ,
141+ Event ,
142+ SelectorOutput ,
143+ ComparatorOutput ,
144+ ReturnType ,
145+ Args ,
146+ ],
147+ _ : weakref .ref | None = None ,
148+ ) -> None :
149+ """Unsubscribe the autorun from the store and clean up resources."""
128150 if self ._unsubscribe :
129151 self ._unsubscribe ()
130152 self ._unsubscribe = None
@@ -140,6 +162,7 @@ def inform_subscribers(
140162 Args ,
141163 ],
142164 ) -> None :
165+ """Inform all subscribers about the latest value."""
143166 for subscriber_ in self ._subscriptions .copy ():
144167 if isinstance (subscriber_ , weakref .ref ):
145168 subscriber = subscriber_ ()
@@ -264,6 +287,7 @@ def __call__(
264287 * args : Args .args ,
265288 ** kwargs : Args .kwargs ,
266289 ) -> ReturnType :
290+ """Call the wrapped function with the current state of the store."""
267291 state = self ._store ._state # noqa: SLF001
268292 self ._check (state )
269293 if self ._should_be_called or args or kwargs or not self ._options .memoization :
@@ -281,6 +305,7 @@ def __repr__(
281305 Args ,
282306 ],
283307 ) -> str :
308+ """Return a string representation of the Autorun instance."""
284309 return (
285310 super ().__repr__ ()
286311 + f'(func: { self ._func } , last_value: { self ._latest_value } )'
@@ -298,6 +323,7 @@ def value(
298323 Args ,
299324 ],
300325 ) -> ReturnType :
326+ """Get the latest value of the autorun function."""
301327 return cast ('ReturnType' , self ._latest_value )
302328
303329 def subscribe (
@@ -315,6 +341,7 @@ def subscribe(
315341 initial_run : bool | None = None ,
316342 keep_ref : bool | None = None ,
317343 ) -> Callable [[], None ]:
344+ """Subscribe to the autorun to be notified of changes in the state."""
318345 if initial_run is None :
319346 initial_run = self ._options .subscribers_initial_run
320347 if keep_ref is None :
@@ -347,4 +374,5 @@ def __signature__(
347374 Args ,
348375 ],
349376 ) -> inspect .Signature :
377+ """Get the signature of the wrapped function."""
350378 return self ._signature
0 commit comments