Skip to content

Commit

Permalink
Add skipStoreUpdates fetch option, only null out lastFetchKey followi…
Browse files Browse the repository at this point in the history
…ng a failed fetch
  • Loading branch information
christianbaroni committed Jan 1, 2025
1 parent d44633f commit 13af587
Showing 1 changed file with 26 additions and 6 deletions.
32 changes: 26 additions & 6 deletions src/state/internal/createQueryStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ interface FetchOptions {
* If `true`, the fetch operation bypasses existing cached data.
*/
force?: boolean;
/**
* If `true`, the fetch will simply return the data without any internal handling or side effects,
* running in parallel with any other ongoing fetches. Use together with `force: true` if you want to
* guarantee that a fresh fetch is triggered regardless of the current store state.
* @default false
*/
skipStoreUpdates?: boolean;
/**
* Overrides the default stale duration for this fetch, in milliseconds.
* If the fetch is successful, the subsequently scheduled refetch will occur after
Expand Down Expand Up @@ -513,7 +520,7 @@ export function createQueryStore<
return activeFetchPromise;
}

if (abortInterruptedFetches) abortActiveFetch();
if (abortInterruptedFetches && !options?.skipStoreUpdates) abortActiveFetch();

if (!options?.force) {
const { errorInfo, lastFetchedAt: cachedLastFetchedAt } = get().queryCache[currentQueryKey] ?? {};
Expand All @@ -525,13 +532,17 @@ export function createQueryStore<
}
}

set(state => ({ ...state, error: null, status: QueryStatuses.Loading }));
lastFetchKey = currentQueryKey;
if (!options?.skipStoreUpdates) {
set(state => ({ ...state, error: null, status: QueryStatuses.Loading }));
lastFetchKey = currentQueryKey;
}

const fetchOperation = async () => {
try {
if (enableLogs) console.log('[🔄 Fetching 🔄] for queryKey: ', currentQueryKey, '::: params: ', effectiveParams);
const rawResult = await (abortInterruptedFetches ? fetchWithAbortControl(effectiveParams) : fetcher(effectiveParams, null));
const rawResult = await (abortInterruptedFetches && !options?.skipStoreUpdates
? fetchWithAbortControl(effectiveParams)
: fetcher(effectiveParams, null));
const lastFetchedAt = Date.now();
if (enableLogs) console.log('[✅ Fetch Successful ✅] for queryKey: ', currentQueryKey);

Expand All @@ -544,6 +555,8 @@ export function createQueryStore<
});
}

if (options?.skipStoreUpdates) return transformedData;

set(state => {
let newState: S = {
...state,
Expand Down Expand Up @@ -614,10 +627,18 @@ export function createQueryStore<
return null;
}

if (options?.skipStoreUpdates) {
logger.error(new RainbowError(`[createQueryStore: ${persistConfig?.storageKey || currentQueryKey}]: Failed to fetch data`), {
error,
});
return null;
}

const typedError = error instanceof Error ? error : new Error(String(error));
const entry = disableCache ? undefined : get().queryCache[currentQueryKey];
const currentRetryCount = entry?.errorInfo?.retryCount ?? 0;

if (lastFetchKey === currentQueryKey) lastFetchKey = null;
onError?.(typedError, currentRetryCount);

if (currentRetryCount < maxRetries) {
Expand Down Expand Up @@ -670,13 +691,12 @@ export function createQueryStore<
}

logger.error(new RainbowError(`[createQueryStore: ${persistConfig?.storageKey || currentQueryKey}]: Failed to fetch data`), {
error: typedError,
error,
});

return null;
} finally {
activeFetchPromise = null;
lastFetchKey = null;
}
};

Expand Down

0 comments on commit 13af587

Please sign in to comment.