Skip to content

Commit cbfd5b8

Browse files
blake-newmanyyx990803
authored andcommitted
[Feature] middlewares to ignore 'silent' mutation && fixed FSB mutations (#137)
- Added support for 'silent' flag - Fixed FSB standards https://github.com/acdlite/flux-standard-action - Updated tests - Updated docs - Updated API doc to include 'object style mutation'
1 parent e4dd092 commit cbfd5b8

File tree

4 files changed

+94
-6
lines changed

4 files changed

+94
-6
lines changed

docs/en/api.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,23 @@ const store = new Vuex.Store({ ...options })
7979
8080
### Vuex.Store Instance Methods
8181
82-
- **dispatch(mutationName: String, ...args)**
82+
- **dispatch(mutationName: String, ...args) | dispatch(mutation: Object)**
8383
8484
Directly dispatch a mutation. This is useful in certain situations are in general you should prefer using actions in application code.
8585
86+
*Object-Style Dispatch*
87+
88+
> requires >=0.6.2
89+
90+
You can also dispatch mutations using objects:
91+
92+
``` js
93+
store.dispatch({
94+
type: 'INCREMENT',
95+
payload: 10
96+
})
97+
```
98+
8699
- **watch(pathOrGetter: String|Function, cb: Function, [options: Object])**
87100
88101
Watch a path or a getter function's value, and call the callback when the value changes. Accepts an optional options object that takes the same options as Vue's `vm.$watch` method.

docs/en/mutations.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,37 @@ mutations: {
6767
}
6868
```
6969

70+
### Silent Dispatch
71+
72+
> requires >=0.7.0
73+
74+
In some scenarios you may not want the middlewares to record the state change. Multiple dispatches to the store in a short period or polled do not always need to be tracked. In these situations is may be considered appropriate to silent the mutations.
75+
76+
*Note:* This should be avoided where necessary. Silent mutations break the contract of all state changes being tracked by the devtool. Use sparingly and where absolutely necessary.
77+
78+
Dispatching without hitting middlewares can be achieved with a `silent` flag.
79+
80+
``` js
81+
/**
82+
* Example: Progress action.
83+
* Dispatches often for changes that are not necessary to be tracked
84+
**/
85+
export function start(store, options = {}) {
86+
let timer = setInterval(() => {
87+
store.dispatch({
88+
type: INCREMENT,
89+
silent: true,
90+
payload: {
91+
amount: 1,
92+
},
93+
});
94+
if (store.state.progress === 100) {
95+
clearInterval(timer);
96+
}
97+
}, 10);
98+
}
99+
```
100+
70101
### Mutations Follow Vue's Reactivity Rules
71102

72103
Since a Vuex store's state is made reactive by Vue, when we mutate the state, Vue components observing the state will update automatically. This also means Vuex mutations are subject to the same reactivity caveats when working with plain Vue:

src/index.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,11 @@ class Store {
7777
*/
7878

7979
dispatch (type, ...payload) {
80+
let silent = false
8081
// compatibility for object actions, e.g. FSA
8182
if (typeof type === 'object' && type.type && arguments.length === 1) {
82-
payload = [type]
83+
payload = [type.payload]
84+
if (type.silent) silent = true
8385
type = type.type
8486
}
8587
const mutation = this._mutations[type]
@@ -93,7 +95,7 @@ class Store {
9395
mutation(state, ...payload)
9496
}
9597
this._dispatching = false
96-
this._applyMiddlewares(type, payload)
98+
if (!silent) this._applyMiddlewares(type, payload)
9799
} else {
98100
console.warn(`[vuex] Unknown mutation: ${type}`)
99101
}

test/unit/test.js

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,48 @@ describe('Vuex', () => {
249249
expect(mutations[0].nextState.a).to.equal(3)
250250
})
251251

252+
it('middleware should ignore silent mutations', function () {
253+
let initState
254+
const mutations = []
255+
const store = new Vuex.Store({
256+
state: {
257+
a: 1
258+
},
259+
mutations: {
260+
[TEST] (state, n) {
261+
state.a += n
262+
}
263+
},
264+
middlewares: [
265+
{
266+
onInit (state) {
267+
initState = state
268+
},
269+
onMutation (mut, state) {
270+
expect(state).to.equal(store.state)
271+
mutations.push(mut)
272+
}
273+
}
274+
]
275+
})
276+
expect(initState).to.equal(store.state)
277+
store.dispatch(TEST, 1)
278+
store.dispatch({
279+
type: TEST,
280+
payload: 2
281+
})
282+
store.dispatch({
283+
type: TEST,
284+
silent: true,
285+
payload: 3
286+
})
287+
expect(mutations.length).to.equal(2)
288+
expect(mutations[0].type).to.equal(TEST)
289+
expect(mutations[1].type).to.equal(TEST)
290+
expect(mutations[0].payload[0]).to.equal(1)
291+
expect(mutations[1].payload[0]).to.equal(2)
292+
})
293+
252294
it('watch', function (done) {
253295
const store = new Vuex.Store({
254296
state: {
@@ -396,14 +438,14 @@ describe('Vuex', () => {
396438
a: 1
397439
},
398440
mutations: {
399-
[TEST] (state, action) {
400-
state.a += action.by
441+
[TEST] (state, amount) {
442+
state.a += amount
401443
}
402444
}
403445
})
404446
store.dispatch({
405447
type: TEST,
406-
by: 2
448+
payload: 2
407449
})
408450
expect(store.state.a).to.equal(3)
409451
})

0 commit comments

Comments
 (0)