updux/dist/buildMutations/index.js

33 lines
1.8 KiB
JavaScript
Raw Normal View History

2019-10-22 22:15:08 +00:00
"use strict";
2019-10-24 15:52:36 +00:00
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const fp_1 = __importDefault(require("lodash/fp"));
const updeep_1 = __importDefault(require("updeep"));
const composeMutations = (mutations) => mutations.reduce((m1, m2) => (payload = null, action) => state => m2(payload, action)(m1(payload, action)(state)));
2019-10-22 22:15:08 +00:00
function buildMutations(mutations = {}, subduxes = {}) {
2019-10-24 15:52:36 +00:00
const actions = fp_1.default.uniq(Object.keys(mutations).concat(...Object.values(subduxes).map(({ mutations = {} }) => Object.keys(mutations))));
let mergedMutations = {};
let [globby, nonGlobby] = fp_1.default.partition(([_, { mutations = {} }]) => mutations['*'], Object.entries(subduxes));
globby = fp_1.default.flow([
fp_1.default.fromPairs,
fp_1.default.mapValues(({ reducer }) => (_, action) => (state) => reducer(state, action)),
])(globby);
const globbyMutation = (payload, action) => updeep_1.default(fp_1.default.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) => updeep_1.default.updateIn(slice)(mutation(payload, action));
mergedMutations[type].push(localized);
});
});
2019-10-22 22:15:08 +00:00
Object.entries(mutations).forEach(([type, mutation]) => {
2019-10-24 15:52:36 +00:00
mergedMutations[type].push(mutation);
2019-10-22 22:15:08 +00:00
});
2019-10-24 15:52:36 +00:00
return fp_1.default.mapValues(composeMutations)(mergedMutations);
}
exports.default = buildMutations;
//# sourceMappingURL=index.js.map