buildUpreducer
This commit is contained in:
parent
11ff89501d
commit
f092526ded
@ -1,15 +0,0 @@
|
|||||||
import fp from 'lodash/fp';
|
|
||||||
|
|
||||||
export default function buildUpreducer(initial, mutations) {
|
|
||||||
return (action = {}) => (state) => {
|
|
||||||
if (state === null) state = initial;
|
|
||||||
|
|
||||||
const a =
|
|
||||||
mutations[(action).type] ||
|
|
||||||
mutations['*'];
|
|
||||||
|
|
||||||
if(!a) return state;
|
|
||||||
|
|
||||||
return a((action).payload, action)(state);
|
|
||||||
};
|
|
||||||
}
|
|
19
src/buildUpreducer/index.ts
Normal file
19
src/buildUpreducer/index.ts
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
import fp from 'lodash/fp';
|
||||||
|
|
||||||
|
import { Dictionary, Mutation, UpduxAction } from '../types';
|
||||||
|
|
||||||
|
function buildUpreducer<S>(initial: S, mutations: Dictionary<Mutation<S>> ) {
|
||||||
|
return (action :UpduxAction) => (state: S) => {
|
||||||
|
if (state === null) state = initial;
|
||||||
|
|
||||||
|
const a =
|
||||||
|
mutations[action.type] ||
|
||||||
|
mutations['*'];
|
||||||
|
|
||||||
|
if(!a) return state;
|
||||||
|
|
||||||
|
return a(action.payload, action)(state);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export default buildUpreducer;
|
10
src/types.ts
Normal file
10
src/types.ts
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
import { Action } from 'redux';
|
||||||
|
|
||||||
|
export type UpduxAction = Action & Partial<{
|
||||||
|
payload: any,
|
||||||
|
meta: any,
|
||||||
|
}>
|
||||||
|
|
||||||
|
export type Dictionary<T> = { [key: string]: T };
|
||||||
|
|
||||||
|
export type Mutation<S> = (payload: any, action: Action) => (state: S) => S ;
|
Loading…
Reference in New Issue
Block a user