Skip to content

Commit d6e1783

Browse files
committed
fix: CombineReducerRegisterAction should take care of CompleteReducerResult returned by the sub-reducer on its initialization.
1 parent 7f5f35c commit d6e1783

File tree

3 files changed

+28
-5
lines changed

3 files changed

+28
-5
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Changelog
22

3+
## Version 0.9.22
4+
5+
- fix: `CombineReducerRegisterAction` should take care of `CompleteReducerResult`
6+
returned by the sub-reducer on its initialization.
7+
38
## Version 0.9.21
49

510
- feat: new option for all subscriptions to hint them keep a weakref of the callback

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "python-redux"
3-
version = "0.9.21"
3+
version = "0.9.22"
44
description = "Redux implementation for Python"
55
authors = ["Sassan Haradji <sassanh@gmail.com>"]
66
license = "Apache-2.0"

redux/combine_reducers.py

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ def combine_reducers(
5656
event_type: type[Event] = BaseEvent, # noqa: ARG001
5757
**reducers: ReducerType,
5858
) -> tuple[ReducerType[CombineReducerState, Action, Event], str]:
59+
reducers = reducers.copy()
5960
_id = uuid.uuid4().hex
6061

6162
state_class = cast(
@@ -72,6 +73,8 @@ def combined_reducer(
7273
state: CombineReducerState | None,
7374
action: Action,
7475
) -> CompleteReducerResult[CombineReducerState, Action, Event]:
76+
result_actions = []
77+
result_events = []
7578
nonlocal state_class
7679
if state is not None and is_combine_reducer_action(action):
7780
if isinstance(action, CombineReducerRegisterAction) and action._id == _id: # noqa: SLF001
@@ -83,17 +86,32 @@ def combined_reducer(
8386
('_id', *reducers.keys()),
8487
frozen=True,
8588
)
89+
reducer_result = reducer(None, InitAction())
8690
state = state_class(
8791
_id=state._id, # noqa: SLF001
8892
**(
8993
{
90-
key_: reducer(None, InitAction())
94+
key_: (
95+
reducer_result.state
96+
if is_reducer_result(reducer_result)
97+
else reducer_result
98+
)
9199
if key == key_
92100
else getattr(state, key_)
93101
for key_ in reducers
94102
}
95103
),
96104
)
105+
result_actions += (
106+
reducer_result.actions or []
107+
if is_reducer_result(reducer_result)
108+
else []
109+
)
110+
result_events += (
111+
reducer_result.events or []
112+
if is_reducer_result(reducer_result)
113+
else []
114+
)
97115
elif (
98116
isinstance(action, CombineReducerUnregisterAction) and action._id == _id # noqa: SLF001
99117
):
@@ -129,14 +147,14 @@ def combined_reducer(
129147
for key, result in reducers_results.items()
130148
},
131149
)
132-
result_actions = sum(
150+
result_actions += sum(
133151
[
134152
result.actions or [] if is_reducer_result(result) else []
135153
for result in reducers_results.values()
136154
],
137155
[],
138156
)
139-
result_events = sum(
157+
result_events += sum(
140158
[
141159
result.events or [] if is_reducer_result(result) else []
142160
for result in reducers_results.values()
@@ -150,4 +168,4 @@ def combined_reducer(
150168
events=result_events,
151169
)
152170

153-
return (combined_reducer, _id)
171+
return combined_reducer, _id

0 commit comments

Comments
 (0)