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