-
Notifications
You must be signed in to change notification settings - Fork 295
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
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This block could come after L18, so There was a problem hiding this comment. Choose a reason for hiding this commentThe 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). There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What are your thoughts on #97 (comment)? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You are right, sorry, There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
}; | ||
} |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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 withlodash
as dependency and use e.g.import identity from 'lodash/identity'
instead. It has same effect but without polluting thepackage.json
and we have access to alllodash
libs if needed without installing new deps. Also maintaining is easier.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
b5546b5