Skip to content
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

Merged
merged 2 commits into from
Oct 3, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
17 changes: 17 additions & 0 deletions modules/store/spec/reducer_creator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,23 @@ import {on} from './modules/store/src/reducer_creator';
state = fooBarReducer(state, bar({ bar: 54 }));
expect(state).toEqual(['[foobar] FOO', '[foobar] BAR']);
});

it('should support "on"s to have identical action types', () => {
const increase = createAction('[COUNTER] increase');

const counterReducer = createReducer(
0,
on(increase, state => state + 1),
on(increase, state => state + 1)
);

expect(typeof counterReducer).toEqual('function');

let state = 5;

state = counterReducer(state, increase());
expect(state).toEqual(7);
});
});
});
});
18 changes: 14 additions & 4 deletions modules/store/src/reducer_creator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,15 +229,25 @@ export function createReducer<S, A extends Action = Action>(
initialState: S,
...ons: On<S>[]
): ActionReducer<S, A> {
const map = new Map<string, ActionReducer<S, A>>();
const map = new Map<string, ActionReducer<S, A>[]>();
for (let on of ons) {
for (let type of on.types) {
map.set(type, on.reducer);
if (map.has(type)) {
Copy link
Member

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);
}

Copy link
Contributor Author

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. :)

const existingReducers = map.get(type) as ActionReducer<S, A>[];
map.set(type, [...existingReducers, on.reducer]);
} else {
map.set(type, [on.reducer]);
}
}
}

return function(state: S = initialState, action: A): S {
const reducer = map.get(action.type);
return reducer ? reducer(state, action) : state;
const reducers = map.get(action.type);
return reducers && reducers.length > 0
? reducers.reduce(
(previousState, reducer) => reducer(previousState, action),
state
)
: state;
};
}