Skip to content

Improved implementation of handleAction - Take 2 #109

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

Merged
merged 5 commits into from
Jul 17, 2016
Merged
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ handleAction('FETCH_DATA', {
});
```

If either `next()` or `throw()` are `undefined` or `null`, then the identity function is used for that reducer.

The optional third parameter specifies a default or initial state, which is used when `undefined` is passed to the reducer.

### `handleActions(reducerMap, ?defaultState)`
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"rimraf": "^2.5.3"
},
"dependencies": {
"lodash": "^4.13.1",
"reduce-reducers": "^0.1.0"
}
}
4 changes: 1 addition & 3 deletions src/createAction.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
function identity(t) {
return t;
}
import identity from 'lodash/identity';

export default function createAction(type, payloadCreator, metaCreator) {
const finalPayloadCreator = typeof payloadCreator === 'function'
Expand Down
34 changes: 14 additions & 20 deletions src/handleAction.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,22 @@
function isFunction(val) {
return typeof val === 'function';
}
import isFunction from 'lodash/isFunction';
import identity from 'lodash/identity';
import isNil from 'lodash/isNil';
import isSymbol from 'lodash/isSymbol';

export default function handleAction(type, reducers, defaultState) {
const typeValue = isFunction(type)
? type.toString()
: type;

return (state = defaultState, action) => {
// If action type does not match, return previous state
if (action.type !== typeValue) return state;
const typeValue = isSymbol(type)
? type
: type.toString();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This works with string action types and action creators. I think calling out isSymbol explicitly clarifies the special case (where we cannot call .toString()).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍


const handlerKey = action.error === true ? 'throw' : 'next';
const [nextReducer, throwReducer] = isFunction(reducers)
? [reducers, reducers]
: [reducers.next, reducers.throw].map(reducer => (isNil(reducer) ? identity : reducer));
Copy link
Member

@timche timche Jul 16, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This block could come after L18, so if (action.type !== typeValue) we can return state earlier and don't execute this block unnecessarily as an action will go through all reducers.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm...which part were you thinking could come after L18?

Our initial thought in moving this up the scope was #97 (comment).

Copy link
Member

@timche timche Jul 16, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright, I've gone through that again. I understand your idea now behind this. Leave it like that ;)

👍


// If function is passed instead of map, use as reducer
if (isFunction(reducers)) {
reducers.next = reducers.throw = reducers;
return (state = defaultState, action) => {
if (action.type !== typeValue) {
return state;
}

// Otherwise, assume an action map was passed
const reducer = reducers[handlerKey];

return isFunction(reducer)
? reducer(state, action)
: state;
return (action.error === true ? throwReducer : nextReducer)(state, action);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

=== true could be omitted.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What are your thoughts on #97 (comment)?

Copy link
Member

@timche timche Jul 16, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right, sorry, error could be any other value than true. === true adds more safety to respect FSA.

Copy link
Contributor Author

@yangmillstheory yangmillstheory Jul 16, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No problem! I thought the same thing when reviewing #97; thanks to @Lucretiel for doing the research and pointing it out.

};
}