checking in docs for github.io page

This commit is contained in:
Ken
2019-02-19 23:41:11 -08:00
parent e88ba9c448
commit 164db9dd93
194 changed files with 103939 additions and 5 deletions

View File

@@ -0,0 +1,14 @@
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 };
}
export type GenericActionMapping<A> = { [somekey in keyof A]: (...args: any) => Action<any> | ActionWithPayload<any, any> };
export type GenericActionTypes<A extends GenericActionMapping<A>> = ReturnType<A[keyof A]>['type'];
export type GenericAction<A extends GenericActionMapping<A>> = ReturnType<A[GenericActionTypes<A>]>;
export type GenericActionLookup<A extends GenericActionMapping<A>> = { [a in GenericActionTypes<A>]: ReturnType<A[a]> };

View File

@@ -0,0 +1,34 @@
import { Reducer } from 'redux';
import { Draft, produce } from 'immer';
import { GenericActionLookup, GenericActionMapping } from './action';
export type ImmerReducer<T, A> = (state: Draft<T>, action?: A) => T;
export type HandlerMap<T, A extends GenericActionMapping<A>> = {
[actionType in keyof A]?: ImmerReducer<T, GenericActionLookup<A>[actionType]>
};
function isHandlerFunction<T, A extends GenericActionMapping<A>>(
handlerOrMap: HandlerMap<T, A> | ImmerReducer<T, A>
): handlerOrMap is ImmerReducer<T, A> {
if (typeof handlerOrMap === 'function') {
return true;
}
return false;
}
export function createGenericReducer<T, A extends GenericActionMapping<A>, AM = keyof GenericActionMapping<A>>(
initialState: T,
handlerOrMap: HandlerMap<T, A> | ImmerReducer<T, GenericActionLookup<A>[AM]>
): Reducer<T> {
return function reducer(state = initialState, action: GenericActionLookup<A>[AM]): T {
if (isHandlerFunction(handlerOrMap)) {
return produce(state, draft => handlerOrMap(draft, action as GenericActionLookup<A>[AM]));
} else if (handlerOrMap.hasOwnProperty(action.type)) {
const handler = (handlerOrMap as any)[action.type] as ImmerReducer<T, GenericActionLookup<A>[AM]>;
return produce(state, draft => handler(draft, action));
} else {
return state;
}
};
}