refactor: extract magic constants — wave 2 (TanStack Query defaults)
Promote the duplicated query lifecycle constants in \$shared/api/queryClient.ts: - staleTime (5 minutes) -> DEFAULT_QUERY_STALE_TIME_MS - gcTime (10 minutes) -> DEFAULT_QUERY_GC_TIME_MS - retry (3) -> QUERY_RETRY_COUNT - retryDelay (1s base, 30s cap) -> QUERY_RETRY_BASE_DELAY_MS + QUERY_RETRY_MAX_DELAY_MS fontCatalogStore and availableFilterStore now import the stale/gc constants instead of re-deriving '5 * 60 * 1000' / '10 * 60 * 1000'. fontCatalogStore.svelte.spec.ts's queryClient mock now passes through the new named exports via importOriginal so the consumer's imports resolve.
This commit is contained in:
@@ -1,5 +1,31 @@
|
||||
import { QueryClient } from '@tanstack/query-core';
|
||||
|
||||
/**
|
||||
* Data remains fresh for this long after fetch. Stores that override
|
||||
* staleness (e.g. filtered queries) can use 0 to bypass.
|
||||
*/
|
||||
export const DEFAULT_QUERY_STALE_TIME_MS = 5 * 60 * 1000;
|
||||
|
||||
/**
|
||||
* Unused cache entries are garbage collected after this long.
|
||||
*/
|
||||
export const DEFAULT_QUERY_GC_TIME_MS = 10 * 60 * 1000;
|
||||
|
||||
/**
|
||||
* How many times a failed query is retried before surfacing the error.
|
||||
*/
|
||||
export const QUERY_RETRY_COUNT = 3;
|
||||
|
||||
/**
|
||||
* Base delay for exponential retry backoff.
|
||||
*/
|
||||
export const QUERY_RETRY_BASE_DELAY_MS = 1000;
|
||||
|
||||
/**
|
||||
* Upper bound on retry delay regardless of attempt index.
|
||||
*/
|
||||
export const QUERY_RETRY_MAX_DELAY_MS = 30000;
|
||||
|
||||
/**
|
||||
* TanStack Query client instance
|
||||
*
|
||||
@@ -15,14 +41,8 @@ import { QueryClient } from '@tanstack/query-core';
|
||||
export const queryClient = new QueryClient({
|
||||
defaultOptions: {
|
||||
queries: {
|
||||
/**
|
||||
* Data remains fresh for 5 minutes after fetch
|
||||
*/
|
||||
staleTime: 5 * 60 * 1000,
|
||||
/**
|
||||
* Unused cache entries are removed after 10 minutes
|
||||
*/
|
||||
gcTime: 10 * 60 * 1000,
|
||||
staleTime: DEFAULT_QUERY_STALE_TIME_MS,
|
||||
gcTime: DEFAULT_QUERY_GC_TIME_MS,
|
||||
/**
|
||||
* Don't refetch when window regains focus
|
||||
*/
|
||||
@@ -31,15 +51,12 @@ export const queryClient = new QueryClient({
|
||||
* Refetch on mount if data is stale
|
||||
*/
|
||||
refetchOnMount: true,
|
||||
retry: QUERY_RETRY_COUNT,
|
||||
/**
|
||||
* Retry failed requests up to 3 times
|
||||
* Exponential backoff: 1s, 2s, 4s, 8s... capped at 30s
|
||||
*/
|
||||
retry: 3,
|
||||
/**
|
||||
* Exponential backoff for retries
|
||||
* 1s, 2s, 4s, 8s... capped at 30s
|
||||
*/
|
||||
retryDelay: attemptIndex => Math.min(1000 * 2 ** attemptIndex, 30000),
|
||||
retryDelay: attemptIndex =>
|
||||
Math.min(QUERY_RETRY_BASE_DELAY_MS * 2 ** attemptIndex, QUERY_RETRY_MAX_DELAY_MS),
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user