python-redux 0.25.2__cp314-cp314-win_arm64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,160 @@
1
+ # ruff: noqa: D100, D103
2
+ from __future__ import annotations
3
+
4
+ import copy
5
+ import functools
6
+ import operator
7
+ import uuid
8
+ from dataclasses import fields
9
+ from typing import TYPE_CHECKING, TypeVar
10
+
11
+ from immutable import make_immutable
12
+
13
+ from .basic_types import (
14
+ Action,
15
+ BaseAction,
16
+ BaseCombineReducerState,
17
+ BaseEvent,
18
+ CombineReducerAction,
19
+ CombineReducerInitAction,
20
+ CombineReducerRegisterAction,
21
+ CombineReducerUnregisterAction,
22
+ CompleteReducerResult,
23
+ Event,
24
+ InitAction,
25
+ is_complete_reducer_result,
26
+ )
27
+
28
+ if TYPE_CHECKING:
29
+ from redux import ReducerType
30
+
31
+
32
+ CombineReducerState = TypeVar(
33
+ 'CombineReducerState',
34
+ bound=BaseCombineReducerState,
35
+ )
36
+ AnyAction = TypeVar('AnyAction', bound=BaseAction)
37
+
38
+
39
+ def combine_reducers(
40
+ state_type: type[CombineReducerState],
41
+ action_type: type[Action] = BaseAction,
42
+ event_type: type[Event] = BaseEvent,
43
+ **reducers: ReducerType,
44
+ ) -> tuple[ReducerType[CombineReducerState, Action, Event], str]:
45
+ _ = action_type, event_type
46
+ reducers = reducers.copy()
47
+ id_ = uuid.uuid4().hex
48
+
49
+ state_class = make_immutable(
50
+ state_type.__name__,
51
+ (('combine_reducers_id', str), *reducers.keys()),
52
+ )
53
+
54
+ def combined_reducer(
55
+ state: CombineReducerState | None,
56
+ action: Action,
57
+ ) -> CompleteReducerResult[CombineReducerState, Action, Event]:
58
+ result_actions = []
59
+ result_events = []
60
+ nonlocal state_class
61
+ if (
62
+ state is not None
63
+ and isinstance(action, CombineReducerAction)
64
+ and action.combine_reducers_id == id_
65
+ ):
66
+ if isinstance(action, CombineReducerRegisterAction):
67
+ key = action.key
68
+ reducer = action.reducer
69
+ reducers[key] = reducer
70
+ state_class = make_immutable(
71
+ state_type.__name__,
72
+ (('combine_reducers_id', str), *reducers.keys()),
73
+ )
74
+ reducer_result = reducer(
75
+ None,
76
+ CombineReducerInitAction(
77
+ combine_reducers_id=id_,
78
+ key=key,
79
+ payload=action.payload,
80
+ ),
81
+ )
82
+ state = state_class(
83
+ combine_reducers_id=state.combine_reducers_id,
84
+ **{
85
+ key_: (
86
+ reducer_result.state
87
+ if is_complete_reducer_result(reducer_result)
88
+ else reducer_result
89
+ )
90
+ if key == key_
91
+ else getattr(state, key_)
92
+ for key_ in reducers
93
+ },
94
+ )
95
+ result_actions += (
96
+ reducer_result.actions or []
97
+ if is_complete_reducer_result(reducer_result)
98
+ else []
99
+ )
100
+ result_events += (
101
+ reducer_result.events or []
102
+ if is_complete_reducer_result(reducer_result)
103
+ else []
104
+ )
105
+ elif isinstance(action, CombineReducerUnregisterAction):
106
+ key = action.key
107
+
108
+ del reducers[key]
109
+ fields_copy = {field.name: field for field in fields(state_class)}
110
+ annotations_copy = copy.deepcopy(state_class.__annotations__)
111
+ del fields_copy[key]
112
+ del annotations_copy[key]
113
+ state_class = make_immutable(state_type.__name__, annotations_copy)
114
+ state_class.__dataclass_fields__ = fields_copy
115
+
116
+ state = state_class(
117
+ combine_reducers_id=state.combine_reducers_id,
118
+ **{key_: getattr(state, key_) for key_ in reducers if key_ != key},
119
+ )
120
+
121
+ reducers_results = {
122
+ key: reducer(
123
+ None if state is None else getattr(state, key),
124
+ CombineReducerInitAction(key=key, combine_reducers_id=id_)
125
+ if isinstance(action, InitAction)
126
+ else action,
127
+ )
128
+ for key, reducer in reducers.items()
129
+ }
130
+ result_state = state_class(
131
+ combine_reducers_id=id_,
132
+ **{
133
+ key: result.state if is_complete_reducer_result(result) else result
134
+ for key, result in reducers_results.items()
135
+ },
136
+ )
137
+ result_actions += functools.reduce(
138
+ operator.iadd,
139
+ [
140
+ result.actions or [] if is_complete_reducer_result(result) else []
141
+ for result in reducers_results.values()
142
+ ],
143
+ [],
144
+ )
145
+ result_events += functools.reduce(
146
+ operator.iadd,
147
+ [
148
+ result.events or [] if is_complete_reducer_result(result) else []
149
+ for result in reducers_results.values()
150
+ ],
151
+ [],
152
+ )
153
+
154
+ return CompleteReducerResult(
155
+ state=result_state,
156
+ actions=result_actions,
157
+ events=result_events,
158
+ )
159
+
160
+ return combined_reducer, id_