Skip to content

Commit a3c46f5

Browse files
committed
undid changes in createSlice
1 parent 0b294ab commit a3c46f5

File tree

3 files changed

+30
-7
lines changed

3 files changed

+30
-7
lines changed

src/createSlice.test.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ describe('createSlice', () => {
6969

7070
it('should have the correct action for increment', () => {
7171
expect(actions.increment()).toEqual({
72-
type: 'increment',
72+
type: 'cool/increment',
7373
payload: undefined
7474
})
7575
})
@@ -113,7 +113,9 @@ describe('createSlice', () => {
113113
const { reducer } = createSlice({
114114
reducers: {
115115
increment: state => state + 1,
116-
multiply: (state, action) => state * action.payload,
116+
multiply: (state, action) => state * action.payload
117+
},
118+
extraReducers: {
117119
[addMore.type]: (state, action) => state + action.payload.amount
118120
},
119121
initialState: 0

src/createSlice.ts

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ export interface Slice<
3939
export interface CreateSliceOptions<
4040
S = any,
4141
A extends Action = AnyAction,
42-
CR extends CaseReducersMapObject<S, A> = CaseReducersMapObject<S, A>
42+
CR extends CaseReducersMapObject<S, A> = CaseReducersMapObject<S, A>,
43+
CR2 extends CaseReducersMapObject<S, A> = CaseReducersMapObject<S, A>
4344
> {
4445
/**
4546
* The slice's name. Used to namespace the generated action types and to
@@ -58,6 +59,17 @@ export interface CreateSliceOptions<
5859
* generated using `createAction()`.
5960
*/
6061
reducers: CR
62+
63+
/**
64+
* A mapping from action types to action-type-specific *case reducer*
65+
* functions. These reducers should have existing action types used
66+
* as the keys, and action creators will _not_ be generated.
67+
*/
68+
extraReducers?: CR2
69+
}
70+
71+
function getType(slice: string, actionKey: string): string {
72+
return slice ? `${slice}/${actionKey}` : actionKey
6173
}
6274

6375
/**
@@ -77,13 +89,20 @@ export function createSlice<
7789
): Slice<S, A, Extract<keyof CR, string>> {
7890
const { slice = '', initialState } = options
7991
const reducers = options.reducers || {}
92+
const extraReducers = options.extraReducers || {}
8093
const actionKeys = Object.keys(reducers)
8194

82-
const reducer = createReducer(initialState, reducers)
95+
const reducerMap = actionKeys.reduce((map, actionKey) => {
96+
map[getType(slice, actionKey)] = reducers[actionKey]
97+
return map
98+
}, extraReducers)
99+
100+
const reducer = createReducer(initialState, reducerMap)
83101

84102
const actionMap = actionKeys.reduce(
85103
(map, action) => {
86-
map[action] = createAction(action)
104+
const type = getType(slice, action)
105+
map[action] = createAction(type)
87106
return map
88107
},
89108
{} as any

type-tests/files/createSlice.typetest.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@ import {
1414
initialState: 0,
1515
reducers: {
1616
increment: (state: number, action) => state + action.payload,
17-
decrement: (state: number, action) => state - action.payload,
18-
"OTHER_ACTION_TYPE": (state: number, action) => state + action.payload.count
17+
decrement: (state: number, action) => state - action.payload
18+
},
19+
extraReducers: {
20+
"OTHER_ACTION_TYPE" : (state : number, action ) => state + action.payload.count
1921
}
2022
})
2123

0 commit comments

Comments
 (0)