@@ -58,12 +58,6 @@ class Store {
58
58
assert ( false , `Use store.replaceState() to explicit replace store state.` )
59
59
}
60
60
61
- replaceState ( state ) {
62
- this . _committing = true
63
- this . _vm . state = state
64
- this . _committing = false
65
- }
66
-
67
61
commit ( type , payload ) {
68
62
// check object-style commit
69
63
let mutation
@@ -105,10 +99,6 @@ class Store {
105
99
} )
106
100
}
107
101
108
- onActionsResolved ( cb ) {
109
- Promise . all ( this . _pendingActions ) . then ( cb )
110
- }
111
-
112
102
subscribe ( fn ) {
113
103
const subs = this . _subscribers
114
104
if ( subs . indexOf ( fn ) < 0 ) {
@@ -127,6 +117,12 @@ class Store {
127
117
return this . _vm . $watch ( ( ) => getter ( this . state ) , cb , options )
128
118
}
129
119
120
+ replaceState ( state ) {
121
+ this . _committing = true
122
+ this . _vm . state = state
123
+ this . _committing = false
124
+ }
125
+
130
126
registerModule ( path , module , hot ) {
131
127
this . _committing = true
132
128
if ( typeof path === 'string' ) path = [ path ]
@@ -136,40 +132,6 @@ class Store {
136
132
this . _committing = false
137
133
}
138
134
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
-
173
135
hotUpdate ( newOptions ) {
174
136
this . _actions = Object . create ( null )
175
137
this . _mutations = Object . create ( null )
@@ -191,6 +153,10 @@ class Store {
191
153
}
192
154
this . registerModule ( [ ] , options , true )
193
155
}
156
+
157
+ onActionsResolved ( cb ) {
158
+ Promise . all ( this . _pendingActions ) . then ( cb )
159
+ }
194
160
}
195
161
196
162
function assert ( condition , msg ) {
@@ -257,18 +223,18 @@ function initModule (store, rootState, path, module, hot) {
257
223
258
224
if ( mutations ) {
259
225
Object . keys ( mutations ) . forEach ( key => {
260
- store . registerMutation ( key , mutations [ key ] , path )
226
+ registerMutation ( store , key , mutations [ key ] , path )
261
227
} )
262
228
}
263
229
264
230
if ( actions ) {
265
231
Object . keys ( actions ) . forEach ( key => {
266
- store . registerAction ( key , actions [ key ] , path )
232
+ registerAction ( store , key , actions [ key ] , path )
267
233
} )
268
234
}
269
235
270
236
if ( getters ) {
271
- wrapGetters ( store . _wrappedGetters , getters , path )
237
+ wrapGetters ( store , getters , path )
272
238
}
273
239
274
240
if ( modules ) {
@@ -278,14 +244,46 @@ function initModule (store, rootState, path, module, hot) {
278
244
}
279
245
}
280
246
281
- function wrapGetters ( getters , moduleGetters , modulePath ) {
247
+ function registerMutation ( store , type , handler , path = [ ] ) {
248
+ const entry = store . _mutations [ type ] || ( store . _mutations [ type ] = [ ] )
249
+ entry . push ( function wrappedMutationHandler ( payload ) {
250
+ handler ( getNestedState ( store . state , path ) , payload )
251
+ } )
252
+ }
253
+
254
+ function registerAction ( store , type , handler , path = [ ] ) {
255
+ const entry = store . _actions [ type ] || ( store . _actions [ type ] = [ ] )
256
+ const { dispatch, commit } = store
257
+ entry . push ( function wrappedActionHandler ( payload , cb ) {
258
+ let res = handler ( {
259
+ dispatch,
260
+ commit,
261
+ getters : store . getters ,
262
+ state : getNestedState ( store . state , path ) ,
263
+ rootState : store . state
264
+ } , payload , cb )
265
+ if ( ! isPromise ( res ) ) {
266
+ res = Promise . resolve ( res )
267
+ }
268
+ if ( store . _devtoolHook ) {
269
+ return res . catch ( err => {
270
+ store . _devtoolHook . emit ( 'vuex:error' , err )
271
+ throw err
272
+ } )
273
+ } else {
274
+ return res
275
+ }
276
+ } )
277
+ }
278
+
279
+ function wrapGetters ( store , moduleGetters , modulePath ) {
282
280
Object . keys ( moduleGetters ) . forEach ( getterKey => {
283
281
const rawGetter = moduleGetters [ getterKey ]
284
- if ( getters [ getterKey ] ) {
282
+ if ( store . _wrappedGetters [ getterKey ] ) {
285
283
console . error ( `[vuex] duplicate getter key: ${ getterKey } ` )
286
284
return
287
285
}
288
- getters [ getterKey ] = function wrappedGetter ( store ) {
286
+ store . _wrappedGetters [ getterKey ] = function wrappedGetter ( store ) {
289
287
return rawGetter (
290
288
getNestedState ( store . state , modulePath ) , // local state
291
289
store . getters , // getters
0 commit comments