UNPKG

yax

Version:

Yet another store using redux. (Inspired by vuex and dva)

104 lines (78 loc) 2.3 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); exports.__esModule = true; exports["default"] = void 0; var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass")); var _util = require("../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) { (0, _util.forEachValue)(this._children, fn); }; _proto.forEachAction = function forEachAction(fn) { if (this._rawModule.actions) { (0, _util.forEachValue)(this._rawModule.actions, fn); } }; _proto.forEachReducer = function forEachReducer(fn) { if (this._rawModule.reducers) { (0, _util.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 (0, _util.mapReducers)(tmp)(state, action); } return state; }; }; (0, _createClass2["default"])(Module, [{ key: "state", get: function get() { var state = this._rawModule.state; if (state === undefined) { return {}; } return state; } }]); return Module; }(); exports["default"] = Module;