77import queue
88import weakref
99from collections import defaultdict
10- from collections .abc import Callable
1110from threading import Lock , Thread
1211from typing import (
1312 TYPE_CHECKING ,
3534 CreateStoreOptions ,
3635 DispatchParameters ,
3736 Event ,
38- Event2 ,
3937 EventHandler ,
4038 EventMiddleware ,
4139 FinishAction ,
4644 SelectorOutput ,
4745 SnapshotAtom ,
4846 State ,
47+ StrictEvent ,
48+ SubscribeEventCleanup ,
4949 UnknownAutorunDecorator ,
5050 UnknownViewDecorator ,
5151 ViewDecorator ,
6161from redux .side_effect_runner import SideEffectRunnerThread
6262
6363if TYPE_CHECKING :
64- from collections .abc import Awaitable
64+ from collections .abc import Awaitable , Callable
6565
6666
6767class Store (Generic [State , Action , Event ], SerializationMixin ):
@@ -112,11 +112,11 @@ def __init__(
112112 if self .store_options .auto_init :
113113 if self .store_options .scheduler :
114114 self .store_options .scheduler (
115- lambda : self .dispatch (cast (Action , InitAction ())),
115+ lambda : self .dispatch (cast (' Action' , InitAction ())),
116116 interval = False ,
117117 )
118118 else :
119- self .dispatch (cast (Action , InitAction ()))
119+ self .dispatch (cast (' Action' , InitAction ()))
120120
121121 if self .store_options .scheduler :
122122 self .store_options .scheduler (self .run , interval = True )
@@ -148,7 +148,7 @@ def _run_actions(self: Store[State, Action, Event]) -> None:
148148 self ._call_listeners (self ._state )
149149
150150 if isinstance (action , FinishAction ):
151- self ._dispatch ([cast (Event , FinishEvent ())])
151+ self ._dispatch ([cast (' Event' , FinishEvent ())])
152152
153153 def _run_event_handlers (self : Store [State , Action , Event ]) -> None :
154154 while len (self ._events ) > 0 :
@@ -216,7 +216,7 @@ def _dispatch(
216216 ) -> None :
217217 for item in items :
218218 if isinstance (item , BaseAction ):
219- action = cast (Action , item )
219+ action = cast (' Action' , item )
220220 for action_middleware in self ._action_middlewares :
221221 action_ = action_middleware (action )
222222 if action_ is None :
@@ -225,7 +225,7 @@ def _dispatch(
225225 else :
226226 self ._actions .append (action )
227227 if isinstance (item , BaseEvent ):
228- event = cast (Event , item )
228+ event = cast (' Event' , item )
229229 for event_middleware in self ._event_middlewares :
230230 event_ = event_middleware (event )
231231 if event_ is None :
@@ -237,7 +237,7 @@ def _dispatch(
237237 if self .store_options .scheduler is None and not self ._is_running .locked ():
238238 self .run ()
239239
240- def subscribe (
240+ def _subscribe (
241241 self : Store [State , Action , Event ],
242242 listener : Callable [[State ], Any ],
243243 * ,
@@ -256,11 +256,11 @@ def subscribe(
256256
257257 def subscribe_event (
258258 self : Store [State , Action , Event ],
259- event_type : type [Event2 ],
260- handler : EventHandler [Event2 ],
259+ event_type : type [StrictEvent ],
260+ handler : EventHandler [StrictEvent ],
261261 * ,
262262 keep_ref : bool = True ,
263- ) -> Callable [[], None ] :
263+ ) -> SubscribeEventCleanup :
264264 """Subscribe to events."""
265265 if keep_ref :
266266 handler_ref = handler
@@ -269,12 +269,12 @@ def subscribe_event(
269269 else :
270270 handler_ref = weakref .ref (handler )
271271
272- self ._event_handlers [cast (Any , event_type )].add (handler_ref )
272+ self ._event_handlers [cast (' Any' , event_type )].add (handler_ref )
273273
274274 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 )
276276
277- return unsubscribe
277+ return SubscribeEventCleanup ( unsubscribe = unsubscribe , handler = handler )
278278
279279 def _wait_for_store_to_finish (self : Store [State , Action , Event ]) -> None :
280280 """Wait for the store to finish."""
@@ -325,20 +325,20 @@ def autorun(
325325 """Create a new autorun, reflecting on state changes."""
326326
327327 @overload
328- def decorator (
328+ def autorun_decorator (
329329 func : Callable [
330330 Concatenate [SelectorOutput , Args ],
331331 ReturnType ,
332332 ],
333333 ) -> AutorunReturnType [ReturnType , Args ]: ...
334334 @overload
335- def decorator (
335+ def autorun_decorator (
336336 func : Callable [
337337 Concatenate [SelectorOutput , Args ],
338338 Awaitable [ReturnType ],
339339 ],
340340 ) -> AutorunReturnType [Awaitable [ReturnType ], Args ]: ...
341- def decorator (
341+ def autorun_decorator (
342342 func : Callable [
343343 Concatenate [SelectorOutput , Args ],
344344 AwaitableOrNot [ReturnType ],
@@ -348,11 +348,11 @@ def decorator(
348348 store = self ,
349349 selector = selector ,
350350 comparator = comparator ,
351- func = cast (Callable , func ),
351+ func = cast (' Callable' , func ),
352352 options = options or AutorunOptions (),
353353 )
354354
355- return decorator
355+ return autorun_decorator
356356
357357 @overload
358358 def view (
@@ -379,21 +379,21 @@ def view(
379379 """Create a new view, throttling calls for unchanged selector results."""
380380
381381 @overload
382- def decorator (
382+ def view_decorator (
383383 func : Callable [
384384 Concatenate [SelectorOutput , Args ],
385385 ReturnType ,
386386 ],
387387 ) -> ViewReturnType [ReturnType , Args ]: ...
388388 @overload
389- def decorator (
389+ def view_decorator (
390390 func : Callable [
391391 Concatenate [SelectorOutput , Args ],
392392 Awaitable [ReturnType ],
393393 ],
394394 ) -> ViewReturnType [Awaitable [ReturnType ], Args ]: ...
395395
396- def decorator (
396+ def view_decorator (
397397 func : Callable [
398398 Concatenate [SelectorOutput , Args ],
399399 AwaitableOrNot [ReturnType ],
@@ -404,7 +404,7 @@ def decorator(
404404 store = self ,
405405 selector = selector ,
406406 comparator = None ,
407- func = cast (Callable , func ),
407+ func = cast (' Callable' , func ),
408408 options = AutorunOptions (
409409 default_value = _options .default_value ,
410410 auto_await = True ,
@@ -417,7 +417,7 @@ def decorator(
417417 ),
418418 )
419419
420- return decorator
420+ return view_decorator
421421
422422 def with_state (
423423 self : Store [State , Action , Event ],
@@ -433,7 +433,7 @@ def with_state(
433433 `store._state` is also possible.
434434 """
435435
436- def decorator (
436+ def with_state_decorator (
437437 func : Callable [
438438 Concatenate [SelectorOutput , Args ],
439439 ReturnType ,
@@ -445,9 +445,11 @@ def wrapper(*args: Args.args, **kwargs: Args.kwargs) -> ReturnType:
445445 raise RuntimeError (msg )
446446 return func (selector (self ._state ), * args , ** kwargs )
447447
448+ wrapper .__name__ = f'with_state:{ func .__name__ } '
449+
448450 return wrapper
449451
450- return decorator
452+ return with_state_decorator
451453
452454 @property
453455 def snapshot (self : Store [State , Action , Event ]) -> SnapshotAtom :
0 commit comments