buildMutations => ts

This commit is contained in:
Yanick Champoux 2019-10-23 17:01:48 -04:00
parent 4754fb2377
commit 125800a8b9
3 changed files with 67 additions and 54 deletions

View File

@ -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);
}

View 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;

View File

@ -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;