简体   繁体   English

Vuex 突变是原子的吗?

[英]Is Vuex mutations are atomic?

I'm trying to understand if mutations in Vuex are atomic.我试图了解 Vuex 中的突变是否是原子的。 I have this code and I'm not sure if there is any scenario when CHANGE_A mutation will be call while CHANGE_B is not finished yet:我有这段代码,但我不确定在 CHANGE_B 尚未完成的情况下是否会调用 CHANGE_A 突变:

const mutations = {
  [CHANGE_A](state, DATA) {
    Vue.set(this.test, 'left', DATA);
  },

  [CHANGE_B](state, data) {
    Vue.set(this.test, 'right', DATA);
    Vue.set(this.test, 'left', DATA);
  },
}

Thank you谢谢

Mutation are atomic because their handler functions must always be synchronous, as described in the documentation: https://vuex.vuejs.org/guide/mutations.html突变是原子的,因为它们的处理函数必须始终是同步的,如文档中所述: https://vuex.vuejs.org/guide/mutations.html

If you were to go against this practice and make the handler function asynchronous then they would no longer be atomic.如果您要 go 反对这种做法并使处理程序 function 异步,那么它们将不再是原子的。

Mutations are just ordinary synchronous functions, so yes they are atomic.突变只是普通的同步函数,所以是的,它们是原子的。 No two mutations will execute at the same time, provided you aren't doing any async operations inside any of them like fetch requests, setTimeout or promises, or indirectly calling a mutation within another mutation.没有两个突变将同时执行,前提是您没有在其中任何一个中执行任何异步操作,例如获取请求、 setTimeout或承诺,或者在另一个突变中间接调用突变。

When Vue detects that a change has been made to reactive data, then those change events are queued and watchers will be executed within the next microtask (asynchronous).当 Vue 检测到对响应数据进行了更改时,这些更改事件会排队,并且观察者将在下一个微任务(异步)中执行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM