-
Notifications
You must be signed in to change notification settings - Fork 296
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 all 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 |
---|---|---|
|
@@ -48,6 +48,7 @@ | |
"rimraf": "^2.5.3" | ||
}, | ||
"dependencies": { | ||
"lodash": "^4.13.1", | ||
"reduce-reducers": "^0.1.0" | ||
} | ||
} |
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(); | ||
|
||
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. |
||
}; | ||
} |
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 works with string action types and action creators. I think calling out
isSymbol
explicitly clarifies the special case (where we cannot call.toString()
).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.
👍