1
- # ruff: noqa: D100, D101, D102, D103, D104, D105, D107
1
+ """Redux autorun module."""
2
+
2
3
from __future__ import annotations
3
4
4
5
import asyncio
36
37
37
38
38
39
class AwaitableWrapper (Generic [T ]):
40
+ """A wrapper for a coroutine to track if it has been awaited."""
41
+
39
42
def __init__ (self , coro : Coroutine [None , None , T ]) -> None :
43
+ """Initialize the AwaitableWrapper with a coroutine."""
40
44
self .coro = coro
41
45
self .awaited = False
42
46
43
47
def __await__ (self ) -> Generator [None , None , T ]:
48
+ """Await the coroutine and set the awaited flag to True."""
44
49
self .awaited = True
45
50
return self .coro .__await__ ()
46
51
47
52
def close (self ) -> None :
53
+ """Close the coroutine if it has not been awaited."""
48
54
self .coro .close ()
49
55
50
56
def __repr__ (self ) -> str :
57
+ """Return a string representation of the AwaitableWrapper."""
51
58
return f'AwaitableWrapper({ self .coro } , awaited={ self .awaited } )'
52
59
53
60
@@ -62,6 +69,8 @@ class Autorun(
62
69
Args ,
63
70
],
64
71
):
72
+ """Run a wrapped function in response to specific state changes in the store."""
73
+
65
74
def __init__ (
66
75
self : Autorun ,
67
76
* ,
@@ -74,6 +83,7 @@ def __init__(
74
83
],
75
84
options : AutorunOptions [ReturnType ],
76
85
) -> None :
86
+ """Initialize the Autorun instance."""
77
87
self .__name__ = func .__name__
78
88
self ._store = store
79
89
self ._selector = selector
@@ -124,7 +134,19 @@ def __init__(
124
134
else :
125
135
self ._unsubscribe = None
126
136
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."""
128
150
if self ._unsubscribe :
129
151
self ._unsubscribe ()
130
152
self ._unsubscribe = None
@@ -140,6 +162,7 @@ def inform_subscribers(
140
162
Args ,
141
163
],
142
164
) -> None :
165
+ """Inform all subscribers about the latest value."""
143
166
for subscriber_ in self ._subscriptions .copy ():
144
167
if isinstance (subscriber_ , weakref .ref ):
145
168
subscriber = subscriber_ ()
@@ -264,6 +287,7 @@ def __call__(
264
287
* args : Args .args ,
265
288
** kwargs : Args .kwargs ,
266
289
) -> ReturnType :
290
+ """Call the wrapped function with the current state of the store."""
267
291
state = self ._store ._state # noqa: SLF001
268
292
self ._check (state )
269
293
if self ._should_be_called or args or kwargs or not self ._options .memoization :
@@ -281,6 +305,7 @@ def __repr__(
281
305
Args ,
282
306
],
283
307
) -> str :
308
+ """Return a string representation of the Autorun instance."""
284
309
return (
285
310
super ().__repr__ ()
286
311
+ f'(func: { self ._func } , last_value: { self ._latest_value } )'
@@ -298,6 +323,7 @@ def value(
298
323
Args ,
299
324
],
300
325
) -> ReturnType :
326
+ """Get the latest value of the autorun function."""
301
327
return cast ('ReturnType' , self ._latest_value )
302
328
303
329
def subscribe (
@@ -315,6 +341,7 @@ def subscribe(
315
341
initial_run : bool | None = None ,
316
342
keep_ref : bool | None = None ,
317
343
) -> Callable [[], None ]:
344
+ """Subscribe to the autorun to be notified of changes in the state."""
318
345
if initial_run is None :
319
346
initial_run = self ._options .subscribers_initial_run
320
347
if keep_ref is None :
@@ -347,4 +374,5 @@ def __signature__(
347
374
Args ,
348
375
],
349
376
) -> inspect .Signature :
377
+ """Get the signature of the wrapped function."""
350
378
return self ._signature
0 commit comments