1 | import { Reducer } from 'redux'
|
2 | import {
|
3 | ActionCreatorWithoutPayload,
|
4 | createAction,
|
5 | PayloadAction,
|
6 | PayloadActionCreator,
|
7 | PrepareAction,
|
8 | _ActionCreatorWithPreparedPayload
|
9 | } from './createAction'
|
10 | import { CaseReducer, CaseReducers, createReducer } from './createReducer'
|
11 | import {
|
12 | ActionReducerMapBuilder,
|
13 | executeReducerBuilderCallback
|
14 | } from './mapBuilders'
|
15 | import { NoInfer } from './tsHelpers'
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 | export type SliceActionCreator<P> = PayloadActionCreator<P>
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 | export interface Slice<
|
32 | State = any,
|
33 | CaseReducers extends SliceCaseReducers<State> = SliceCaseReducers<State>,
|
34 | Name extends string = string
|
35 | > {
|
36 | |
37 |
|
38 |
|
39 | name: Name
|
40 |
|
41 | |
42 |
|
43 |
|
44 | reducer: Reducer<State>
|
45 |
|
46 | |
47 |
|
48 |
|
49 |
|
50 | actions: CaseReducerActions<CaseReducers>
|
51 |
|
52 | |
53 |
|
54 |
|
55 |
|
56 | caseReducers: SliceDefinedCaseReducers<CaseReducers>
|
57 | }
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 | export interface CreateSliceOptions<
|
65 | State = any,
|
66 | CR extends SliceCaseReducers<State> = SliceCaseReducers<State>,
|
67 | Name extends string = string
|
68 | > {
|
69 | |
70 |
|
71 |
|
72 | name: Name
|
73 |
|
74 | |
75 |
|
76 |
|
77 | initialState: State
|
78 |
|
79 | |
80 |
|
81 |
|
82 |
|
83 |
|
84 | reducers: ValidateSliceCaseReducers<State, CR>
|
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 | extraReducers?:
|
132 | | CaseReducers<NoInfer<State>, any>
|
133 | | ((builder: ActionReducerMapBuilder<NoInfer<State>>) => void)
|
134 | }
|
135 |
|
136 | /**
|
137 | * A CaseReducer with a `prepare` method.
|
138 | *
|
139 | * @public
|
140 | */
|
141 | export type CaseReducerWithPrepare<State, Action extends PayloadAction> = {
|
142 | reducer: CaseReducer<State, Action>
|
143 | prepare: PrepareAction<Action['payload']>
|
144 | }
|
145 |
|
146 | /**
|
147 | * The type describing a slice's `reducers` option.
|
148 | *
|
149 | * @public
|
150 | */
|
151 | export type SliceCaseReducers<State> = {
|
152 | [K: string]:
|
153 | | CaseReducer<State, PayloadAction<any>>
|
154 | | CaseReducerWithPrepare<State, PayloadAction<any, string, any, any>>
|
155 | }
|
156 |
|
157 | /**
|
158 | * Derives the slice's `actions` property from the `reducers` options
|
159 | *
|
160 | * @public
|
161 | */
|
162 | export type CaseReducerActions<CaseReducers extends SliceCaseReducers<any>> = {
|
163 | [Type in keyof CaseReducers]: CaseReducers[Type] extends { prepare: any }
|
164 | ? ActionCreatorForCaseReducerWithPrepare<CaseReducers[Type]>
|
165 | : ActionCreatorForCaseReducer<CaseReducers[Type]>
|
166 | }
|
167 |
|
168 | /**
|
169 | * Get a `PayloadActionCreator` type for a passed `CaseReducerWithPrepare`
|
170 | *
|
171 | * @internal
|
172 | */
|
173 | type ActionCreatorForCaseReducerWithPrepare<
|
174 | CR extends { prepare: any }
|
175 | > = _ActionCreatorWithPreparedPayload<CR['prepare'], string>
|
176 |
|
177 | /**
|
178 | * Get a `PayloadActionCreator` type for a passed `CaseReducer`
|
179 | *
|
180 | * @internal
|
181 | */
|
182 | type ActionCreatorForCaseReducer<CR> = CR extends (
|
183 | state: any,
|
184 | action: infer Action
|
185 | ) => any
|
186 | ? Action extends { payload: infer P }
|
187 | ? PayloadActionCreator<P>
|
188 | : ActionCreatorWithoutPayload
|
189 | : ActionCreatorWithoutPayload
|
190 |
|
191 |
|
192 |
|
193 |
|
194 |
|
195 |
|
196 |
|
197 | type SliceDefinedCaseReducers<CaseReducers extends SliceCaseReducers<any>> = {
|
198 | [Type in keyof CaseReducers]: CaseReducers[Type] extends {
|
199 | reducer: infer Reducer
|
200 | }
|
201 | ? Reducer
|
202 | : CaseReducers[Type]
|
203 | }
|
204 |
|
205 |
|
206 |
|
207 |
|
208 |
|
209 |
|
210 |
|
211 |
|
212 |
|
213 |
|
214 |
|
215 |
|
216 |
|
217 | export type ValidateSliceCaseReducers<
|
218 | S,
|
219 | ACR extends SliceCaseReducers<S>
|
220 | > = ACR &
|
221 | {
|
222 | [T in keyof ACR]: ACR[T] extends {
|
223 | reducer(s: S, action?: infer A): any
|
224 | }
|
225 | ? {
|
226 | prepare(...a: never[]): Omit<A, 'type'>
|
227 | }
|
228 | : {}
|
229 | }
|
230 |
|
231 | function getType(slice: string, actionKey: string): string {
|
232 | return `${slice}/${actionKey}`
|
233 | }
|
234 |
|
235 |
|
236 |
|
237 |
|
238 |
|
239 |
|
240 |
|
241 |
|
242 |
|
243 |
|
244 |
|
245 | export function createSlice<
|
246 | State,
|
247 | CaseReducers extends SliceCaseReducers<State>,
|
248 | Name extends string = string
|
249 | >(
|
250 | options: CreateSliceOptions<State, CaseReducers, Name>
|
251 | ): Slice<State, CaseReducers, Name> {
|
252 | const { name, initialState } = options
|
253 | if (!name) {
|
254 | throw new Error('`name` is a required option for createSlice')
|
255 | }
|
256 | const reducers = options.reducers || {}
|
257 | const [
|
258 | extraReducers = {},
|
259 | actionMatchers = [],
|
260 | defaultCaseReducer = undefined
|
261 | ] =
|
262 | typeof options.extraReducers === 'undefined'
|
263 | ? []
|
264 | : typeof options.extraReducers === 'function'
|
265 | ? executeReducerBuilderCallback(options.extraReducers)
|
266 | : [options.extraReducers]
|
267 |
|
268 | const reducerNames = Object.keys(reducers)
|
269 |
|
270 | const sliceCaseReducersByName: Record<string, CaseReducer> = {}
|
271 | const sliceCaseReducersByType: Record<string, CaseReducer> = {}
|
272 | const actionCreators: Record<string, Function> = {}
|
273 |
|
274 | reducerNames.forEach(reducerName => {
|
275 | const maybeReducerWithPrepare = reducers[reducerName]
|
276 | const type = getType(name, reducerName)
|
277 |
|
278 | let caseReducer: CaseReducer<State, any>
|
279 | let prepareCallback: PrepareAction<any> | undefined
|
280 |
|
281 | if ('reducer' in maybeReducerWithPrepare) {
|
282 | caseReducer = maybeReducerWithPrepare.reducer
|
283 | prepareCallback = maybeReducerWithPrepare.prepare
|
284 | } else {
|
285 | caseReducer = maybeReducerWithPrepare
|
286 | }
|
287 |
|
288 | sliceCaseReducersByName[reducerName] = caseReducer
|
289 | sliceCaseReducersByType[type] = caseReducer
|
290 | actionCreators[reducerName] = prepareCallback
|
291 | ? createAction(type, prepareCallback)
|
292 | : createAction(type)
|
293 | })
|
294 |
|
295 | const finalCaseReducers = { ...extraReducers, ...sliceCaseReducersByType }
|
296 | const reducer = createReducer(
|
297 | initialState,
|
298 | finalCaseReducers as any,
|
299 | actionMatchers,
|
300 | defaultCaseReducer
|
301 | )
|
302 |
|
303 | return {
|
304 | name,
|
305 | reducer,
|
306 | actions: actionCreators as any,
|
307 | caseReducers: sliceCaseReducersByName as any
|
308 | }
|
309 | }
|