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:
@@ -1,12 +1,4 @@
|
|||||||
import { Action } from 'redux';
|
import { action } from '../redux-utils/action';
|
||||||
|
|
||||||
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 };
|
|
||||||
}
|
|
||||||
|
|
||||||
export const actions = {
|
export const actions = {
|
||||||
add: (label: string) => action('add', { label }),
|
add: (label: string) => action('add', { label }),
|
||||||
|
|||||||
@@ -1,30 +1,9 @@
|
|||||||
import { Reducer } from 'redux';
|
import { ActionTypes, TodoActionLookup } from '../actions';
|
||||||
import { ActionTypes, TodoAction, TodoActionLookup } from '../actions';
|
import { createGenericReducer, HandlerMap, ImmerReducer } from '../redux-utils/reducer';
|
||||||
import { Draft, produce } from 'immer';
|
|
||||||
|
|
||||||
export type ImmerReducer<T, A = any> = (state: Draft<T>, action?: A) => T;
|
export function createReducer<T, AT extends ActionTypes | never = never>(
|
||||||
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>(
|
|
||||||
initialState: T,
|
initialState: T,
|
||||||
handlerOrMap: HandlerMap<T> | ImmerReducer<T, TodoActionLookup[AType]>
|
handlerOrMap: HandlerMap<T, ActionTypes, TodoActionLookup> | ImmerReducer<T, TodoActionLookup[AT]>
|
||||||
): Reducer<T> {
|
) {
|
||||||
return function reducer(state = initialState, action: TodoAction | TodoActionLookup[AType]): T {
|
return createGenericReducer(initialState, handlerOrMap);
|
||||||
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;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|||||||
9
playground/src/redux-utils/action.ts
Normal file
9
playground/src/redux-utils/action.ts
Normal 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 };
|
||||||
|
}
|
||||||
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