@@ -83,9 +83,7 @@ class Store {
83
83
type = type . type
84
84
}
85
85
const mutation = this . _mutations [ type ]
86
- const prevSnapshot = this . _prevSnapshot
87
86
const state = this . state
88
- let snapshot , clonedPayload
89
87
if ( mutation ) {
90
88
this . _dispatching = true
91
89
// apply the mutation
@@ -95,20 +93,7 @@ class Store {
95
93
mutation ( state , ...payload )
96
94
}
97
95
this . _dispatching = false
98
- // invoke middlewares
99
- if ( this . _needSnapshots ) {
100
- snapshot = this . _prevSnapshot = deepClone ( state )
101
- clonedPayload = deepClone ( payload )
102
- }
103
- this . _middlewares . forEach ( m => {
104
- if ( m . onMutation ) {
105
- if ( m . snapshot ) {
106
- m . onMutation ( { type, payload : clonedPayload } , snapshot , prevSnapshot , this )
107
- } else {
108
- m . onMutation ( { type, payload } , state , this )
109
- }
110
- }
111
- } )
96
+ this . _applyMiddlewares ( type , payload )
112
97
} else {
113
98
console . warn ( `[vuex] Unknown mutation: ${ type } ` )
114
99
}
@@ -241,6 +226,32 @@ class Store {
241
226
}
242
227
} )
243
228
}
229
+
230
+ /**
231
+ * Apply the middlewares on a given mutation.
232
+ *
233
+ * @param {String } type
234
+ * @param {Array } payload
235
+ */
236
+
237
+ _applyMiddlewares ( type , payload ) {
238
+ const state = this . state
239
+ const prevSnapshot = this . _prevSnapshot
240
+ let snapshot , clonedPayload
241
+ if ( this . _needSnapshots ) {
242
+ snapshot = this . _prevSnapshot = deepClone ( state )
243
+ clonedPayload = deepClone ( payload )
244
+ }
245
+ this . _middlewares . forEach ( m => {
246
+ if ( m . onMutation ) {
247
+ if ( m . snapshot ) {
248
+ m . onMutation ( { type, payload : clonedPayload } , snapshot , prevSnapshot , this )
249
+ } else {
250
+ m . onMutation ( { type, payload } , state , this )
251
+ }
252
+ }
253
+ } )
254
+ }
244
255
}
245
256
246
257
function install ( _Vue ) {
0 commit comments