-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
fix/feat(store): createReducer: on should not be overwritten #2103
fix/feat(store): createReducer: on should not be overwritten #2103
Conversation
I have added an example of the necessity of this behaviour here (#1956 (comment)) |
Preview docs changes for e8aebae at https://previews.ngrx.io/pr2103-e8aebae/ |
createReducer should allow ons to take actions of same type and excute all ons to match the action dispatched
e8aebae
to
dcc72ee
Compare
Preview docs changes for dcc72ee at https://previews.ngrx.io/pr2103-dcc72ee/ |
I'm not sure if this is a "bug". |
Yeah, it could be either feature or bug. In my opinion, the Some usage case for justifying this feature could be:
However, I can't think of any case that need to overwrite previous |
for (let on of ons) { | ||
for (let type of on.types) { | ||
map.set(type, on.reducer); | ||
if (map.has(type)) { |
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.
You could do the composition on the fly here instead of storing arrays for a small perf bump in the generated reducer:
if (map.has(type)) {
const reducer = map.get(type) as ActionReducer<S, A>;
map.set(type, (state, action) => on.reducer(reducer(state, action), action);
}
else {
map.set(type, on.reducer);
}
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.
Thanks @MikeRyanDev for the great suggestion. I have pushed a commit. :)
685cb32
to
8b31906
Compare
8b31906
to
a32c101
Compare
Preview docs changes for 685cb32 at https://previews.ngrx.io/pr2103-685cb32/ |
Preview docs changes for a32c101 at https://previews.ngrx.io/pr2103-a32c101/ |
@timdeschryver you have anymore thoughts on this? |
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 looks good for me @brandonroberts
I believe we should follow-up with some docs changes, if I remember correctly it mentions that the on
will be overwritten.
createReducer should allow ons to take actions of same type and excute all ons to match the action dispatched
PR Checklist
Please check if your PR fulfills the following requirements:
PR Type
What kind of change does this PR introduce?
What is the current behavior?
If the following is configured and initial state is 0, if
increase
action is dispatched, the next state is 2, because the the secondon
has same action type as the firston
, and the firston
is overwritten.(Indirectly) Closes #1956
What is the new behavior?
In
createReducer
,on
should not overwrite previouson
s if they have the same action types.Does this PR introduce a breaking change?