1 | import createNextState, { Draft, isDraft, isDraftable } from 'immer'
|
2 | import { AnyAction, Action, Reducer } from 'redux'
|
3 | import {
|
4 | executeReducerBuilderCallback,
|
5 | ActionReducerMapBuilder
|
6 | } from './mapBuilders'
|
7 | import { NoInfer } from './tsHelpers'
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | export type Actions<T extends keyof any = string> = Record<T, Action>
|
18 |
|
19 | export interface ActionMatcher<A extends AnyAction> {
|
20 | (action: AnyAction): action is A
|
21 | }
|
22 |
|
23 | export type ActionMatcherDescription<S, A extends AnyAction> = {
|
24 | matcher: ActionMatcher<A>
|
25 | reducer: CaseReducer<S, NoInfer<A>>
|
26 | }
|
27 |
|
28 | export type ActionMatcherDescriptionCollection<S> = Array<
|
29 | ActionMatcherDescription<S, any>
|
30 | >
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 | export type CaseReducer<S = any, A extends Action = AnyAction> = (
|
49 | state: Draft<S>,
|
50 | action: A
|
51 | ) => S | void | Draft<S>
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 | export type CaseReducers<S, AS extends Actions> = {
|
63 | [T in keyof AS]: AS[T] extends Action ? CaseReducer<S, AS[T]> : void
|
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 | export function createReducer<S>(
|
131 | initialState: S,
|
132 | builderCallback: (builder: ActionReducerMapBuilder<S>) => void
|
133 | ): Reducer<S>
|
134 |
|
135 | /**
|
136 | * A utility function that allows defining a reducer as a mapping from action
|
137 | * type to *case reducer* functions that handle these action types. The
|
138 | * reducer's initial state is passed as the first argument.
|
139 | *
|
140 | * The body of every case reducer is implicitly wrapped with a call to
|
141 | * `produce()` from the [immer](https:
|
142 | * This means that rather than returning a new state object, you can also
|
143 | * mutate the passed-in state object directly; these mutations will then be
|
144 | * automatically and efficiently translated into copies, giving you both
|
145 | * convenience and immutability.
|
146 | *
|
147 | * @overloadSummary
|
148 | * This overload accepts an object where the keys are string action types, and the values
|
149 | * are case reducer functions to handle those action types.
|
150 | *
|
151 | * @param initialState - The initial state that should be used when the reducer is called the first time.
|
152 | * @param actionsMap - An object mapping from action types to _case reducers_, each of which handles one specific action type.
|
153 | * @param actionMatchers - An array of matcher definitions in the form `{matcher, reducer}`.
|
154 | * All matching reducers will be executed in order, independently if a case reducer matched or not.
|
155 | * @param defaultCaseReducer - A "default case" reducer that is executed if no case reducer and no matcher
|
156 | * reducer was executed for this action.
|
157 | *
|
158 | * @example
|
159 | ```js
|
160 | const counterReducer = createReducer(0, {
|
161 | increment: (state, action) => state + action.payload,
|
162 | decrement: (state, action) => state - action.payload
|
163 | })
|
164 | ```
|
165 |
|
166 | * Action creators that were generated using [`createAction`](./createAction) may be used directly as the keys here, using computed property syntax:
|
167 |
|
168 | ```js
|
169 | const increment = createAction('increment')
|
170 | const decrement = createAction('decrement')
|
171 |
|
172 | const counterReducer = createReducer(0, {
|
173 | [increment]: (state, action) => state + action.payload,
|
174 | [decrement.type]: (state, action) => state - action.payload
|
175 | })
|
176 | ```
|
177 | * @public
|
178 | */
|
179 | export function createReducer<
|
180 | S,
|
181 | CR extends CaseReducers<S, any> = CaseReducers<S, any>
|
182 | >(
|
183 | initialState: S,
|
184 | actionsMap: CR,
|
185 | actionMatchers?: ActionMatcherDescriptionCollection<S>,
|
186 | defaultCaseReducer?: CaseReducer<S>
|
187 | ): Reducer<S>
|
188 |
|
189 | export function createReducer<S>(
|
190 | initialState: S,
|
191 | mapOrBuilderCallback:
|
192 | | CaseReducers<S, any>
|
193 | | ((builder: ActionReducerMapBuilder<S>) => void),
|
194 | actionMatchers: ActionMatcherDescriptionCollection<S> = [],
|
195 | defaultCaseReducer?: CaseReducer<S>
|
196 | ): Reducer<S> {
|
197 | let [actionsMap, finalActionMatchers, finalDefaultCaseReducer] =
|
198 | typeof mapOrBuilderCallback === 'function'
|
199 | ? executeReducerBuilderCallback(mapOrBuilderCallback)
|
200 | : [mapOrBuilderCallback, actionMatchers, defaultCaseReducer]
|
201 |
|
202 | const frozenInitialState = createNextState(initialState, () => {})
|
203 |
|
204 | return function(state = frozenInitialState, action): S {
|
205 | let caseReducers = [
|
206 | actionsMap[action.type],
|
207 | ...finalActionMatchers
|
208 | .filter(({ matcher }) => matcher(action))
|
209 | .map(({ reducer }) => reducer)
|
210 | ]
|
211 | if (caseReducers.filter(cr => !!cr).length === 0) {
|
212 | caseReducers = [finalDefaultCaseReducer]
|
213 | }
|
214 |
|
215 | return caseReducers.reduce((previousState, caseReducer): S => {
|
216 | if (caseReducer) {
|
217 | if (isDraft(previousState)) {
|
218 | // If it's already a draft, we must already be inside a `createNextState` call,
|
219 |
|
220 |
|
221 | const draft = previousState as Draft<S>
|
222 | const result = caseReducer(draft, action)
|
223 |
|
224 | if (typeof result === 'undefined') {
|
225 | return previousState
|
226 | }
|
227 |
|
228 | return result as S
|
229 | } else if (!isDraftable(previousState)) {
|
230 |
|
231 |
|
232 | const result = caseReducer(previousState as any, action)
|
233 |
|
234 | if (typeof result === 'undefined') {
|
235 | if (previousState === null) {
|
236 | return previousState
|
237 | }
|
238 | throw Error(
|
239 | 'A case reducer on a non-draftable value must not return undefined'
|
240 | )
|
241 | }
|
242 |
|
243 | return result as S
|
244 | } else {
|
245 |
|
246 |
|
247 |
|
248 | return createNextState(previousState, (draft: Draft<S>) => {
|
249 | return caseReducer(draft, action)
|
250 | })
|
251 | }
|
252 | }
|
253 |
|
254 | return previousState
|
255 | }, state)
|
256 | }
|
257 | }
|