Vuex: Provide action's type in store.subscribe

Created on 5 Nov 2018  路  2Comments  路  Source: vuejs/vuex

What problem does this feature solve?

One mutation might be commited by serveral actions, sometimes, I need the action's type which commited the mutation when writing a vuex plugin. For example:

A plugin that can maintain an state to monitor the asynchronous state of each action.

Without the provided action's type, the only way I can think of is that each action corresponds to a unique mutaion.

By the way, I know the subscribeAction method, but it can't help, because I need both mutation's type and action's type.

thx!

What does the proposed API look like?

store.subscribe((mutation,  state,  type) => {
  // mutation's type is 'mutation.type'
  // action's type is 'type'

  // I need these two types both.
})

Most helpful comment

Since mutations can be commited as Object-Style Commit you can pass any value, even if the mutation doesn't need it.

E.g.:

...
  actions: {
    MY_ACTION({ commit }, actionArgs) {
      commit({
        type: 'MY_MUTATION',
        data: actionArgs,
        $mySecretVar: true
      })
   }
...

Inside the store.subscribe method you can check for mutation.$mySecretVar.

All 2 comments

Since mutations can be commited as Object-Style Commit you can pass any value, even if the mutation doesn't need it.

E.g.:

...
  actions: {
    MY_ACTION({ commit }, actionArgs) {
      commit({
        type: 'MY_MUTATION',
        data: actionArgs,
        $mySecretVar: true
      })
   }
...

Inside the store.subscribe method you can check for mutation.$mySecretVar.

Closing due to inactivity. I think you should just subscribe to action because it's possible now via subscribeAction method.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

gdelazzari picture gdelazzari  路  3Comments

fnlctrl picture fnlctrl  路  4Comments

weepy picture weepy  路  3Comments

niallobrien picture niallobrien  路  3Comments

visualjerk picture visualjerk  路  3Comments