Last active
January 27, 2016 15:12
-
-
Save mobinni/0953f7d6e4182b9039c1 to your computer and use it in GitHub Desktop.
A Modern Isomorphic Stack with Redux - Part 2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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