Skip to content

Instantly share code, notes, and snippets.

@captain-yossarian
Last active June 13, 2019 16:28
Show Gist options
  • Save captain-yossarian/07e955663a8fcb702cb76f14bc04f88d to your computer and use it in GitHub Desktop.
Save captain-yossarian/07e955663a8fcb702cb76f14bc04f88d to your computer and use it in GitHub Desktop.
Reducer
import { ActionPayload, AllActionsPayload, Constants, IActionMap } from './actionFactory';
type ReducerCase<P> = (state: IStore, payload: P) => IStore;
type IReducer = { [P in keyof Record<Constants, ReducerCase<AllActionsPayload>>]: ReducerCase<IActionMap[P]> };
export const reducer: IReducer = {
ACTION_NAMESPACE: (state, payload) => {
const { id, mode } = payload;
return state
},
};
export const communicatorReducer = (reducers: Record<Constants, ReducerCase<AllActionsPayload>>) => (
state = initialState,
{ type, payload }: ActionPayload<Constants, AllActionsPayload>
): IStore => (reducers[type] ? reducers[type](state, payload) : state);
export default communicatorReducer(reducer);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment