-
Notifications
You must be signed in to change notification settings - Fork 300
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
createAction does more pre-processing #210
Conversation
Slightly simplified createAction, and made it do more processing ahead of time that doesn't have to be done every time an action is emitted
src/createAction.js
Outdated
@@ -10,35 +9,30 @@ export default function createAction(type, payloadCreator = identity, metaCreato | |||
'Expected payloadCreator to be a function, undefined or null' | |||
); | |||
|
|||
const finalPayloadCreator = isNull(payloadCreator) | |||
const finalPayloadCreator = payloadCreator === null || payloadCreator === identity |
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.
Not sure why we're not using isNull
anymore? Also why is the identity
check needed?
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.
- No reason. I just figured that the purpose of
isNull
is to be a predicate to functions likefilter
, so there's no reason to have the function call overhead here. - The
identity
check means that if thepayloadCreator
isidentity
, we can skip theinstanceof Error
check every time it's called, and don't have to use the latter version of thefinalPayloadCreator
.
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.
- re: overhead, this is called just once during when defining the action creator, so do we need to worry about it? If not, I would advise removing the
isFunction
call above and below as well for consistency, but would prefer to just keep as is.
src/createAction.js
Outdated
action.meta = metaCreator(...args); | ||
} | ||
|
||
return action; | ||
}; | ||
|
||
actionCreator.toString = () => type.toString(); | ||
const typeStr = type.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.
Is the local declaration needed, given that it's used in one place?
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.
It means that we don't have to call type.toString
every time actionCreator.toString
is called.
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.
Can we move this declaration up a scope?
src/createAction.js
Outdated
const payload = hasError ? args[0] : finalPayloadCreator(...args); | ||
if (!isUndefined(payload)) { | ||
const payload = finalPayloadCreator(...args); | ||
const action = { type, error: payload instanceof Error }; |
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 makes the key error
always be present. Do we really want this new behavior?
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.
I don't have a strong preference either way. It doesn't change anything for FSA-compliant applications, and I liked the simplicity of this version. I would have put meta
and payload
inline as well, but there was no good way to do that given the limitations of javascript syntax.
src/createAction.js
Outdated
const payload = finalPayloadCreator(...args); | ||
const action = { type, error: payload instanceof Error }; | ||
|
||
if (payload !== undefined) { |
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.
Wait what's the objection to using isUndefined
? Does it cost that much bytes that the readability isn't worth it? It's not much less tokens to parse, but still.
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.
It's not a readability thing; just avoiding the function call overhead. I figured that the purpose of isUndefined
is to be used as a predicate, like in filter
, but there's no reason to use it here, when the comparison is so simple. Compare to isFunction
, which has a much more complicated definition.
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.
Makes sense here, sense this is being called dynamically at runtime.
src/createAction.js
Outdated
|
||
if (isFunction(metaCreator)) { | ||
|
||
if (hasMeta) { |
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.
So this is where the savings is?
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.
Yes, especially given that isFunction
is surprisingly involved.
Obviously, all of this stuff is based on my personal preference. Originally I was only planning on doing the |
So, these are actually quite good reasons. The build is failing because of lint; can you get that to pass and I'll get this merged Thanks. |
@Lucretiel I fixed some lint issues in the PR. I would like to get this in, but some tests are failing. Could you fix? These should be reproducible locally with |
Yeah, mostly it has to do with the having |
Do you mind if I do a force push? Or did you make changes you feel strongly about? |
Looks good now. Can you take a look and tell me if this is OK with you? I'll merge and publish a patch version. |
Made one final change. Everything should be good now. |
Thanks 👍 . |
https://github.com/acdlite/redux-actions/releases/tag/v2.0.3
|
Slightly simplified createAction, and made it do more processing ahead of time that doesn't have to be done every time an action is emitted