1
- import type { Reducer } from 'redux'
1
+ import type { AnyAction , Reducer } from 'redux'
2
+ import { createNextState } from '.'
2
3
import type {
3
4
ActionCreatorWithoutPayload ,
4
5
PayloadAction ,
@@ -7,7 +8,11 @@ import type {
7
8
_ActionCreatorWithPreparedPayload ,
8
9
} from './createAction'
9
10
import { createAction } from './createAction'
10
- import type { CaseReducer , CaseReducers } from './createReducer'
11
+ import type {
12
+ CaseReducer ,
13
+ CaseReducers ,
14
+ ReducerWithInitialState ,
15
+ } from './createReducer'
11
16
import { createReducer , NotFunction } from './createReducer'
12
17
import type { ActionReducerMapBuilder } from './mapBuilders'
13
18
import { executeReducerBuilderCallback } from './mapBuilders'
@@ -253,19 +258,16 @@ export function createSlice<
253
258
> (
254
259
options : CreateSliceOptions < State , CaseReducers , Name >
255
260
) : Slice < State , CaseReducers , Name > {
256
- const { name, initialState } = options
261
+ const { name } = options
257
262
if ( ! name ) {
258
263
throw new Error ( '`name` is a required option for createSlice' )
259
264
}
265
+ const initialState =
266
+ typeof options . initialState == 'function'
267
+ ? options . initialState
268
+ : createNextState ( options . initialState , ( ) => { } )
269
+
260
270
const reducers = options . reducers || { }
261
- const [
262
- extraReducers = { } ,
263
- actionMatchers = [ ] ,
264
- defaultCaseReducer = undefined ,
265
- ] =
266
- typeof options . extraReducers === 'function'
267
- ? executeReducerBuilderCallback ( options . extraReducers )
268
- : [ options . extraReducers ]
269
271
270
272
const reducerNames = Object . keys ( reducers )
271
273
@@ -294,19 +296,36 @@ export function createSlice<
294
296
: createAction ( type )
295
297
} )
296
298
297
- const finalCaseReducers = { ...extraReducers , ...sliceCaseReducersByType }
298
- const reducer = createReducer (
299
- initialState ,
300
- finalCaseReducers as any ,
301
- actionMatchers ,
302
- defaultCaseReducer
303
- )
299
+ function buildReducer ( ) {
300
+ const [
301
+ extraReducers = { } ,
302
+ actionMatchers = [ ] ,
303
+ defaultCaseReducer = undefined ,
304
+ ] =
305
+ typeof options . extraReducers === 'function'
306
+ ? executeReducerBuilderCallback ( options . extraReducers )
307
+ : [ options . extraReducers ]
308
+
309
+ const finalCaseReducers = { ...extraReducers , ...sliceCaseReducersByType }
310
+ return createReducer (
311
+ initialState ,
312
+ finalCaseReducers as any ,
313
+ actionMatchers ,
314
+ defaultCaseReducer
315
+ )
316
+ }
317
+
318
+ let _reducer : ReducerWithInitialState < State >
304
319
305
320
return {
306
321
name,
307
- reducer,
322
+ reducer ( state , action ) {
323
+ return ( _reducer ??= buildReducer ( ) ) ( state , action )
324
+ } ,
308
325
actions : actionCreators as any ,
309
326
caseReducers : sliceCaseReducersByName as any ,
310
- getInitialState : reducer . getInitialState ,
327
+ getInitialState ( ) {
328
+ return ( _reducer ??= buildReducer ( ) ) . getInitialState ( )
329
+ } ,
311
330
}
312
331
}
0 commit comments