placed the redux-utils goop in a different directory

This commit is contained in:
Ken
2019-01-31 13:34:19 -08:00
parent 6da255c206
commit 2ecb667b3a
4 changed files with 49 additions and 36 deletions

View File

@@ -1,12 +1,4 @@
import { Action } from 'redux';
type ActionWithPayload<T, P> = Action<T> & P;
function action<T extends string>(type: T): Action<T>;
function action<T extends string, P>(type: T, payload: P): ActionWithPayload<T, P>;
function action<T extends string, P>(type: T, payload?: P) {
return { type, ...payload };
}
import { action } from '../redux-utils/action';
export const actions = {
add: (label: string) => action('add', { label }),

View File

@@ -1,30 +1,9 @@
import { Reducer } from 'redux';
import { ActionTypes, TodoAction, TodoActionLookup } from '../actions';
import { Draft, produce } from 'immer';
import { ActionTypes, TodoActionLookup } from '../actions';
import { createGenericReducer, HandlerMap, ImmerReducer } from '../redux-utils/reducer';
export type ImmerReducer<T, A = any> = (state: Draft<T>, action?: A) => T;
export type HandlerMap<T> = { [actionType in ActionTypes]?: ImmerReducer<T, TodoActionLookup[actionType]> };
function isHandlerFunction<T>(handlerOrMap: HandlerMap<T> | ImmerReducer<T>): handlerOrMap is ImmerReducer<T> {
if (typeof handlerOrMap === 'function') {
return true;
}
return false;
}
export function createReducer<T, AType extends ActionTypes | never = never>(
export function createReducer<T, AT extends ActionTypes | never = never>(
initialState: T,
handlerOrMap: HandlerMap<T> | ImmerReducer<T, TodoActionLookup[AType]>
): Reducer<T> {
return function reducer(state = initialState, action: TodoAction | TodoActionLookup[AType]): T {
if (isHandlerFunction(handlerOrMap)) {
return produce(state, draft => handlerOrMap(draft, action as TodoActionLookup[AType]));
} else if (handlerOrMap.hasOwnProperty(action.type)) {
const handler = handlerOrMap[action.type] as ImmerReducer<T>;
return produce(state, draft => handler(draft, action));
} else {
return state;
}
};
handlerOrMap: HandlerMap<T, ActionTypes, TodoActionLookup> | ImmerReducer<T, TodoActionLookup[AT]>
) {
return createGenericReducer(initialState, handlerOrMap);
}

View File

@@ -0,0 +1,9 @@
import { Action } from 'redux';
type ActionWithPayload<T, P> = Action<T> & P;
export function action<T extends string>(type: T): Action<T>;
export function action<T extends string, P>(type: T, payload: P): ActionWithPayload<T, P>;
export function action<T extends string, P>(type: T, payload?: P) {
return { type, ...payload };
}

View 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;
}
};
}