Skip to content

Instantly share code, notes, and snippets.

@bloadvenro
Created February 11, 2019 23:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bloadvenro/3ac411dce00cf68bd6ed4c3b47e0e09f to your computer and use it in GitHub Desktop.
Save bloadvenro/3ac411dce00cf68bd6ed4c3b47e0e09f to your computer and use it in GitHub Desktop.
interface Dispatch<A extends { type: string }> {
(action: A): void;
}
interface StateGetter<S extends { [key: string]: any }> {
(): S;
}
interface ActionReducer<A extends { type: string }, S extends { [key: string]: any }> {
dispatch: Dispatch<A>;
getState: StateGetter<S>;
}
function ActionReducer<A extends { type: string }, S extends { [key: string]: any }>(
reducer: Reducer<S, A>,
initialState: S,
): ActionReducer<A, S> {
let state = initialState;
return {
dispatch(action) {
state = reducer(state, action);
},
getState: () => ({ ...state }),
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment