Skip to content

Commit

Permalink
fix(useSWR): fix not getting stale data until fetcher resolves when k…
Browse files Browse the repository at this point in the history
…ey change
  • Loading branch information
edumudu committed Aug 25, 2022
1 parent 87da2a7 commit b54e131
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions lib/composables/swr/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { computed, ref, readonly, watch, toRefs, toRef } from 'vue';
import { computed, readonly, watch, toRefs, toRef, unref, Ref } from 'vue';
import { toReactive, useEventListener, useIntervalFn } from '@vueuse/core';

import type { OmitFirstArrayIndex, SWRComposableConfig, SWRFetcher, SWRKey } from '@/types';
Expand Down Expand Up @@ -36,10 +36,10 @@ export const useSWR = <Data = any, Error = any>(
const valueInCache = computed(() => cacheProvider.get(key.value));
const hasCachedValue = computed(() => !!valueInCache.value);

const error = valueInCache.value ? toRef(valueInCache.value, 'error') : ref<Error>();
const isValidating = valueInCache.value ? toRef(valueInCache.value, 'isValidating') : ref(true);
const data = valueInCache.value ? toRef(valueInCache.value, 'data') : ref(fallbackValue);
const fetchedIn = valueInCache.value ? toRef(valueInCache.value, 'fetchedIn') : ref(new Date());
const error: Ref<Error | undefined> = toRef(valueInCache.value || { error: undefined }, 'error');
const data: Ref<Data | undefined> = toRef(valueInCache.value || { data: fallbackValue }, 'data');
const isValidating = toRef(valueInCache.value || { isValidating: true }, 'isValidating');
const fetchedIn = toRef(valueInCache.value || { fetchedIn: new Date() }, 'fetchedIn');

const fetchData = async () => {
const timestampToDedupExpire = (fetchedIn.value?.getTime() || 0) + dedupingInterval;
Expand Down Expand Up @@ -96,6 +96,13 @@ export const useSWR = <Data = any, Error = any>(
useIntervalFn(onRefresh, refreshInterval);
}

watch(valueInCache, (newValueInCache) => {
data.value = newValueInCache?.data;
error.value = newValueInCache?.error;
isValidating.value = newValueInCache?.isValidating ?? isValidating.value;
fetchedIn.value = newValueInCache?.fetchedIn ?? fetchedIn.value;
});

watch(
key,
(newKey, oldKey) => {
Expand All @@ -109,8 +116,8 @@ export const useSWR = <Data = any, Error = any>(
if (!hasCachedValue.value) {
cacheProvider.set(key.value, {
error,
isValidating,
data,
isValidating,
fetchedIn,
});
}
Expand Down

0 comments on commit b54e131

Please sign in to comment.