superwf / vuex-cache

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

cache.dispatch , Promise and return response #39

Closed bigoper closed 3 years ago

bigoper commented 5 years ago

I'm following the docs

import { cacheAction } from 'vuex-cache';

// ...

const actions = {
  'FETCH_STARGAZERS': cacheAction(
    ({ cache, commit }, payload) => (
      cache.dispatch('FETCH_REPOSITORIES')
        .then((repos) => Promise.all(repos.map(getStargazers)))
        .then((stargazers) => {
          commit('STARGAZERS', [].concat(...stargazers));
        })
    )
  ),

  'SET_STARGAZERS': (context, payload) => { ... }
}

I have the followings (my code)

FETCH_LISTINGSV2_CACHED: cacheAction(function({ cache, dispatch, commit }, id) {
    console.info('FETCH_LISTINGSV2_CACHED', id)
    cache.dispatch('GET_LISTINGV2', id)
  }),
async GET_LISTINGV2({ commit }, id) {
    const response = await this.$axios.get('/listings/' + id, {
      baseURL: `https://${APISERVERS[Math.floor(Math.random() * APISERVERS.length)]}/api/v1`,
      headers: headers
    })
    const { listing } = await response
    return listing
  },

It works, but now I wish to do the following:

VitorLuizC commented 5 years ago

Do you are using version 2 or 3? There's an issue on version 2's cacheAction.

(I pointed it in my review, but it was merged anyway https://github.com/superwf/vuex-cache/pull/17)