@@ -226,35 +226,27 @@ export interface ReducerHandlingContext<State> {
226
226
reducer : CaseReducer < State , A extends Action ? A : A & Action > ,
227
227
) : ReducerHandlingContext < State >
228
228
/**
229
- * Add an action to be exposed under the final `slice.actions` key.
230
- * @param name The key to be exposed as.
229
+ * Add an action to be exposed under the final `slice.actions[reducerName]` key.
231
230
* @param actionCreator The action to expose.
232
231
* @example
233
- * context.exposeAction("addPost", createAction<Post>("addPost" ));
232
+ * context.exposeAction(createAction<Post>(type ));
234
233
*
235
234
* export const { addPost } = slice.actions
236
235
*
237
236
* dispatch(addPost(post))
238
237
*/
239
- exposeAction (
240
- name : string ,
241
- actionCreator : unknown ,
242
- ) : ReducerHandlingContext < State >
238
+ exposeAction ( actionCreator : unknown ) : ReducerHandlingContext < State >
243
239
/**
244
- * Add a case reducer to be exposed under the final `slice.caseReducers` key.
245
- * @param name The key to be exposed as.
240
+ * Add a case reducer to be exposed under the final `slice.caseReducers[reducerName]` key.
246
241
* @param reducer The reducer to expose.
247
242
* @example
248
- * context.exposeCaseReducer("addPost", (state, action: PayloadAction<Post>) => {
243
+ * context.exposeCaseReducer((state, action: PayloadAction<Post>) => {
249
244
* state.push(action.payload)
250
245
* })
251
246
*
252
247
* slice.caseReducers.addPost([], addPost(post))
253
248
*/
254
- exposeCaseReducer (
255
- name : string ,
256
- reducer : unknown ,
257
- ) : ReducerHandlingContext < State >
249
+ exposeCaseReducer ( reducer : unknown ) : ReducerHandlingContext < State >
258
250
/**
259
251
* Provides access to the initial state value given to the slice.
260
252
* If a lazy state initializer was provided, it will be called and a fresh value returned.
@@ -756,11 +748,11 @@ export const reducerCreator: ReducerCreator<ReducerType.reducer> = {
756
748
} as const ,
757
749
)
758
750
} ,
759
- handle ( { type, reducerName } , reducer , context ) {
751
+ handle ( { type } , reducer , context ) {
760
752
context
761
753
. addCase ( type , reducer as any )
762
- . exposeCaseReducer ( reducerName , reducer )
763
- . exposeAction ( reducerName , createAction ( type ) )
754
+ . exposeCaseReducer ( reducer )
755
+ . exposeAction ( createAction ( type ) )
764
756
} ,
765
757
}
766
758
@@ -774,11 +766,11 @@ export const preparedReducerCreator: ReducerCreator<ReducerType.reducerWithPrepa
774
766
reducer,
775
767
}
776
768
} ,
777
- handle ( { type, reducerName } , { prepare, reducer } , context ) {
769
+ handle ( { type } , { prepare, reducer } , context ) {
778
770
context
779
771
. addCase ( type , reducer )
780
- . exposeCaseReducer ( reducerName , reducer )
781
- . exposeAction ( reducerName , createAction ( type , prepare ) )
772
+ . exposeCaseReducer ( reducer )
773
+ . exposeAction ( createAction ( type , prepare ) )
782
774
} ,
783
775
}
784
776
@@ -892,49 +884,64 @@ export function buildCreateSlice<
892
884
893
885
const getInitialState = makeGetInitialState ( options . initialState )
894
886
895
- const context : InternalReducerHandlingContext < State > = {
887
+ const internalContext : InternalReducerHandlingContext < State > = {
896
888
sliceCaseReducersByName : { } ,
897
889
sliceCaseReducersByType : { } ,
898
890
actionCreators : { } ,
899
891
sliceMatchers : [ ] ,
900
892
}
901
893
902
- const contextMethods : ReducerHandlingContext < State > = {
903
- addCase (
904
- typeOrActionCreator : string | TypedActionCreator < any > ,
905
- reducer : CaseReducer < State > ,
906
- ) {
907
- const type =
908
- typeof typeOrActionCreator === 'string'
909
- ? typeOrActionCreator
910
- : typeOrActionCreator . type
911
- if ( ! type ) {
912
- throw new Error (
913
- '`context.addCase` cannot be called with an empty action type' ,
914
- )
915
- }
916
- if ( type in context . sliceCaseReducersByType ) {
917
- throw new Error (
918
- '`context.addCase` cannot be called with two reducers for the same action type: ' +
919
- type ,
920
- )
921
- }
922
- context . sliceCaseReducersByType [ type ] = reducer
923
- return contextMethods
924
- } ,
925
- addMatcher ( matcher , reducer ) {
926
- context . sliceMatchers . push ( { matcher, reducer } )
927
- return contextMethods
928
- } ,
929
- exposeAction ( name , actionCreator ) {
930
- context . actionCreators [ name ] = actionCreator
931
- return contextMethods
932
- } ,
933
- exposeCaseReducer ( name , reducer ) {
934
- context . sliceCaseReducersByName [ name ] = reducer
935
- return contextMethods
936
- } ,
937
- getInitialState,
894
+ function getContext ( { reducerName } : ReducerDetails ) {
895
+ const context : ReducerHandlingContext < State > = {
896
+ addCase (
897
+ typeOrActionCreator : string | TypedActionCreator < any > ,
898
+ reducer : CaseReducer < State > ,
899
+ ) {
900
+ const type =
901
+ typeof typeOrActionCreator === 'string'
902
+ ? typeOrActionCreator
903
+ : typeOrActionCreator . type
904
+ if ( ! type ) {
905
+ throw new Error (
906
+ '`context.addCase` cannot be called with an empty action type' ,
907
+ )
908
+ }
909
+ if ( type in internalContext . sliceCaseReducersByType ) {
910
+ throw new Error (
911
+ '`context.addCase` cannot be called with two reducers for the same action type: ' +
912
+ type ,
913
+ )
914
+ }
915
+ internalContext . sliceCaseReducersByType [ type ] = reducer
916
+ return context
917
+ } ,
918
+ addMatcher ( matcher , reducer ) {
919
+ internalContext . sliceMatchers . push ( { matcher, reducer } )
920
+ return context
921
+ } ,
922
+ exposeAction ( actionCreator ) {
923
+ if ( reducerName in internalContext . actionCreators ) {
924
+ throw new Error (
925
+ 'context.exposeAction cannot be called twice for the same reducer definition:' +
926
+ reducerName ,
927
+ )
928
+ }
929
+ internalContext . actionCreators [ reducerName ] = actionCreator
930
+ return context
931
+ } ,
932
+ exposeCaseReducer ( reducer ) {
933
+ if ( reducerName in internalContext . sliceCaseReducersByName ) {
934
+ throw new Error (
935
+ 'context.exposeCaseReducer cannot be called twice for the same reducer definition:' +
936
+ reducerName ,
937
+ )
938
+ }
939
+ internalContext . sliceCaseReducersByName [ reducerName ] = reducer
940
+ return context
941
+ } ,
942
+ getInitialState,
943
+ }
944
+ return context
938
945
}
939
946
940
947
if ( isCreatorCallback ( options . reducers ) ) {
@@ -955,7 +962,11 @@ export function buildCreateSlice<
955
962
reducerName,
956
963
type : getType ( name , reducerName ) ,
957
964
}
958
- handler ( reducerDetails , reducerDefinition as any , contextMethods )
965
+ handler (
966
+ reducerDetails ,
967
+ reducerDefinition as any ,
968
+ getContext ( reducerDetails ) ,
969
+ )
959
970
}
960
971
} else {
961
972
for ( const [ reducerName , reducerDefinition ] of Object . entries (
@@ -970,7 +981,11 @@ export function buildCreateSlice<
970
981
'reducer' in reducerDefinition
971
982
? preparedReducerCreator
972
983
: reducerCreator
973
- handler . handle ( reducerDetails , reducerDefinition as any , contextMethods )
984
+ handler . handle (
985
+ reducerDetails ,
986
+ reducerDefinition as any ,
987
+ getContext ( reducerDetails ) ,
988
+ )
974
989
}
975
990
}
976
991
@@ -993,14 +1008,14 @@ export function buildCreateSlice<
993
1008
994
1009
const finalCaseReducers = {
995
1010
...extraReducers ,
996
- ...context . sliceCaseReducersByType ,
1011
+ ...internalContext . sliceCaseReducersByType ,
997
1012
}
998
1013
999
1014
return createReducer ( options . initialState , ( builder ) => {
1000
1015
for ( let key in finalCaseReducers ) {
1001
1016
builder . addCase ( key , finalCaseReducers [ key ] as CaseReducer )
1002
1017
}
1003
- for ( let sM of context . sliceMatchers ) {
1018
+ for ( let sM of internalContext . sliceMatchers ) {
1004
1019
builder . addMatcher ( sM . matcher , sM . reducer )
1005
1020
}
1006
1021
for ( let m of actionMatchers ) {
@@ -1101,8 +1116,8 @@ export function buildCreateSlice<
1101
1116
> = {
1102
1117
name,
1103
1118
reducer,
1104
- actions : context . actionCreators as any ,
1105
- caseReducers : context . sliceCaseReducersByName as any ,
1119
+ actions : internalContext . actionCreators as any ,
1120
+ caseReducers : internalContext . sliceCaseReducersByName as any ,
1106
1121
getInitialState,
1107
1122
...makeSelectorProps ( reducerPath ) ,
1108
1123
injectInto ( injectable , { reducerPath : pathOpt , ...config } = { } ) {
0 commit comments