-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
reducer_creator.ts
249 lines (244 loc) · 6.38 KB
/
reducer_creator.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
import { ActionCreator, ActionReducer, ActionType, Action } from './models';
// Return type of the `on` fn.
export interface On<S> {
reducer: ActionReducer<S>;
types: string[];
}
// Specialized Reducer that is aware of the Action type it needs to handle
export interface OnReducer<S, C extends ActionCreator[]> {
(state: S, action: ActionType<C[number]>): S;
}
export function on<C1 extends ActionCreator, S>(
creator1: C1,
reducer: OnReducer<S, [C1]>
): On<S>;
export function on<C1 extends ActionCreator, C2 extends ActionCreator, S>(
creator1: C1,
creator2: C2,
reducer: OnReducer<S, [C1, C2]>
): On<S>;
export function on<
C1 extends ActionCreator,
C2 extends ActionCreator,
C3 extends ActionCreator,
S
>(
creator1: C1,
creator2: C2,
creator3: C3,
reducer: OnReducer<S, [C1, C2, C3]>
): On<S>;
export function on<
C1 extends ActionCreator,
C2 extends ActionCreator,
C3 extends ActionCreator,
C4 extends ActionCreator,
S
>(
creator1: C1,
creator2: C2,
creator3: C3,
creator4: C4,
reducer: OnReducer<S, [C1, C2, C3, C4]>
): On<S>;
export function on<
C1 extends ActionCreator,
C2 extends ActionCreator,
C3 extends ActionCreator,
C4 extends ActionCreator,
C5 extends ActionCreator,
S
>(
creator1: C1,
creator2: C2,
creator3: C3,
creator4: C4,
creator5: C5,
reducer: OnReducer<S, [C1, C2, C3, C4, C5]>
): On<S>;
export function on<
C1 extends ActionCreator,
C2 extends ActionCreator,
C3 extends ActionCreator,
C4 extends ActionCreator,
C5 extends ActionCreator,
C6 extends ActionCreator,
S
>(
creator1: C1,
creator2: C2,
creator3: C3,
creator4: C4,
creator5: C5,
creator6: C6,
reducer: OnReducer<S, [C1, C2, C3, C4, C5, C6]>
): On<S>;
export function on<
C1 extends ActionCreator,
C2 extends ActionCreator,
C3 extends ActionCreator,
C4 extends ActionCreator,
C5 extends ActionCreator,
C6 extends ActionCreator,
C7 extends ActionCreator,
S
>(
creator1: C1,
creator2: C2,
creator3: C3,
creator4: C4,
creator5: C5,
creator6: C6,
creator7: C7,
reducer: OnReducer<S, [C1, C2, C3, C4, C5, C6, C7]>
): On<S>;
export function on<
C1 extends ActionCreator,
C2 extends ActionCreator,
C3 extends ActionCreator,
C4 extends ActionCreator,
C5 extends ActionCreator,
C6 extends ActionCreator,
C7 extends ActionCreator,
C8 extends ActionCreator,
S
>(
creator1: C1,
creator2: C2,
creator3: C3,
creator4: C4,
creator5: C5,
creator6: C6,
creator7: C7,
creator8: C8,
reducer: OnReducer<S, [C1, C2, C3, C4, C5, C6, C7, C8]>
): On<S>;
export function on<
C1 extends ActionCreator,
C2 extends ActionCreator,
C3 extends ActionCreator,
C4 extends ActionCreator,
C5 extends ActionCreator,
C6 extends ActionCreator,
C7 extends ActionCreator,
C8 extends ActionCreator,
C9 extends ActionCreator,
S
>(
creator1: C1,
creator2: C2,
creator3: C3,
creator4: C4,
creator5: C5,
creator6: C6,
creator7: C7,
creator8: C8,
creator9: C9,
reducer: OnReducer<S, [C1, C2, C3, C4, C5, C6, C7, C8, C9]>
): On<S>;
export function on<
C1 extends ActionCreator,
C2 extends ActionCreator,
C3 extends ActionCreator,
C4 extends ActionCreator,
C5 extends ActionCreator,
C6 extends ActionCreator,
C7 extends ActionCreator,
C8 extends ActionCreator,
C9 extends ActionCreator,
C10 extends ActionCreator,
S
>(
creator1: C1,
creator2: C2,
creator3: C3,
creator4: C4,
creator5: C5,
creator6: C6,
creator7: C7,
creator8: C8,
creator9: C9,
creator10: C10,
reducer: OnReducer<S, [C1, C2, C3, C4, C5, C6, C7, C8, C9, C10]>
): On<S>;
export function on<S>(
creator: ActionCreator,
...rest: (ActionCreator | OnReducer<S, [ActionCreator]>)[]
): On<S>;
/**
* @description
* Associates actions with a given state change function.
* A state change function must be provided as the last parameter.
*
* @param args `ActionCreator`'s followed by a state change function.
*
* **To maintain type-safety**: pass 10 or less `ActionCreator`'s.
* @returns an association of action types with a state change function.
*/
export function on(
...args: (ActionCreator | Function)[]
): { reducer: Function; types: string[] } {
const reducer = args.pop() as Function;
const types = args.reduce(
(result, creator) => [...result, (creator as ActionCreator).type],
[] as string[]
);
return { reducer, types };
}
/**
* @description
* Creates a reducer function to handle state transitions.
*
* Reducer creators reduce the explicitness of reducer functions with switch statements.
*
* @param initialState Provides a state value if the current state is `undefined`, as it is initially.
* @param ons Associations between actions and state changes.
* @returns A reducer function.
*
* @usageNotes
*
* - Must be used with `ActionCreator`'s (returned by `createAction`). Cannot be used with class-based action creators.
* - An action can be associated with multiple state change functions. In this case the functions will be executed in the specified order.
* - The returned `ActionReducer` should additionally be returned from an exported `reducer` function.
* This is because [function calls are not supported](https://angular.io/guide/aot-compiler#function-calls-are-not-supported) by the AOT compiler.
*
* **Declaring a reducer creator with an exported reducer function**
*
* ```ts
* const featureReducer = createReducer(
* initialState,
* on(
* featureActions.actionOne,
* featureActions.actionTwo,
* (state, { updatedValue }) => ({ ...state, prop: updatedValue })
* ),
* on(featureActions.actionThree, () => initialState);
* );
*
* export function reducer(state: State | undefined, action: Action) {
* return featureReducer(state, action);
* }
* ```
*/
export function createReducer<S, A extends Action = Action>(
initialState: S,
...ons: On<S>[]
): ActionReducer<S, A> {
const map = new Map<string, ActionReducer<S, A>>();
for (let on of ons) {
for (let type of on.types) {
if (map.has(type)) {
const existingReducer = map.get(type) as ActionReducer<S, A>;
const newReducer: ActionReducer<S, A> = (state, action) =>
on.reducer(existingReducer(state, action), action);
map.set(type, newReducer);
} else {
map.set(type, on.reducer);
}
}
}
return function(state: S = initialState, action: A): S {
const reducer = map.get(action.type);
return reducer ? reducer(state, action) : state;
};
}