|
@@ -1,17 +1,10 @@
|
|
|
import Module from './module'
|
|
|
-import { forEachValue } from '../util'
|
|
|
+import { assert, forEachValue } from '../util'
|
|
|
|
|
|
export default class ModuleCollection {
|
|
|
constructor (rawRootModule) {
|
|
|
// register root module (Vuex.Store options)
|
|
|
- this.root = new Module(rawRootModule, false)
|
|
|
-
|
|
|
- // register all nested modules
|
|
|
- if (rawRootModule.modules) {
|
|
|
- forEachValue(rawRootModule.modules, (rawModule, key) => {
|
|
|
- this.register([key], rawModule, false)
|
|
|
- })
|
|
|
- }
|
|
|
+ this.register([], rawRootModule, false)
|
|
|
}
|
|
|
|
|
|
get (path) {
|
|
@@ -29,13 +22,19 @@ export default class ModuleCollection {
|
|
|
}
|
|
|
|
|
|
update (rawRootModule) {
|
|
|
- update(this.root, rawRootModule)
|
|
|
+ update([], this.root, rawRootModule)
|
|
|
}
|
|
|
|
|
|
register (path, rawModule, runtime = true) {
|
|
|
- const parent = this.get(path.slice(0, -1))
|
|
|
+ assertRawModule(path, rawModule)
|
|
|
+
|
|
|
const newModule = new Module(rawModule, runtime)
|
|
|
- parent.addChild(path[path.length - 1], newModule)
|
|
|
+ if (path.length === 0) {
|
|
|
+ this.root = newModule
|
|
|
+ } else {
|
|
|
+ const parent = this.get(path.slice(0, -1))
|
|
|
+ parent.addChild(path[path.length - 1], newModule)
|
|
|
+ }
|
|
|
|
|
|
// register nested modules
|
|
|
if (rawModule.modules) {
|
|
@@ -54,7 +53,9 @@ export default class ModuleCollection {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-function update (targetModule, newModule) {
|
|
|
+function update (path, targetModule, newModule) {
|
|
|
+ assertRawModule(path, newModule)
|
|
|
+
|
|
|
// update target module
|
|
|
targetModule.update(newModule)
|
|
|
|
|
@@ -68,7 +69,34 @@ function update (targetModule, newModule) {
|
|
|
)
|
|
|
return
|
|
|
}
|
|
|
- update(targetModule.getChild(key), newModule.modules[key])
|
|
|
+ update(
|
|
|
+ path.concat(key),
|
|
|
+ targetModule.getChild(key),
|
|
|
+ newModule.modules[key]
|
|
|
+ )
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+function assertRawModule (path, rawModule) {
|
|
|
+ ['getters', 'actions', 'mutations'].forEach(key => {
|
|
|
+ if (!rawModule[key]) return
|
|
|
+
|
|
|
+ forEachValue(rawModule[key], (value, type) => {
|
|
|
+ assert(
|
|
|
+ typeof value === 'function',
|
|
|
+ makeAssertionMessage(path, key, type, value)
|
|
|
+ )
|
|
|
+ })
|
|
|
+ })
|
|
|
+}
|
|
|
+
|
|
|
+function makeAssertionMessage (path, key, type, value) {
|
|
|
+ let buf = `${key} should be function but "${key}.${type}"`
|
|
|
+ if (path.length > 0) {
|
|
|
+ buf += ` in module "${path.join('.')}"`
|
|
|
+ }
|
|
|
+ buf += ` is ${JSON.stringify(value)}.`
|
|
|
+
|
|
|
+ return buf
|
|
|
+}
|