Skip to content

Instantly share code, notes, and snippets.

@Kolenov
Last active August 4, 2020 12:57
Show Gist options
  • Save Kolenov/d58a76943fc116c4f2cf5fe7f9c40ff8 to your computer and use it in GitHub Desktop.
Save Kolenov/d58a76943fc116c4f2cf5fe7f9c40ff8 to your computer and use it in GitHub Desktop.
export class ReducerPool {
constructor() {
this.redusers = [];
}
registerReducer(reducer) {
redusers.push(reducer);
}
getNewState(state, action) {
return reducers.reduce((state, reducer) => {
return reducer.getNewState(state, action);
}, state);
}
}
import { BaseReducer } from './BaseReducer'
import { ReducerPool } from './ReducerPool'
import { ADD_TODO } from './constants/TodoActionTypes'
export class AddTodoReducer extends BaseReducer {
getNewState(state, action) {
if (action.type === ADD_TODO) {
return [
{
id: state.reduce((maxID, todo) => Math.max(todo.id, maxID),
completed: false,
text: action.text
},
...state
]
}
}
}
ReducerPool.redister(new AddTodoReducer());
const ACTION_HANDLERS = {
[ACTION_1]: (state, action) => ...,
[ACTION_2]: (state, action) => ...,
[ACTION_3]: (state, action) => ...,
[ACTION_4]: (state, action) => ...,
...
};
const reducer = (state = INITIAL_STATE, action) => {
const handler = ACTION_HANDLERS[action.type];
return handler ? handler(state, action) : state;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment