buildMutations => ts
This commit is contained in:
parent
4754fb2377
commit
125800a8b9
@ -1,53 +0,0 @@
|
||||
import fp from 'lodash/fp';
|
||||
import u from 'updeep';
|
||||
|
||||
const composeMutations = (mutations) =>
|
||||
mutations.reduce( (m1,m2) =>
|
||||
(payload=null,action={}) => state => m2(payload,action)(
|
||||
m1(payload,action)(state) ));
|
||||
|
||||
export default function buildMutations(mutations = {}, subduxes= {}) {
|
||||
// we have to differentiate the subduxes with '*' than those
|
||||
// without, as the root '*' is not the same as any sub-'*'
|
||||
|
||||
const actions = fp.uniq( Object.keys(mutations).concat(
|
||||
...Object.values( subduxes ).map( ({mutations = {}}) => Object.keys(mutations) )
|
||||
) );
|
||||
|
||||
let mergedMutations = {};
|
||||
|
||||
let [ globby, nonGlobby ] = fp.partition(
|
||||
([_,{mutations={}}]) => mutations['*'],
|
||||
Object.entries(subduxes)
|
||||
);
|
||||
|
||||
globby =
|
||||
fp.flow([
|
||||
fp.fromPairs,
|
||||
fp.mapValues(
|
||||
({reducer}) => (_,action={}) => state =>
|
||||
reducer(state,action) ),
|
||||
])(globby);
|
||||
|
||||
const globbyMutation = (payload,action) => u(
|
||||
fp.mapValues( (mut) => mut(payload,action) )(globby)
|
||||
);
|
||||
|
||||
actions.forEach( action => {
|
||||
mergedMutations[action] = [ globbyMutation ]
|
||||
});
|
||||
|
||||
nonGlobby.forEach( ([slice, {mutations={},reducer={}}]) => {
|
||||
Object.entries(mutations).forEach(([type,mutation]) => {
|
||||
const localized = (payload=null,action={}) => u.updateIn( slice )( (mutation)(payload,action) );
|
||||
|
||||
mergedMutations[type].push(localized);
|
||||
})
|
||||
});
|
||||
|
||||
Object.entries(mutations).forEach(([type,mutation]) => {
|
||||
mergedMutations[type].push(mutation);
|
||||
});
|
||||
|
||||
return fp.mapValues( composeMutations )(mergedMutations);
|
||||
}
|
66
src/buildMutations/index.ts
Normal file
66
src/buildMutations/index.ts
Normal file
@ -0,0 +1,66 @@
|
||||
import fp from 'lodash/fp';
|
||||
import u from 'updeep';
|
||||
import {Mutation, Action, Dictionary} from '../types';
|
||||
|
||||
const composeMutations = (mutations: Mutation[]) =>
|
||||
mutations.reduce((m1, m2) => (payload: any = null, action: Action) => state =>
|
||||
m2(payload, action)(m1(payload, action)(state)),
|
||||
);
|
||||
|
||||
type SubMutations = {
|
||||
[ slice: string ]: Dictionary<Mutation>
|
||||
}
|
||||
|
||||
function buildMutations(
|
||||
mutations :Dictionary<Mutation> = {},
|
||||
subduxes = {}
|
||||
) {
|
||||
// we have to differentiate the subduxes with '*' than those
|
||||
// without, as the root '*' is not the same as any sub-'*'
|
||||
|
||||
const actions = fp.uniq(
|
||||
Object.keys(mutations).concat(
|
||||
...Object.values(subduxes).map(({mutations = {}}:any) =>
|
||||
Object.keys(mutations),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
let mergedMutations :Dictionary<Mutation[]> = {};
|
||||
|
||||
let [globby, nonGlobby] = fp.partition(
|
||||
([_, {mutations = {}}]:any) => mutations['*'],
|
||||
Object.entries(subduxes),
|
||||
);
|
||||
|
||||
globby = fp.flow([
|
||||
fp.fromPairs,
|
||||
fp.mapValues(({reducer}) => (_:any, action :Action) => ( state: any ) =>
|
||||
reducer(state, action),
|
||||
),
|
||||
])(globby);
|
||||
|
||||
const globbyMutation = (payload:any, action:Action) =>
|
||||
u(fp.mapValues((mut:any) => mut(payload, action))(globby));
|
||||
|
||||
actions.forEach(action => {
|
||||
mergedMutations[action] = [globbyMutation];
|
||||
});
|
||||
|
||||
nonGlobby.forEach(([slice, {mutations = {}, reducer = {}}]:any[]) => {
|
||||
Object.entries(mutations).forEach(([type, mutation]) => {
|
||||
const localized = (payload = null, action :Action) =>
|
||||
u.updateIn(slice)((mutation as Mutation)(payload, action));
|
||||
|
||||
mergedMutations[type].push(localized);
|
||||
});
|
||||
});
|
||||
|
||||
Object.entries(mutations).forEach(([type, mutation]) => {
|
||||
mergedMutations[type].push(mutation);
|
||||
});
|
||||
|
||||
return fp.mapValues(composeMutations)(mergedMutations);
|
||||
}
|
||||
|
||||
export default buildMutations;
|
@ -7,7 +7,7 @@ export type Action = {
|
||||
|
||||
export type Dictionary<T> = { [key: string]: T };
|
||||
|
||||
export type Mutation<S> = (payload: any, action: Action) => (state: S) => S ;
|
||||
export type Mutation<S=any> = (payload: any, action: Action) => (state: S) => S ;
|
||||
|
||||
export type ActionCreator = (...args: any[] ) => Action;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user