Skip to content

Support Additional arguments for handleAction(s) #161

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,15 +141,16 @@ import { handleAction } from 'redux-actions';
```

Wraps a reducer so that it only handles Flux Standard Actions of a certain type.
Generated reducer will pass down any received extra arguments.

If a `reducer` function is passed, it is used to handle both normal actions and failed actions. (A failed action is analogous to a rejected promise.) You can use this form if you know a certain type of action will never fail, like the increment example above.

Otherwise, you can specify separate reducers for `next()` and `throw()` using the `reducerMap` form. This API is inspired by the ES6 generator interface.

```js
handleAction('FETCH_DATA', {
next(state, action) {...},
throw(state, action) {...}
next(state, action, ...args) {...},
throw(state, action, ...args) {...}
}, defaultState);
```

Expand All @@ -175,11 +176,11 @@ Example:

```js
const reducer = handleActions({
INCREMENT: (state, action) => ({
INCREMENT: (state, action, ...args) => ({
counter: state.counter + action.payload
}),

DECREMENT: (state, action) => ({
DECREMENT: (state, action, ...args) => ({
counter: state.counter - action.payload
})
}, { counter: 0 });
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@
"dependencies": {
"flux-standard-action": "^1.0.0",
"invariant": "^2.2.1",
"lodash": "^4.13.1",
"reduce-reducers": "^0.1.0"
"lodash": "^4.13.1"
}
}
18 changes: 18 additions & 0 deletions src/__tests__/handleAction-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,24 @@ describe('handleAction()', () => {
counter: 7
});
});

it('should should support additional arguments', () => {
const { increment } = createActions('INCREMENT');

const reducer = handleAction(
increment, (state, { payload }, extraArg1, extraArg2) => ({
counter: state.counter + payload + extraArg1 + extraArg2
}),
{ counter: 3 }
);

const extraArg1 = 5;
const extraArg2 = 10;
expect(reducer(undefined, increment(7), extraArg1, extraArg2))
.to.deep.equal({
counter: 10 + extraArg1 + extraArg2
});
});
});
});

Expand Down
16 changes: 16 additions & 0 deletions src/__tests__/handleActions-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,4 +184,20 @@ describe('handleActions', () => {
counter: 7
});
});

it('accepts additional arguments', () => {
const incrementAction = createAction('INCREMENT');
const reducer = handleActions({
[incrementAction]: (
{ counter }, { payload: amount }, extraArg1, extraArg2
) => ({ counter: counter + amount + extraArg1 + extraArg2 })
}, defaultState);

const extraArg1 = 5;
const extraArg2 = 1;
expect(reducer({ counter: 3 }, incrementAction(7), extraArg1, extraArg2))
.to.deep.equal({
counter: 10 + extraArg1 + extraArg2
});
});
});
6 changes: 4 additions & 2 deletions src/handleAction.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export default function handleAction(actionType, reducer = identity, defaultStat
? [reducer, reducer]
: [reducer.next, reducer.throw].map(aReducer => (isNil(aReducer) ? identity : aReducer));

return (state = defaultState, action) => {
return (state = defaultState, action, ...args) => {
invariant(
isFSA(action),
'The FSA spec mandates an action object with a type. Try using the createAction(s) method.'
Expand All @@ -33,6 +33,8 @@ export default function handleAction(actionType, reducer = identity, defaultStat
return state;
}

return (action.error === true ? throwReducer : nextReducer)(state, action);
return (
action.error === true ? throwReducer : nextReducer
)(state, action, ...args);
};
}
10 changes: 7 additions & 3 deletions src/handleActions.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import handleAction from './handleAction';
import ownKeys from './ownKeys';
import reduceReducers from 'reduce-reducers';

export default function handleActions(handlers, defaultState) {
const reducers = ownKeys(handlers).map(type =>
Expand All @@ -10,6 +9,11 @@ export default function handleActions(handlers, defaultState) {
defaultState
)
);
const reducer = reduceReducers(...reducers);
return (state, action) => reducer(state, action);
const reducer = (state, action, ...args) => reducers.reduce(
(previousState, currentReducer) =>
currentReducer(previousState, action, ...args),
state
);
return (state, action, ...args) => reducer(state, action, ...args);
}