regular-redux-undo
Version:
the plugin of regular-redux to archieve undo and redo
64 lines (53 loc) • 1.54 kB
JavaScript
import { forEachValue, isEmpty, error, typeOf} from '../util';
export default class Module {
constructor(rawModule, path) {
this._children = Object.create(null);
this._rawModule = rawModule;
this.path = path;
const state = rawModule.state || {};
this.state = typeOf(state) === 'function' ? state() : state;
}
addChild(key, module) {
this._children[key] = module
}
getChild(key) {
return this._children[key]
}
removeChild(key) {
delete this._children[key]
}
hasChild() {
return !isEmpty(this._children);
}
get reducers() {
return this._rawModule.reducers || {};
}
/**
* traverse the module including its submodule
* @param {Function} the callback to do something
*/
forEachModule(fn) {
fn(this);
forEachValue(this._children, (module) => {
module.forEachModule(fn);
})
}
/**
* check the module state, to see whether it is an object or not when it has submodule
* @param {String} moduleName the name of module
*/
checkState(moduleName = "the options of regular-redux's store") {
if (process.env.NODE_ENV !== 'production') {
error(this.hasChild() && typeOf(this.state) !== 'object', `module[${moduleName}] has submodules but It's state is not an object`)
forEachValue(this._children, (module, key) => {
module.checkState(key);
})
}
}
combineState() {
forEachValue(this._children, (module, key) => {
this.state[key] = module.combineState();
});
return this.state;
}
}