mirror of
https://github.com/microsoft/frontend-bootcamp.git
synced 2026-01-26 14:56:42 +08:00
placed the redux-utils goop in a different directory
This commit is contained in:
33
playground/src/redux-utils/reducer.ts
Normal file
33
playground/src/redux-utils/reducer.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
import { Reducer } from 'redux';
|
||||
import { Draft, produce } from 'immer';
|
||||
|
||||
export type ImmerReducer<T, A> = (state: Draft<T>, action?: A) => T;
|
||||
export type HandlerMap<T, AT extends string, Lookup extends { [t in AT]: any }> = {
|
||||
[actionType in AT]?: ImmerReducer<T, Lookup[actionType]>
|
||||
};
|
||||
|
||||
function isHandlerFunction<T, A, AT extends string, Lookup extends { [t in AT]: any }>(
|
||||
handlerOrMap: HandlerMap<T, AT, Lookup> | ImmerReducer<T, A>
|
||||
): handlerOrMap is ImmerReducer<T, A> {
|
||||
if (typeof handlerOrMap === 'function') {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
export function createGenericReducer<T, AT extends string, Lookup extends { [t in AT]: any }>(
|
||||
initialState: T,
|
||||
handlerOrMap: HandlerMap<T, AT, Lookup> | ImmerReducer<T, Lookup[AT]>
|
||||
): Reducer<T> {
|
||||
return function reducer(state = initialState, action: Lookup[AT]): T {
|
||||
if (isHandlerFunction(handlerOrMap)) {
|
||||
return produce(state, draft => handlerOrMap(draft, action as Lookup[AT]));
|
||||
} else if (handlerOrMap.hasOwnProperty(action.type)) {
|
||||
const handler = (handlerOrMap as any)[action.type] as ImmerReducer<T, Lookup[AT]>;
|
||||
return produce(state, draft => handler(draft, action));
|
||||
} else {
|
||||
return state;
|
||||
}
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user