@@ -226,35 +226,27 @@ export interface ReducerHandlingContext<State> {
226226 reducer : CaseReducer < State , A extends Action ? A : A & Action > ,
227227 ) : ReducerHandlingContext < State >
228228 /**
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.
231230 * @param actionCreator The action to expose.
232231 * @example
233- * context.exposeAction("addPost", createAction<Post>("addPost" ));
232+ * context.exposeAction(createAction<Post>(type ));
234233 *
235234 * export const { addPost } = slice.actions
236235 *
237236 * dispatch(addPost(post))
238237 */
239- exposeAction (
240- name : string ,
241- actionCreator : unknown ,
242- ) : ReducerHandlingContext < State >
238+ exposeAction ( actionCreator : unknown ) : ReducerHandlingContext < State >
243239 /**
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.
246241 * @param reducer The reducer to expose.
247242 * @example
248- * context.exposeCaseReducer("addPost", (state, action: PayloadAction<Post>) => {
243+ * context.exposeCaseReducer((state, action: PayloadAction<Post>) => {
249244 * state.push(action.payload)
250245 * })
251246 *
252247 * slice.caseReducers.addPost([], addPost(post))
253248 */
254- exposeCaseReducer (
255- name : string ,
256- reducer : unknown ,
257- ) : ReducerHandlingContext < State >
249+ exposeCaseReducer ( reducer : unknown ) : ReducerHandlingContext < State >
258250 /**
259251 * Provides access to the initial state value given to the slice.
260252 * 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> = {
756748 } as const ,
757749 )
758750 } ,
759- handle ( { type, reducerName } , reducer , context ) {
751+ handle ( { type } , reducer , context ) {
760752 context
761753 . addCase ( type , reducer as any )
762- . exposeCaseReducer ( reducerName , reducer )
763- . exposeAction ( reducerName , createAction ( type ) )
754+ . exposeCaseReducer ( reducer )
755+ . exposeAction ( createAction ( type ) )
764756 } ,
765757}
766758
@@ -774,11 +766,11 @@ export const preparedReducerCreator: ReducerCreator<ReducerType.reducerWithPrepa
774766 reducer,
775767 }
776768 } ,
777- handle ( { type, reducerName } , { prepare, reducer } , context ) {
769+ handle ( { type } , { prepare, reducer } , context ) {
778770 context
779771 . addCase ( type , reducer )
780- . exposeCaseReducer ( reducerName , reducer )
781- . exposeAction ( reducerName , createAction ( type , prepare ) )
772+ . exposeCaseReducer ( reducer )
773+ . exposeAction ( createAction ( type , prepare ) )
782774 } ,
783775 }
784776
@@ -892,49 +884,64 @@ export function buildCreateSlice<
892884
893885 const getInitialState = makeGetInitialState ( options . initialState )
894886
895- const context : InternalReducerHandlingContext < State > = {
887+ const internalContext : InternalReducerHandlingContext < State > = {
896888 sliceCaseReducersByName : { } ,
897889 sliceCaseReducersByType : { } ,
898890 actionCreators : { } ,
899891 sliceMatchers : [ ] ,
900892 }
901893
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
938945 }
939946
940947 if ( isCreatorCallback ( options . reducers ) ) {
@@ -955,7 +962,11 @@ export function buildCreateSlice<
955962 reducerName,
956963 type : getType ( name , reducerName ) ,
957964 }
958- handler ( reducerDetails , reducerDefinition as any , contextMethods )
965+ handler (
966+ reducerDetails ,
967+ reducerDefinition as any ,
968+ getContext ( reducerDetails ) ,
969+ )
959970 }
960971 } else {
961972 for ( const [ reducerName , reducerDefinition ] of Object . entries (
@@ -970,7 +981,11 @@ export function buildCreateSlice<
970981 'reducer' in reducerDefinition
971982 ? preparedReducerCreator
972983 : reducerCreator
973- handler . handle ( reducerDetails , reducerDefinition as any , contextMethods )
984+ handler . handle (
985+ reducerDetails ,
986+ reducerDefinition as any ,
987+ getContext ( reducerDetails ) ,
988+ )
974989 }
975990 }
976991
@@ -993,14 +1008,14 @@ export function buildCreateSlice<
9931008
9941009 const finalCaseReducers = {
9951010 ...extraReducers ,
996- ...context . sliceCaseReducersByType ,
1011+ ...internalContext . sliceCaseReducersByType ,
9971012 }
9981013
9991014 return createReducer ( options . initialState , ( builder ) => {
10001015 for ( let key in finalCaseReducers ) {
10011016 builder . addCase ( key , finalCaseReducers [ key ] as CaseReducer )
10021017 }
1003- for ( let sM of context . sliceMatchers ) {
1018+ for ( let sM of internalContext . sliceMatchers ) {
10041019 builder . addMatcher ( sM . matcher , sM . reducer )
10051020 }
10061021 for ( let m of actionMatchers ) {
@@ -1101,8 +1116,8 @@ export function buildCreateSlice<
11011116 > = {
11021117 name,
11031118 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 ,
11061121 getInitialState,
11071122 ...makeSelectorProps ( reducerPath ) ,
11081123 injectInto ( injectable , { reducerPath : pathOpt , ...config } = { } ) {
0 commit comments