Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@
"rimraf": "^2.5.3"
},
"dependencies": {
"lodash.identity": "^3.0.0",
"lodash.isfunction": "^3.0.8",
"lodash.isnil": "^4.0.0",
"lodash.issymbol": "^4.0.1",
Copy link
Contributor Author

@yangmillstheory yangmillstheory Jul 14, 2016

Choose a reason for hiding this comment

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

This would be a new change (originally suggested by @Lucretiel); I think it's a good one. Thoughts?

Willing to roll our own to minimize dependencies, but why re-invent the wheel?

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.

Using lodash is fine, but I'd suggest to just go with lodash as dependency and use e.g. import identity from 'lodash/identity' instead. It has same effect but without polluting the package.json and we have access to all lodash libs if needed without installing new deps. Also maintaining is easier.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

"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.

};
}