Skip to content

Commit 064fb14

Browse files
committed
move applyMiddlewares into separate method
1 parent 9bcdb9c commit 064fb14

File tree

1 file changed

+27
-16
lines changed

1 file changed

+27
-16
lines changed

src/index.js

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,7 @@ class Store {
8383
type = type.type
8484
}
8585
const mutation = this._mutations[type]
86-
const prevSnapshot = this._prevSnapshot
8786
const state = this.state
88-
let snapshot, clonedPayload
8987
if (mutation) {
9088
this._dispatching = true
9189
// apply the mutation
@@ -95,20 +93,7 @@ class Store {
9593
mutation(state, ...payload)
9694
}
9795
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)
11297
} else {
11398
console.warn(`[vuex] Unknown mutation: ${type}`)
11499
}
@@ -241,6 +226,32 @@ class Store {
241226
}
242227
})
243228
}
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+
}
244255
}
245256

246257
function install (_Vue) {

0 commit comments

Comments
 (0)