From 2ecb667b3abd1ff5619c92feddd3473f7c285956 Mon Sep 17 00:00:00 2001 From: Ken Date: Thu, 31 Jan 2019 13:34:19 -0800 Subject: [PATCH] placed the redux-utils goop in a different directory --- playground/src/actions/index.ts | 10 +------ playground/src/reducers/createReducer.ts | 33 +++++------------------- playground/src/redux-utils/action.ts | 9 +++++++ playground/src/redux-utils/reducer.ts | 33 ++++++++++++++++++++++++ 4 files changed, 49 insertions(+), 36 deletions(-) create mode 100644 playground/src/redux-utils/action.ts create mode 100644 playground/src/redux-utils/reducer.ts diff --git a/playground/src/actions/index.ts b/playground/src/actions/index.ts index 50b9d3e..de4eb75 100644 --- a/playground/src/actions/index.ts +++ b/playground/src/actions/index.ts @@ -1,12 +1,4 @@ -import { Action } from 'redux'; - -type ActionWithPayload = Action & P; - -function action(type: T): Action; -function action(type: T, payload: P): ActionWithPayload; -function action(type: T, payload?: P) { - return { type, ...payload }; -} +import { action } from '../redux-utils/action'; export const actions = { add: (label: string) => action('add', { label }), diff --git a/playground/src/reducers/createReducer.ts b/playground/src/reducers/createReducer.ts index f2bcb3e..eef8a14 100644 --- a/playground/src/reducers/createReducer.ts +++ b/playground/src/reducers/createReducer.ts @@ -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 = (state: Draft, action?: A) => T; -export type HandlerMap = { [actionType in ActionTypes]?: ImmerReducer }; - -function isHandlerFunction(handlerOrMap: HandlerMap | ImmerReducer): handlerOrMap is ImmerReducer { - if (typeof handlerOrMap === 'function') { - return true; - } - - return false; -} - -export function createReducer( +export function createReducer( initialState: T, - handlerOrMap: HandlerMap | ImmerReducer -): Reducer { - 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; - return produce(state, draft => handler(draft, action)); - } else { - return state; - } - }; + handlerOrMap: HandlerMap | ImmerReducer +) { + return createGenericReducer(initialState, handlerOrMap); } diff --git a/playground/src/redux-utils/action.ts b/playground/src/redux-utils/action.ts new file mode 100644 index 0000000..fd067af --- /dev/null +++ b/playground/src/redux-utils/action.ts @@ -0,0 +1,9 @@ +import { Action } from 'redux'; + +type ActionWithPayload = Action & P; + +export function action(type: T): Action; +export function action(type: T, payload: P): ActionWithPayload; +export function action(type: T, payload?: P) { + return { type, ...payload }; +} diff --git a/playground/src/redux-utils/reducer.ts b/playground/src/redux-utils/reducer.ts new file mode 100644 index 0000000..be59494 --- /dev/null +++ b/playground/src/redux-utils/reducer.ts @@ -0,0 +1,33 @@ +import { Reducer } from 'redux'; +import { Draft, produce } from 'immer'; + +export type ImmerReducer = (state: Draft, action?: A) => T; +export type HandlerMap = { + [actionType in AT]?: ImmerReducer +}; + +function isHandlerFunction( + handlerOrMap: HandlerMap | ImmerReducer +): handlerOrMap is ImmerReducer { + if (typeof handlerOrMap === 'function') { + return true; + } + + return false; +} + +export function createGenericReducer( + initialState: T, + handlerOrMap: HandlerMap | ImmerReducer +): Reducer { + 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; + return produce(state, draft => handler(draft, action)); + } else { + return state; + } + }; +}