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