superwf / vuex-cache

cache vuex action when dispatch
MIT License
510 stars 32 forks source link

Expose vuex-cache also as an action enchancer #17

Closed qnp closed 5 years ago

qnp commented 5 years ago

@superwf Regarding your feature request in vuex (vuejs/vuex#895), I came accross the same issue, to be able to use context.cache anywhere in my code. However, the linked discussion (vuejs/vuex#571) ended up with using action enhancers:

We're finally going in the way of action enhancers.

A nice feature for vuex-cache would be to expose the plugin and an action enhancer wrapper.

API

// expose action enhancer
export function cacheAction(action) {
  return function cacheEnhancedAction(context, payload) {
    cachePlugin(context)
    return action(context, payload)
  }
}

To be used as:

import { cacheAction } from 'vuex-cache'
actions: {
  CACHED_ACTION: cacheAction(({ cache }, payload) => {
    cache.dispatch('OTHER_ACTION', payload);
  });
}

What do you think ?