Every second Vuex commit to the same action is slow

徘徊边缘 提交于 2021-01-27 07:31:55

问题


I am using Vuex V3.0.1 with Vue.js v 2.5.17

The same action commit in vuex is slow exactly every second time. If I put the same axios request inside my component and call it from within the component, it is consistently between 16 and 22 ms. When I however dispatch to the store, every second request is consistently over 300ms.

this.$store.dispatch('getQueryData', {QueryId: this.to.QueryId, params: data, CacheResults : this.to.CacheResults }).then(response => {
    this.options = response.data
        var arr = response.data
        var defaultValue = this.model[this.field.key] || this.field.defaultValue || null
        var valueprop = this.to.valueProp
        var model = {}
        if (defaultValue !== null)
        {
          this.options.forEach(function(value){
              if (value[valueprop] == defaultValue){
                model = value
              }
          })
          this.model[this.field.key] = model
        }
}, error => {
   this.$store.commit('setError', error.response)
   this.options = [];
})

And in my store

getQueryData ({commit}, payload) {
  const QueryIdStr = String(payload.QueryId)

  if (store.state.queries[QueryIdStr]) {
    var promise = Promise.resolve({data: store.state.queries[QueryIdStr]})
    return promise
  } else {
    return new Promise((resolve, reject) => {
      axios
        .get('/scadservices/api/tablesv2/Query/' + payload.QueryId + '/data.json', {crossdomain: true, withCredentials: true, params: payload.params})
        .then(response => {
          var output = {
            QueryId: payload.QueryId,
            data: response.data
          }
          if (payload.CacheResults)
          {
            commit('setQueryData', output)
          }
          resolve(response)
        }, error => {
          commit('setError', error.response)
          reject(error)
        })
    })
  }
},

And in my mutations

setQueryData: (state, payload) => {
  const QueryIdStr = String(payload.QueryId)
  Vue.set(state.queries, QueryIdStr, payload.data)
}

来源:https://stackoverflow.com/questions/52715729/every-second-vuex-commit-to-the-same-action-is-slow

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!