44import copy
55import uuid
66from dataclasses import asdict , make_dataclass
7- from typing import TYPE_CHECKING , Any , TypeGuard , TypeVar , cast
7+ from typing import TYPE_CHECKING , Any , TypeVar , cast
88
99from .basic_types import (
1010 Action ,
@@ -25,37 +25,37 @@ class BaseCombineReducerState(Immutable):
2525 _id : str
2626
2727
28- class BaseCombineReducerAction (BaseAction ):
28+ class CombineReducerAction (BaseAction ):
2929 _id : str
3030
3131
32- class CombineReducerRegisterAction (BaseCombineReducerAction ):
32+ class CombineReducerInitAction (CombineReducerAction , InitAction ):
33+ key : str
34+
35+
36+ class CombineReducerRegisterAction (CombineReducerAction ):
3337 key : str
3438 reducer : ReducerType
3539
3640
37- class CombineReducerUnregisterAction (BaseCombineReducerAction ):
41+ class CombineReducerUnregisterAction (CombineReducerAction ):
3842 key : str
3943
4044
41- CombineReducerAction = CombineReducerRegisterAction | CombineReducerUnregisterAction
4245CombineReducerState = TypeVar (
4346 'CombineReducerState' ,
4447 bound = BaseCombineReducerState ,
4548)
4649AnyAction = TypeVar ('AnyAction' , bound = BaseAction )
4750
4851
49- def is_combine_reducer_action (action : BaseAction ) -> TypeGuard [CombineReducerAction ]:
50- return isinstance (action , BaseCombineReducerAction )
51-
52-
5352def combine_reducers (
5453 state_type : type [CombineReducerState ],
55- action_type : type [Action ] = BaseAction , # noqa: ARG001
56- event_type : type [Event ] = BaseEvent , # noqa: ARG001
54+ action_type : type [Action ] = BaseAction ,
55+ event_type : type [Event ] = BaseEvent ,
5756 ** reducers : ReducerType ,
5857) -> tuple [ReducerType [CombineReducerState , Action , Event ], str ]:
58+ _ = action_type , event_type
5959 reducers = reducers .copy ()
6060 _id = uuid .uuid4 ().hex
6161
@@ -76,7 +76,7 @@ def combined_reducer(
7676 result_actions = []
7777 result_events = []
7878 nonlocal state_class
79- if state is not None and is_combine_reducer_action (action ):
79+ if state is not None and isinstance (action , CombineReducerAction ):
8080 if isinstance (action , CombineReducerRegisterAction ) and action ._id == _id : # noqa: SLF001
8181 key = action .key
8282 reducer = action .reducer
@@ -86,7 +86,10 @@ def combined_reducer(
8686 ('_id' , * reducers .keys ()),
8787 frozen = True ,
8888 )
89- reducer_result = reducer (None , InitAction ())
89+ reducer_result = reducer (
90+ None ,
91+ CombineReducerInitAction (_id = _id , key = key ),
92+ )
9093 state = state_class (
9194 _id = state ._id , # noqa: SLF001
9295 ** (
@@ -136,7 +139,9 @@ def combined_reducer(
136139 reducers_results = {
137140 key : reducer (
138141 None if state is None else getattr (state , key ),
139- action ,
142+ CombineReducerInitAction (key = key , _id = _id )
143+ if isinstance (action , InitAction )
144+ else action ,
140145 )
141146 for key , reducer in reducers .items ()
142147 }
0 commit comments