@@ -37,13 +37,14 @@ class Store {
37
37
// strict mode
38
38
this . strict = strict
39
39
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 )
44
44
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 )
47
48
48
49
// apply plugins
49
50
plugins . concat ( devtoolPlugin ) . forEach ( plugin => plugin ( this ) )
@@ -63,52 +64,6 @@ class Store {
63
64
this . _committing = false
64
65
}
65
66
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
-
112
67
commit ( type , payload ) {
113
68
// check object-style commit
114
69
let mutation
@@ -172,6 +127,49 @@ class Store {
172
127
return this . _vm . $watch ( ( ) => getter ( this . state ) , cb , options )
173
128
}
174
129
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
+
175
173
hotUpdate ( newOptions ) {
176
174
this . _actions = Object . create ( null )
177
175
this . _mutations = Object . create ( null )
@@ -240,7 +238,7 @@ function initStoreVM (store, state, getters) {
240
238
}
241
239
}
242
240
243
- function initModule ( store , path , module , hot ) {
241
+ function initModule ( store , rootState , path , module , hot ) {
244
242
const isRoot = ! path . length
245
243
const {
246
244
state,
@@ -252,7 +250,7 @@ function initModule (store, path, module, hot) {
252
250
253
251
// set state
254
252
if ( ! isRoot && ! hot ) {
255
- const parentState = getNestedState ( store . state , path . slice ( 0 , - 1 ) )
253
+ const parentState = getNestedState ( rootState , path . slice ( 0 , - 1 ) )
256
254
const moduleName = path [ path . length - 1 ]
257
255
Vue . set ( parentState , moduleName , state || { } )
258
256
}
@@ -275,7 +273,7 @@ function initModule (store, path, module, hot) {
275
273
276
274
if ( modules ) {
277
275
Object . keys ( modules ) . forEach ( key => {
278
- initModule ( store , path . concat ( key ) , modules [ key ] , hot )
276
+ initModule ( store , rootState , path . concat ( key ) , modules [ key ] , hot )
279
277
} )
280
278
}
281
279
}
0 commit comments