Skip to content

Instantly share code, notes, and snippets.

@mobinni
Last active January 27, 2016 15:12
Show Gist options
  • Save mobinni/0953f7d6e4182b9039c1 to your computer and use it in GitHub Desktop.
Save mobinni/0953f7d6e4182b9039c1 to your computer and use it in GitHub Desktop.
A Modern Isomorphic Stack with Redux - Part 2
class ModuleCollector {
constructor(config) {
this.actions = [];
this.reducers = [];
this.modules = {};
this.config = config;
}
add(newModule) {
if (typeof newModule === 'function') {
newModule = newModule(this.config);
}
if (!newModule) {
throw new Error('Check if your module is an object or is returning an object');
}
if (newModule.name) {
if (this.modules.hasOwnProperty(newModule.name)) {
throw new Error(`Module with name '${newModule.name}' was already registered.`);
}
this.modules[newModule.name] = newModule;
}
if (newModule.reducers) {
this.reducers.push(newModule.reducers);
}
if (newModule.actions) {
this.actions.push(newModule.actions);
}
}
get(moduleName) {
if (typeof moduleName === 'string') {
return this.modules[moduleName];
}
return this.modules;
}
getReducers() {
return Object.assign({}, ...this.reducers);
}
}
export default ModuleCollector;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment