@@ -11,7 +11,6 @@ class Store {
11
11
12
12
const {
13
13
state = { } ,
14
- modules = { } ,
15
14
plugins = [ ] ,
16
15
strict = false
17
16
} = options
@@ -21,6 +20,7 @@ class Store {
21
20
this . _committing = false
22
21
this . _actions = Object . create ( null )
23
22
this . _mutations = Object . create ( null )
23
+ this . _wrappedGetters = Object . create ( null )
24
24
this . _subscribers = [ ]
25
25
this . _pendingActions = [ ]
26
26
@@ -34,16 +34,17 @@ class Store {
34
34
return commit . call ( store , type , payload )
35
35
}
36
36
37
- // init state and getters
38
- const getters = extractModuleGetters ( options . getters , modules )
39
- initStoreState ( this , state , getters )
37
+ // strict mode
38
+ this . strict = strict
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
44
41
45
// apply root module
42
46
this . module ( [ ] , options )
43
47
44
- // strict mode
45
- if ( strict ) enableStrictMode ( this )
46
-
47
48
// apply plugins
48
49
plugins . concat ( devtoolPlugin ) . forEach ( plugin => plugin ( this ) )
49
50
}
@@ -67,39 +68,10 @@ class Store {
67
68
if ( typeof path === 'string' ) path = [ path ]
68
69
assert ( Array . isArray ( path ) , `module path must be a string or an Array.` )
69
70
70
- const isRoot = ! path . length
71
- const {
72
- state,
73
- actions,
74
- mutations,
75
- modules
76
- } = module
77
-
78
- // set state
79
- if ( ! isRoot && ! hot ) {
80
- const parentState = getNestedState ( this . state , path . slice ( 0 , - 1 ) )
81
- if ( ! parentState ) debugger
82
- const moduleName = path [ path . length - 1 ]
83
- Vue . set ( parentState , moduleName , state || { } )
84
- }
85
-
86
- if ( mutations ) {
87
- Object . keys ( mutations ) . forEach ( key => {
88
- this . mutation ( key , mutations [ key ] , path )
89
- } )
90
- }
71
+ initModule ( this , path , module , hot )
91
72
92
- if ( actions ) {
93
- Object . keys ( actions ) . forEach ( key => {
94
- this . action ( key , actions [ key ] , path )
95
- } )
96
- }
73
+ initStoreVM ( this , this . state , this . _wrappedGetters )
97
74
98
- if ( modules ) {
99
- Object . keys ( modules ) . forEach ( key => {
100
- this . module ( path . concat ( key ) , modules [ key ] , hot )
101
- } )
102
- }
103
75
this . _committing = false
104
76
}
105
77
@@ -203,43 +175,33 @@ class Store {
203
175
hotUpdate ( newOptions ) {
204
176
this . _actions = Object . create ( null )
205
177
this . _mutations = Object . create ( null )
178
+ this . _wrappedGetters = Object . create ( null )
206
179
const options = this . _options
207
180
if ( newOptions . actions ) {
208
181
options . actions = newOptions . actions
209
182
}
210
183
if ( newOptions . mutations ) {
211
184
options . mutations = newOptions . mutations
212
185
}
186
+ if ( newOptions . getters ) {
187
+ options . getters = newOptions . getters
188
+ }
213
189
if ( newOptions . modules ) {
214
190
for ( const key in newOptions . modules ) {
215
191
options . modules [ key ] = newOptions . modules [ key ]
216
192
}
217
193
}
218
194
this . module ( [ ] , options , true )
219
-
220
- // update getters
221
- const getters = extractModuleGetters ( newOptions . getters , newOptions . modules )
222
- if ( Object . keys ( getters ) . length ) {
223
- const oldVm = this . _vm
224
- initStoreState ( this , this . state , getters )
225
- if ( this . strict ) {
226
- enableStrictMode ( this )
227
- }
228
- // dispatch changes in all subscribed watchers
229
- // to force getter re-evaluation.
230
- this . _committing = true
231
- oldVm . state = null
232
- this . _committing = false
233
- Vue . nextTick ( ( ) => oldVm . $destroy ( ) )
234
- }
235
195
}
236
196
}
237
197
238
198
function assert ( condition , msg ) {
239
199
if ( ! condition ) throw new Error ( `[vuex] ${ msg } ` )
240
200
}
241
201
242
- function initStoreState ( store , state , getters ) {
202
+ function initStoreVM ( store , state , getters ) {
203
+ const oldVm = store . _vm
204
+
243
205
// bind getters
244
206
store . getters = { }
245
207
const computed = { }
@@ -262,30 +224,67 @@ function initStoreState (store, state, getters) {
262
224
computed
263
225
} )
264
226
Vue . config . silent = silent
227
+
228
+ // enable strict mode for new vm
229
+ if ( store . strict ) {
230
+ enableStrictMode ( store )
231
+ }
232
+
233
+ if ( oldVm ) {
234
+ // dispatch changes in all subscribed watchers
235
+ // to force getter re-evaluation.
236
+ store . _committing = true
237
+ oldVm . state = null
238
+ store . _committing = false
239
+ Vue . nextTick ( ( ) => oldVm . $destroy ( ) )
240
+ }
265
241
}
266
242
267
- function extractModuleGetters ( getters = { } , modules = { } , path = [ ] ) {
268
- if ( ! path . length ) {
269
- wrapGetters ( getters , getters , path , true )
243
+ function initModule ( store , path , module , hot ) {
244
+ const isRoot = ! path . length
245
+ const {
246
+ state,
247
+ actions,
248
+ mutations,
249
+ getters,
250
+ modules
251
+ } = module
252
+
253
+ // set state
254
+ if ( ! isRoot && ! hot ) {
255
+ const parentState = getNestedState ( store . state , path . slice ( 0 , - 1 ) )
256
+ if ( ! parentState ) debugger
257
+ const moduleName = path [ path . length - 1 ]
258
+ Vue . set ( parentState , moduleName , state || { } )
270
259
}
271
- if ( ! modules ) {
272
- return getters
260
+
261
+ if ( mutations ) {
262
+ Object . keys ( mutations ) . forEach ( key => {
263
+ store . mutation ( key , mutations [ key ] , path )
264
+ } )
265
+ }
266
+
267
+ if ( actions ) {
268
+ Object . keys ( actions ) . forEach ( key => {
269
+ store . action ( key , actions [ key ] , path )
270
+ } )
271
+ }
272
+
273
+ if ( getters ) {
274
+ wrapGetters ( store . _wrappedGetters , getters , path )
275
+ }
276
+
277
+ if ( modules ) {
278
+ Object . keys ( modules ) . forEach ( key => {
279
+ initModule ( store , path . concat ( key ) , modules [ key ] , hot )
280
+ } )
273
281
}
274
- Object . keys ( modules ) . forEach ( key => {
275
- const module = modules [ key ]
276
- const modulePath = path . concat ( key )
277
- if ( module . getters ) {
278
- wrapGetters ( getters , module . getters , modulePath )
279
- }
280
- extractModuleGetters ( getters , module . modules , modulePath )
281
- } )
282
- return getters
283
282
}
284
283
285
- function wrapGetters ( getters , moduleGetters , modulePath , force ) {
284
+ function wrapGetters ( getters , moduleGetters , modulePath ) {
286
285
Object . keys ( moduleGetters ) . forEach ( getterKey => {
287
286
const rawGetter = moduleGetters [ getterKey ]
288
- if ( getters [ getterKey ] && ! force ) {
287
+ if ( getters [ getterKey ] ) {
289
288
console . error ( `[vuex] duplicate getter key: ${ getterKey } ` )
290
289
return
291
290
}
0 commit comments