Skip to content

Instantly share code, notes, and snippets.

@ghetolay
Created October 6, 2017 14:00
Show Gist options
  • Save ghetolay/5c3dea9e36ac553a1974509d3bfb722b to your computer and use it in GitHub Desktop.
Save ghetolay/5c3dea9e36ac553a1974509d3bfb722b to your computer and use it in GitHub Desktop.
Ngrx reducer as map
const IncrementAction = ...;
const DecrementAction = ...;
const reducer1 = {
[]: () => 0,
[IncrementAction.type]: (state, action) => state + 1,
[DecrementAction.type]: (state, action) => state - 1
};
const reducers: { [type: string]: ReduceFn[] } = merge(reducer1, reducer2, ...);
function dispatch(action: Action){
const newState = reducers[action]?.reduce( (state, reducer) => reducer(state, action), currentState );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment