Files
clash-verge-rev/src/services/query-client.ts
T
Tunglies e7572a92cc refactor(services): drop the bespoke retry engine from the query wrapper
The custom onErrorRetry existed only to honor a function-form
retryDelay, which exactly one caller (the app data provider) used for
exponential backoff. Fall back to SWR's built-in error retry and
express that caller's cadence with constant options.
2026-11-02 21:48:54 +08:00

110 lines
2.8 KiB
TypeScript

import useSWR, {
type SWRConfiguration,
type SWRResponse,
mutate as swrMutate,
} from 'swr'
type QueryKey = string | readonly unknown[]
type QueryDataUpdater<T> =
| T
| undefined
| ((current: T | undefined) => T | undefined)
type QueryOptions<T> = {
queryKey: QueryKey
queryFn: () => Promise<T> | T
enabled?: boolean
initialData?: T | (() => T | undefined)
placeholderData?: T | (() => T | undefined)
staleTime?: number
retry?: number | false
retryDelay?: number
refetchInterval?: number | false
refetchIntervalInBackground?: boolean
revalidateOnMount?: boolean
refetchOnWindowFocus?: boolean
refetchOnReconnect?: boolean
}
type QueryResult<T> = SWRResponse<T> & {
isFetching: boolean
isPending: boolean
refetch: () => Promise<{ data: T | undefined }>
}
export const swrConfig: SWRConfiguration = {
dedupingInterval: 2000,
errorRetryCount: 3,
errorRetryInterval: 5000,
revalidateOnFocus: false,
}
export const setCacheData = <T>(
queryKey: QueryKey,
updaterOrData: QueryDataUpdater<T>,
) =>
swrMutate<T>(queryKey, updaterOrData, {
populateCache: true,
revalidate: false,
})
export const revalidateQuery = (queryKey: QueryKey) => swrMutate(queryKey)
export const revalidateQueries = (queryKeys: readonly QueryKey[]) =>
Promise.all(queryKeys.map(revalidateQuery))
export const removeCacheData = (queryKey: QueryKey) =>
setCacheData(queryKey, undefined)
export const fetchCacheData = async <T>(
queryKey: QueryKey,
queryFn: () => Promise<T> | T,
) => {
const data = await queryFn()
await setCacheData(queryKey, data)
return data
}
export function useQuery<T>(options: QueryOptions<T>): QueryResult<T> {
const {
queryKey,
queryFn,
enabled = true,
initialData,
placeholderData,
retry,
retryDelay,
refetchInterval,
refetchIntervalInBackground,
revalidateOnMount,
refetchOnWindowFocus,
refetchOnReconnect,
staleTime,
} = options
const fallbackDataSource = initialData ?? placeholderData
const fallbackData =
typeof fallbackDataSource === 'function'
? (fallbackDataSource as () => T | undefined)()
: fallbackDataSource
const swr = useSWR<T>(enabled ? queryKey : null, queryFn, {
dedupingInterval: staleTime,
errorRetryCount: retry === false ? 0 : retry,
errorRetryInterval: retryDelay ?? swrConfig.errorRetryInterval,
fallbackData,
keepPreviousData: placeholderData !== undefined,
revalidateOnFocus: refetchOnWindowFocus,
revalidateOnMount,
revalidateOnReconnect: refetchOnReconnect,
refreshInterval: refetchInterval || 0,
refreshWhenHidden: refetchIntervalInBackground ?? false,
})
return {
...swr,
isFetching: swr.isValidating,
isPending: swr.isLoading,
refetch: async () => ({ data: await swr.mutate() }),
}
}