@@ -58,12 +58,6 @@ class Store {
5858 assert ( false , `Use store.replaceState() to explicit replace store state.` )
5959 }
6060
61- replaceState ( state ) {
62- this . _committing = true
63- this . _vm . state = state
64- this . _committing = false
65- }
66-
6761 commit ( type , payload ) {
6862 // check object-style commit
6963 let mutation
@@ -105,10 +99,6 @@ class Store {
10599 } )
106100 }
107101
108- onActionsResolved ( cb ) {
109- Promise . all ( this . _pendingActions ) . then ( cb )
110- }
111-
112102 subscribe ( fn ) {
113103 const subs = this . _subscribers
114104 if ( subs . indexOf ( fn ) < 0 ) {
@@ -127,6 +117,12 @@ class Store {
127117 return this . _vm . $watch ( ( ) => getter ( this . state ) , cb , options )
128118 }
129119
120+ replaceState ( state ) {
121+ this . _committing = true
122+ this . _vm . state = state
123+ this . _committing = false
124+ }
125+
130126 registerModule ( path , module , hot ) {
131127 this . _committing = true
132128 if ( typeof path === 'string' ) path = [ path ]
@@ -136,40 +132,6 @@ class Store {
136132 this . _committing = false
137133 }
138134
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-
173135 hotUpdate ( newOptions ) {
174136 this . _actions = Object . create ( null )
175137 this . _mutations = Object . create ( null )
@@ -191,6 +153,10 @@ class Store {
191153 }
192154 this . registerModule ( [ ] , options , true )
193155 }
156+
157+ onActionsResolved ( cb ) {
158+ Promise . all ( this . _pendingActions ) . then ( cb )
159+ }
194160}
195161
196162function assert ( condition , msg ) {
@@ -257,18 +223,18 @@ function initModule (store, rootState, path, module, hot) {
257223
258224 if ( mutations ) {
259225 Object . keys ( mutations ) . forEach ( key => {
260- store . registerMutation ( key , mutations [ key ] , path )
226+ registerMutation ( store , key , mutations [ key ] , path )
261227 } )
262228 }
263229
264230 if ( actions ) {
265231 Object . keys ( actions ) . forEach ( key => {
266- store . registerAction ( key , actions [ key ] , path )
232+ registerAction ( store , key , actions [ key ] , path )
267233 } )
268234 }
269235
270236 if ( getters ) {
271- wrapGetters ( store . _wrappedGetters , getters , path )
237+ wrapGetters ( store , getters , path )
272238 }
273239
274240 if ( modules ) {
@@ -278,14 +244,46 @@ function initModule (store, rootState, path, module, hot) {
278244 }
279245}
280246
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 ) {
282280 Object . keys ( moduleGetters ) . forEach ( getterKey => {
283281 const rawGetter = moduleGetters [ getterKey ]
284- if ( getters [ getterKey ] ) {
282+ if ( store . _wrappedGetters [ getterKey ] ) {
285283 console . error ( `[vuex] duplicate getter key: ${ getterKey } ` )
286284 return
287285 }
288- getters [ getterKey ] = function wrappedGetter ( store ) {
286+ store . _wrappedGetters [ getterKey ] = function wrappedGetter ( store ) {
289287 return rawGetter (
290288 getNestedState ( store . state , modulePath ) , // local state
291289 store . getters , // getters
0 commit comments