36 lines
911 B
JavaScript
36 lines
911 B
JavaScript
|
import R from 'remeda';
|
||
|
import u from 'updeep';
|
||
|
|
||
|
const localMutation = (mutations) => (action) => (state) => {
|
||
|
const mutation = mutations[action.type];
|
||
|
|
||
|
if (!mutation) return state;
|
||
|
|
||
|
return mutation(action.payload, action)(state);
|
||
|
};
|
||
|
|
||
|
const subMutations = (subduxes) => (action) => (state) => {
|
||
|
const subReducers =
|
||
|
Object.keys(subduxes).length > 0
|
||
|
? R.mapValues(subduxes, R.prop('upreducer'))
|
||
|
: null;
|
||
|
|
||
|
if (!subReducers) return state;
|
||
|
|
||
|
if (subReducers['*']) {
|
||
|
return u.updateIn('*', subReducers['*'](action), state);
|
||
|
}
|
||
|
|
||
|
const update = R.mapValues(subReducers, (upReducer) => upReducer(action));
|
||
|
|
||
|
return u(update, state);
|
||
|
};
|
||
|
|
||
|
export function buildUpreducer(mutations, subduxes) {
|
||
|
return (action) => (state) => {
|
||
|
state = subMutations(subduxes)(action)(state);
|
||
|
|
||
|
return localMutation(mutations)(action)(state);
|
||
|
};
|
||
|
}
|