superwf / vuex-cache

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

create a mapCacheActions helper #14

Closed VitorLuizC closed 5 years ago

VitorLuizC commented 6 years ago

Would be nice to have a helper function similar to mapActions, but for our cache actions.

import types from '@/store/types';
import { mapCacheActions } from 'vuex-cache';

export default {
  ...,
  methods: mapCacheActions({
    fetchUser: 'user/FETCH',
    fetchServices: { type: 'service/FETCH', timeout: 5000, param: 'ACTIVE' }
  }),
  mounted () {
    this.fetchUser();
    this.fetchServices();
  }
};

mapCacheActions should resolve it's arguments

mapCacheActions({ A: 'ACTION' }) === ({
  A () {
    return this.$store.cache.dispatch('ACTION');
  }
})

mapCacheActions({ B: { type: 'ACTION', timeout: 200, param: '0' } }) === ({
  B () {
    return this.$store.cache.dispatch({ type: 'ACTION', timeout: 200, param: '0' });
  }
})