8000 fix object-style dispatch · poorprogrammer/vuejs.org@db8b44f · GitHub
[go: up one dir, main page]

Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit db8b44f

Browse files
committed
fix object-style dispatch
1 parent 61ae0be commit db8b44f

File tree

2 files changed

+32
-14
lines changed

2 files changed

+32
-14
lines changed

src/index.js

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,11 @@ class Store {
8383

8484
dispatch (type, ...payload) {
8585
let silent = false
86+
let isObjectStyleDispatch = false
8687
// compatibility for object actions, e.g. FSA
8788
if (typeof type === 'object' && type.type && arguments.length === 1) {
88-
payload = [type.payload]
89+
isObjectStyleDispatch = true
90+
payload = type
8991
if (type.silent) silent = true
9092
type = type.type
9193
}
@@ -95,12 +97,20 @@ class Store {
9597
this._dispatching = true
9698
// apply the mutation
9799
if (Array.isArray(mutation)) {
98-
mutation.forEach(m => m(state, ...payload))
100+
mutation.forEach(m => {
101+
isObjectStyleDispatch
102+
? m(state, payload)
103+
: m(state, ...payload)
104+
})
99105
} else {
100-
mutation(state, ...payload)
106+
isObjectStyleDispatch
107+
? mutation(state, payload)
108+
: mutation(state, ...payload)
101109
}
102110
this._dispatching = false
103-
if (!silent) this._applyMiddlewares(type, payload)
111+
if (!silent) {
112+
this._applyMiddlewares(type, payload, isObjectStyleDispatch)
113+
}
104114
} else {
105115
console.warn(`[vuex] Unknown mutation: ${type}`)
106116
}
@@ -273,9 +283,10 @@ class Store {
273283
*
274284
* @param {String} type
275285
* @param {Array} payload
286+
* @param {Boolean} isObjectStyleDispatch
276287
*/
277288

278-
_applyMiddlewares (type, payload) {
289+
_applyMiddlewares (type, payload, isObjectStyleDispatch) {
279290
const state = this.state
280291
const prevSnapshot = this._prevSnapshot
281292
let snapshot, clonedPayload
@@ -285,10 +296,17 @@ class Store {
285296
}
286297
this._middlewares.forEach(m => {
287298
if (m.onMutation) {
299+
const mutation = isObjectStyleDispatch
300+
? m.snapshot
301+
? clonedPayload
302+
: payload
303+
: m.snapshot
304+
? { type, payload: clonedPayload }
305+
: { type, payload }
288306
if (m.snapshot) {
289-
m.onMutation({ type, payload: clonedPayload }, snapshot, prevSnapshot, this)
307+
m.onMutation(mutation, snapshot, prevSnapshot, this)
290308
} else {
291-
m.onMutation({ type, payload }, state, this)
309+
m.onMutation(mutation, state, this)
292310
}
293311
}
294312
})

test/unit/test.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -348,8 +348,8 @@ describe('Vuex', () => {
348348
a: 1
349349
},
350350
mutations: {
351-
[TEST] (state, n) {
352-
state.a += n
351+
[TEST] (state, { payload }) {
352+
state.a += payload
353353
}
354354
},
355355
middlewares: [
@@ -378,8 +378,8 @@ describe('Vuex', () => {
378378
expect(mutations.length).to.equal(2)
379379
expect(mutations[0].type).to.equal(TEST)
380380
expect(mutations[1].type).to.equal(TEST)
381-
expect(mutations[0].payload[0]).to.equal(1)
382-
expect(mutations[1].payload[0]).to.equal(2)
381+
expect(mutations[0].payload[0]).to.equal(1) // normal dispatch
382+
expect(mutations[1].payload).to.equal(2) // object dispatch
383383
})
384384

385385
it('watch', function (done) {
@@ -525,14 +525,14 @@ describe('Vuex', () => {
525525
a: 1
526526
},
527527
mutations: {
528-
[TEST] (state, amount) {
529-
state.a += amount
528+
[TEST] (state, { by }) {
529+
state.a += by
530530
}
531531
}
532532
})
533533
store.dispatch({
534534
type: TEST,
535-
payload: 2
535+
by: 2
536536
})
537537
expect(store.state.a).to.equal(3)
538538
})

0 commit comments

Comments
 (0)
0