Skip to content

Commit

Permalink
Ensure effectiveParams and currentQueryKey are always in sync within …
Browse files Browse the repository at this point in the history
…each fetch operation
  • Loading branch information
christianbaroni committed Dec 30, 2024
1 parent c898f97 commit a6f8998
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions src/state/internal/createQueryStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ export function createQueryStore<
clearTimeout(activeRefetchTimeout);
activeRefetchTimeout = null;
}
const currentQueryKey = get().queryKey;
const currentQueryKey = getQueryKey(params);
const lastFetchedAt =
get().queryCache[currentQueryKey]?.lastFetchedAt || (disableCache && lastFetchKey === currentQueryKey ? get().lastFetchedAt : null);
const timeUntilRefetch = lastFetchedAt ? effectiveStaleTime - (Date.now() - lastFetchedAt) : effectiveStaleTime;
Expand All @@ -500,8 +500,8 @@ export function createQueryStore<
if (!options?.force && !get().enabled) return;

const effectiveParams = params ?? getCurrentResolvedParams();
const { queryKey: currentQueryKey, status } = get();
const isLoading = status === QueryStatuses.Loading;
const currentQueryKey = getQueryKey(effectiveParams);
const isLoading = get().status === QueryStatuses.Loading;

if (activeFetchPromise && lastFetchKey === currentQueryKey && isLoading && !options?.force) {
return activeFetchPromise;
Expand Down Expand Up @@ -733,12 +733,13 @@ export function createQueryStore<
}
});

const { enabled, fetch, isStale, queryKey } = get();

const { enabled, fetch, isStale } = get();
const currentParams = getCurrentResolvedParams();
set(state => ({ ...state, queryKey: getQueryKey(currentParams) }));
const currentQueryKey = getQueryKey(currentParams);

set(state => ({ ...state, queryKey: currentQueryKey }));

if (!get().queryCache[queryKey] || isStale()) {
if (!get().queryCache[currentQueryKey] || isStale()) {
fetch(currentParams);
} else if (enabled) {
scheduleNextFetch(currentParams, undefined);
Expand Down

0 comments on commit a6f8998

Please sign in to comment.