Skip to content

Commit 6a33083

Browse files
Xiaohan ZhangXiaohan Zhang
Xiaohan Zhang
authored and
Xiaohan Zhang
committed
Support Additional arguments for handleAction(s)
* resolve redux-utilities#148
1 parent 4b6a980 commit 6a33083

File tree

4 files changed

+47
-5
lines changed

4 files changed

+47
-5
lines changed

src/__tests__/handleAction-test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,22 @@ describe('handleAction()', () => {
6565
counter: 10
6666
});
6767
});
68+
69+
it('should should support additional arguments', () => {
70+
const { increment } = createActions('INCREMENT');
71+
72+
const reducer = handleAction(
73+
increment, (state, { payload }, extraArg) => ({
74+
counter: state.counter + payload + extraArg
75+
}),
76+
{ counter: 3 });
77+
78+
const counterBase = 10;
79+
expect(reducer(undefined, increment(7), counterBase))
80+
.to.deep.equal({
81+
counter: 10 + counterBase
82+
});
83+
});
6884
});
6985
});
7086

src/__tests__/handleActions-test.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,4 +147,27 @@ describe('handleActions', () => {
147147
counter: 7
148148
});
149149
});
150+
151+
it('accepts additional arguments', () => {
152+
// meaningless base and fix just for testing additional arguments
153+
const base = 5;
154+
const fix = 1;
155+
const incrementAction = createAction('INCREMENT');
156+
const reducer = handleActions({
157+
[incrementAction]: (
158+
{ counter }, { payload: amount }, extraArg1, extraArg2
159+
) => {
160+
console.log(extraArg1);
161+
console.log(extraArg2);
162+
return {
163+
counter: counter + amount + extraArg1 + extraArg2
164+
}
165+
}
166+
});
167+
168+
expect(reducer({ counter: 3 }, incrementAction(7), base, fix))
169+
.to.deep.equal({
170+
counter: 10 + base + fix
171+
});
172+
});
150173
});

src/handleAction.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@ export default function handleAction(actionType, reducers, defaultState) {
1111
? [reducers, reducers]
1212
: [reducers.next, reducers.throw].map(reducer => (isNil(reducer) ? identity : reducer));
1313

14-
return (state = defaultState, action) => {
14+
return (state = defaultState, action, ...args) => {
1515
if (!includes(actionTypes, action.type.toString())) {
1616
return state;
1717
}
1818

19-
return (action.error === true ? throwReducer : nextReducer)(state, action);
19+
return (
20+
action.error === true ? throwReducer : nextReducer
21+
)(state, action, ...args);
2022
};
2123
}

src/handleActions.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import handleAction from './handleAction';
22
import ownKeys from './ownKeys';
3-
import reduceReducers from 'reduce-reducers';
43

54
export default function handleActions(handlers, defaultState) {
65
const reducers = ownKeys(handlers).map(type => handleAction(type, handlers[type]));
7-
const reducer = reduceReducers(...reducers);
6+
const reducer = (state, action, ...args) => reducers.reduce(
7+
(p, r) => r(p, action, ...args), state);
88

9-
return (state = defaultState, action) => reducer(state, action);
9+
return (state = defaultState, action, ...args) => reducer(state, action, ...args);
1010
}
11+

0 commit comments

Comments
 (0)