7
7
import queue
8
8
import weakref
9
9
from collections import defaultdict
10
- from collections .abc import Callable
11
10
from threading import Lock , Thread
12
11
from typing import (
13
12
TYPE_CHECKING ,
35
34
CreateStoreOptions ,
36
35
DispatchParameters ,
37
36
Event ,
38
- Event2 ,
39
37
EventHandler ,
40
38
EventMiddleware ,
41
39
FinishAction ,
46
44
SelectorOutput ,
47
45
SnapshotAtom ,
48
46
State ,
47
+ StrictEvent ,
48
+ SubscribeEventCleanup ,
49
49
UnknownAutorunDecorator ,
50
50
UnknownViewDecorator ,
51
51
ViewDecorator ,
61
61
from redux .side_effect_runner import SideEffectRunnerThread
62
62
63
63
if TYPE_CHECKING :
64
- from collections .abc import Awaitable
64
+ from collections .abc import Awaitable , Callable
65
65
66
66
67
67
class Store (Generic [State , Action , Event ], SerializationMixin ):
@@ -112,11 +112,11 @@ def __init__(
112
112
if self .store_options .auto_init :
113
113
if self .store_options .scheduler :
114
114
self .store_options .scheduler (
115
- lambda : self .dispatch (cast (Action , InitAction ())),
115
+ lambda : self .dispatch (cast (' Action' , InitAction ())),
116
116
interval = False ,
117
117
)
118
118
else :
119
- self .dispatch (cast (Action , InitAction ()))
119
+ self .dispatch (cast (' Action' , InitAction ()))
120
120
121
121
if self .store_options .scheduler :
122
122
self .store_options .scheduler (self .run , interval = True )
@@ -148,7 +148,7 @@ def _run_actions(self: Store[State, Action, Event]) -> None:
148
148
self ._call_listeners (self ._state )
149
149
150
150
if isinstance (action , FinishAction ):
151
- self ._dispatch ([cast (Event , FinishEvent ())])
151
+ self ._dispatch ([cast (' Event' , FinishEvent ())])
152
152
153
153
def _run_event_handlers (self : Store [State , Action , Event ]) -> None :
154
154
while len (self ._events ) > 0 :
@@ -216,7 +216,7 @@ def _dispatch(
216
216
) -> None :
217
217
for item in items :
218
218
if isinstance (item , BaseAction ):
219
- action = cast (Action , item )
219
+ action = cast (' Action' , item )
220
220
for action_middleware in self ._action_middlewares :
221
221
action_ = action_middleware (action )
222
222
if action_ is None :
@@ -225,7 +225,7 @@ def _dispatch(
225
225
else :
226
226
self ._actions .append (action )
227
227
if isinstance (item , BaseEvent ):
228
- event = cast (Event , item )
228
+ event = cast (' Event' , item )
229
229
for event_middleware in self ._event_middlewares :
230
230
event_ = event_middleware (event )
231
231
if event_ is None :
@@ -237,7 +237,7 @@ def _dispatch(
237
237
if self .store_options .scheduler is None and not self ._is_running .locked ():
238
238
self .run ()
239
239
240
- def subscribe (
240
+ def _subscribe (
241
241
self : Store [State , Action , Event ],
242
242
listener : Callable [[State ], Any ],
243
243
* ,
@@ -256,11 +256,11 @@ def subscribe(
256
256
257
257
def subscribe_event (
258
258
self : Store [State , Action , Event ],
259
- event_type : type [Event2 ],
260
- handler : EventHandler [Event2 ],
259
+ event_type : type [StrictEvent ],
260
+ handler : EventHandler [StrictEvent ],
261
261
* ,
262
262
keep_ref : bool = True ,
263
- ) -> Callable [[], None ] :
263
+ ) -> SubscribeEventCleanup :
264
264
"""Subscribe to events."""
265
265
if keep_ref :
266
266
handler_ref = handler
@@ -269,12 +269,12 @@ def subscribe_event(
269
269
else :
270
270
handler_ref = weakref .ref (handler )
271
271
272
- self ._event_handlers [cast (Any , event_type )].add (handler_ref )
272
+ self ._event_handlers [cast (' Any' , event_type )].add (handler_ref )
273
273
274
274
def unsubscribe () -> None :
275
- self ._event_handlers [cast (Any , event_type )].discard (handler_ref )
275
+ self ._event_handlers [cast (' Any' , event_type )].discard (handler_ref )
276
276
277
- return unsubscribe
277
+ return SubscribeEventCleanup ( unsubscribe = unsubscribe , handler = handler )
278
278
279
279
def _wait_for_store_to_finish (self : Store [State , Action , Event ]) -> None :
280
280
"""Wait for the store to finish."""
@@ -325,20 +325,20 @@ def autorun(
325
325
"""Create a new autorun, reflecting on state changes."""
326
326
327
327
@overload
328
- def decorator (
328
+ def autorun_decorator (
329
329
func : Callable [
330
330
Concatenate [SelectorOutput , Args ],
331
331
ReturnType ,
332
332
],
333
333
) -> AutorunReturnType [ReturnType , Args ]: ...
334
334
@overload
335
- def decorator (
335
+ def autorun_decorator (
336
336
func : Callable [
337
337
Concatenate [SelectorOutput , Args ],
338
338
Awaitable [ReturnType ],
339
339
],
340
340
) -> AutorunReturnType [Awaitable [ReturnType ], Args ]: ...
341
- def decorator (
341
+ def autorun_decorator (
342
342
func : Callable [
343
343
Concatenate [SelectorOutput , Args ],
344
344
AwaitableOrNot [ReturnType ],
@@ -348,11 +348,11 @@ def decorator(
348
348
store = self ,
349
349
selector = selector ,
350
350
comparator = comparator ,
351
- func = cast (Callable , func ),
351
+ func = cast (' Callable' , func ),
352
352
options = options or AutorunOptions (),
353
353
)
354
354
355
- return decorator
355
+ return autorun_decorator
356
356
357
357
@overload
358
358
def view (
@@ -379,21 +379,21 @@ def view(
379
379
"""Create a new view, throttling calls for unchanged selector results."""
380
380
381
381
@overload
382
- def decorator (
382
+ def view_decorator (
383
383
func : Callable [
384
384
Concatenate [SelectorOutput , Args ],
385
385
ReturnType ,
386
386
],
387
387
) -> ViewReturnType [ReturnType , Args ]: ...
388
388
@overload
389
- def decorator (
389
+ def view_decorator (
390
390
func : Callable [
391
391
Concatenate [SelectorOutput , Args ],
392
392
Awaitable [ReturnType ],
393
393
],
394
394
) -> ViewReturnType [Awaitable [ReturnType ], Args ]: ...
395
395
396
- def decorator (
396
+ def view_decorator (
397
397
func : Callable [
398
398
Concatenate [SelectorOutput , Args ],
399
399
AwaitableOrNot [ReturnType ],
@@ -404,7 +404,7 @@ def decorator(
404
404
store = self ,
405
405
selector = selector ,
406
406
comparator = None ,
407
- func = cast (Callable , func ),
407
+ func = cast (' Callable' , func ),
408
408
options = AutorunOptions (
409
409
default_value = _options .default_value ,
410
410
auto_await = True ,
@@ -417,7 +417,7 @@ def decorator(
417
417
),
418
418
)
419
419
420
- return decorator
420
+ return view_decorator
421
421
422
422
def with_state (
423
423
self : Store [State , Action , Event ],
@@ -433,7 +433,7 @@ def with_state(
433
433
`store._state` is also possible.
434
434
"""
435
435
436
- def decorator (
436
+ def with_state_decorator (
437
437
func : Callable [
438
438
Concatenate [SelectorOutput , Args ],
439
439
ReturnType ,
@@ -445,9 +445,11 @@ def wrapper(*args: Args.args, **kwargs: Args.kwargs) -> ReturnType:
445
445
raise RuntimeError (msg )
446
446
return func (selector (self ._state ), * args , ** kwargs )
447
447
448
+ wrapper .__name__ = f'with_state:{ func .__name__ } '
449
+
448
450
return wrapper
449
451
450
- return decorator
452
+ return with_state_decorator
451
453
452
454
@property
453
455
def snapshot (self : Store [State , Action , Event ]) -> SnapshotAtom :
0 commit comments