8000 tweak initialization logic · poorprogrammer/vuejs.org@777b2b1 · GitHub
[go: up one dir, main page]

Skip to content

Commit 777b2b1

Browse files
committed
tweak initialization logic
1 parent ce60bc7 commit 777b2b1

File tree

1 file changed

+53
-55
lines changed

1 file changed

+53
-55
lines changed

src/index.js

Lines changed: 53 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,14 @@ class Store {
3737
// strict mode
3838
this.strict = strict
3939

40-
// init internal vm with root state
41-
// other options and sub modules will be
42-
// initialized in this.module method
43-
initStoreVM(this, state, {})
40+
// init root module.
41+
// this also recursively registers all sub-modules
42+
// and collects all module getters inside this._wrappedGetters
43+
initModule(this, state, [], options)
4444

45-
// apply root module
46-
this.registerModule([], options)
45+
// initialize the store vm, which is responsible for the reactivity
46+
// (also registers _wrappedGetters as computed properties)
47+
initStoreVM(this, state, this._wrappedGetters)
4748

4849
// apply plugins
4950
plugins.concat(devtoolPlugin).forEach(plugin => plugin(this))
@@ -63,52 +64,6 @@ class Store {
6364
this._committing = false
6465
}
6566

66-
registerModule (path, module, hot) {
67-
this._committing = true
68-
if (typeof path === 'string') path = [path]
69-
assert(Array.isArray(path), `module path must be a string or an Array.`)
70-
71-
initModule(this, path, module, hot)
72-
73-
initStoreVM(this, this.state, this._wrappedGetters)
74-
75-
this._committing = false
76-
}
77-
78-
registerMutation (type, handler, path = []) {
79-
const entry = this._mutations[type] || (this._mutations[type] = [])
80-
const store = this
81-
entry.push(function wrappedMutationHandler (payload) {
82-
handler(getNestedState(store.state, path), payload)
83-
})
84-
}
85-
86-
registerAction (type, handler, path = []) {
87-
const entry = this._actions[type] || (this._actions[type] = [])
88-
const store = this
89-
const { dispatch, commit } = this
90-
entry.push(function wrappedActionHandler (payload, cb) {
91-
let res = handler({
92-
dispatch,
93-
commit,
94-
getters: store.getters,
95-
state: getNestedState(store.state, path),
96-
rootState: store.state
97-
}, payload, cb)
98-
if (!isPromise(res)) {
99-
res = Promise.resolve(res)
100-
}
101-
if (store._devtoolHook) {
102-
return res.catch(err => {
103-
store._devtoolHook.emit('vuex:error', err)
104-
throw err
105-
})
106-
} else {
107-
return res
108-
}
109-
})
110-
}
111-
11267
commit (type, payload) {
11368
// check object-style commit
11469
let mutation
@@ -172,6 +127,49 @@ class Store {
172127
return this._vm.$watch(() => getter(this.state), cb, options)
173128
}
174129

130+
registerModule (path, module, hot) {
131+
this._committing = true
132+
if (typeof path === 'string') path = [path]
133+
assert(Array.isArray(path), `module path must be a string or an Array.`)
134+
initModule(this, this.state, path, module, hot)
135+
initStoreVM(this, this.state, this._wrappedGetters)
136+
this._committing = false
137+
}
138+
139+
registerMutation (type, handler, path = []) {
140+
const entry = this._mutations[type] || (this._mutations[type] = [])
141+
const store = this
142+
entry.push(function wrappedMutationHandler (payload) {
143+
handler(getNestedState(store.state, path), payload)
144+
})
145+
}
146+
147+
registerAction (type, handler, path = []) {
148+
const entry = this._actions[type] || (this._actions[type] = [])
149+
const store = this
150+
const { dispatch, commit } = this
151+
entry.push(function wrappedActionHandler (payload, cb) {
152+
let res = handler({
153+
dispatch,
154+
commit,
155+
getters: store.getters,
156+
state: getNestedState(store.state, path),
157+
rootState: store.state
158+
}, payload, cb)
159+
if (!isPromise(res)) {
160+
res = Promise.resolve(res)
161+
}
162+
if (store._devtoolHook) {
163+
return res.catch(err => {
164+
store._devtoolHook.emit('vuex:error', err)
165+
throw err
166+
})
167+
} else {
168+
return res
169+
}
170+
})
171+
}
172+
175173
hotUpdate (newOptions) {
176174
this._actions = Object.create(null)
177175
this._mutations = Object.create(null)
@@ -240,7 +238,7 @@ function initStoreVM (store, state, getters) {
240238
}
241239
}
242240

243-
function initModule (store, path, module, hot) {
241+
function initModule (store, rootState, path, module, hot) {
244242
const isRoot = !path.length
245243
const {
246244
state,
@@ -252,7 +250,7 @@ function initModule (store, path, module, hot) {
252250

253251
// set state
254252
if (!isRoot && !hot) {
255-
const parentState = getNestedState(store.state, path.slice(0, -1))
253+
const parentState = getNestedState(rootState, path.slice(0, -1))
256254
const moduleName = path[path.length - 1]
257255
Vue.set(parentState, moduleName, state || {})
258256
}
@@ -275,7 +273,7 @@ function initModule (store, path, module, hot) {
275273

276274
if (modules) {
277275
Object.keys(modules).forEach(key => {
278-
initModule(store, path.concat(key), modules[key], hot)
276+
initModule(store, rootState, path.concat(key), modules[key], hot)
279277
})
280278
}
281279
}

0 commit comments

Comments
 (0)
0