superwf / vuex-cache

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

cache.dispatch use async/await #16

Closed wcxaa closed 5 years ago

wcxaa commented 5 years ago
cache.dispatch = async (...args) => {
    const type = argsToString(args)

    const timeout = getTimeout(args, option)
    if (timeout) {
      const now = Date.now()
      if (!timeoutCache.has(type)) {
        timeoutCache.set(type, now)
      } else {
        const timeoutOfCurrentType = timeoutCache.get(type)
        // console.log(now - timeout, timeoutOfCurrentType)
        if (now - timeout > timeoutOfCurrentType) {
          cache.delete(type)
          timeoutCache.delete(type)
        }
      }
    }

    if (!cache.has(type)) {
      cache.set(type, await store.dispatch.apply(store, args))
    }

cache.dispatch 使用async/await,当dispatch报错时不set cache?

VitorLuizC commented 5 years ago

It caches the promise, not the value returned by your action.

wcxaa commented 5 years ago

https://github.com/superwf/vuex-cache/issues/7

superwf commented 5 years ago

VitorLuizC is right.

wcxaa commented 5 years ago

VitorLuizC is right.

I know it caches promise, but why it is necessary to cache a rejected promise ?

VitorLuizC commented 5 years ago

Save Promise as cache value is smaller, easier and less invasive than intercept it's resolution/rejection reason. But, maybe is not the right choice, so I'll create another issue with this dicussion.