yax
Version:
Yet another store using redux. (Inspired by vuex and dva)
97 lines (74 loc) • 2.08 kB
JavaScript
import _createClass from "@babel/runtime/helpers/createClass";
import { forEachValue, mapReducers } from '../util';
var Module =
/*#__PURE__*/
function () {
function Module(rawModule) {
this._children = Object.create(null);
this._rawModule = rawModule;
this._reducers = {};
}
var _proto = Module.prototype;
_proto.addChild = function addChild(key, module) {
this._children[key] = module;
};
_proto.removeChild = function removeChild(key) {
delete this._children[key];
};
_proto.getChild = function getChild(key) {
return this._children[key];
};
_proto.forEachChild = function forEachChild(fn) {
forEachValue(this._children, fn);
};
_proto.forEachAction = function forEachAction(fn) {
if (this._rawModule.actions) {
forEachValue(this._rawModule.actions, fn);
}
};
_proto.forEachReducer = function forEachReducer(fn) {
if (this._rawModule.reducers) {
forEachValue(this._rawModule.reducers, fn);
}
};
_proto.cleanReducers = function cleanReducers() {
this._reducers = {};
};
_proto.addReducer = function addReducer(actionType, handler) {
this._reducers[actionType] = handler;
};
_proto.makeReducers = function makeReducers() {
var _this = this;
return function (state, action) {
if (state === void 0) {
state = _this.state;
}
var type = action.type,
payload = action.payload;
var handler = _this._reducers[type];
if (handler) {
return handler(state, payload);
}
if (Object.keys(_this._children).length) {
var tmp = {};
_this.forEachChild(function (child, key) {
tmp[key] = child.makeReducers();
});
return mapReducers(tmp)(state, action);
}
return state;
};
};
_createClass(Module, [{
key: "state",
get: function get() {
var state = this._rawModule.state;
if (state === undefined) {
return {};
}
return state;
}
}]);
return Module;
}();
export { Module as default };