reflux-core
Version:
A simple library for uni-directional dataflow application architecture inspired by ReactJS Flux
68 lines (60 loc) • 2.11 kB
JavaScript
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.mix = mix;
var _utils = require("./utils");
var _ = _interopRequireWildcard(_utils);
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
function mix(def) {
var composed = {
init: [],
preEmit: [],
shouldEmit: []
};
var updated = function mixDef(mixin) {
var mixed = {};
if (mixin.mixins) {
mixin.mixins.forEach(function (subMixin) {
_.extend(mixed, mixDef(subMixin));
});
}
_.extend(mixed, mixin);
Object.keys(composed).forEach(function (composable) {
if (mixin.hasOwnProperty(composable)) {
composed[composable].push(mixin[composable]);
}
});
return mixed;
}(def);
if (composed.init.length > 1) {
updated.init = function () {
var args = arguments;
composed.init.forEach(function (init) {
init.apply(this, args);
}, this);
};
}
if (composed.preEmit.length > 1) {
updated.preEmit = function () {
return composed.preEmit.reduce(function (args, preEmit) {
var newValue = preEmit.apply(this, args);
return newValue === undefined ? args : [newValue];
}.bind(this), arguments);
};
}
if (composed.shouldEmit.length > 1) {
updated.shouldEmit = function () {
var args = arguments;
return !composed.shouldEmit.some(function (shouldEmit) {
return !shouldEmit.apply(this, args);
}, this);
};
}
Object.keys(composed).forEach(function (composable) {
if (composed[composable].length === 1) {
updated[composable] = composed[composable][0];
}
});
return updated;
}
;