Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

suspense.ts

103 lines3.4 KB

1import type {
2 DefaultError,
3 DefaultedQueryObserverOptions,
4 Query,
5 QueryKey,
6 QueryObserver,
7 QueryObserverResult,
8} from '@tanstack/query-core'
9​
10import type { QueryErrorResetBoundaryValue } from './QueryErrorResetBoundary'
11​
12/**
13 * The default `throwOnError` of the suspense hooks: throws the error to the nearest error boundary
14 * only if the query has no data to show.
15 * @param _error - The error of the query. Unused.
16 * @param query - The query that errored.
17 * @returns `true` if the query has no data.
18 */
19export const defaultThrowOnError = <
20 TQueryFnData = unknown,
21 TError = DefaultError,
22 TData = TQueryFnData,
23 TQueryKey extends QueryKey = QueryKey,
24>(
25 _error: TError,
26 query: Query<TQueryFnData, TError, TData, TQueryKey>,
27) => query.state.data === undefined
28​
29/**
30 * Raises `staleTime` and a numeric `gcTime` to at least 1000ms when `suspense` is enabled, so a
31 * component that remounts after suspending doesn't refetch right away or find the query garbage
32 * collected. A `'static'` `staleTime` is kept as is. Mutates the options.
33 * @param defaultedOptions - The defaulted query options to adjust.
34 */
35export const ensureSuspenseTimers = (
36 defaultedOptions: DefaultedQueryObserverOptions<any, any, any, any, any>,
37) => {
38 if (defaultedOptions.suspense) {
39 // Handle staleTime to ensure minimum 1000ms in Suspense mode
40 // This prevents unnecessary refetching when components remount after suspending
41 const MIN_SUSPENSE_TIME_MS = 1000
42​
43 const clamp = (value: number | 'static' | undefined) =>
44 value === 'static'
45 ? value
46 : Math.max(value ?? MIN_SUSPENSE_TIME_MS, MIN_SUSPENSE_TIME_MS)
47​
48 const originalStaleTime = defaultedOptions.staleTime
49 defaultedOptions.staleTime =
50 typeof originalStaleTime === 'function'
51 ? (...args) => clamp(originalStaleTime(...args))
52 : clamp(originalStaleTime)
53​
54 if (typeof defaultedOptions.gcTime === 'number') {
55 defaultedOptions.gcTime = Math.max(
56 defaultedOptions.gcTime,
57 MIN_SUSPENSE_TIME_MS,
58 )
59 }
60 }
62​
63/**
64 * Checks whether a query should suspend: `suspense` is enabled and the result is still `pending`.
65 * @param defaultedOptions - The defaulted query options, if any.
66 * @param result - The current result of the observer.
67 * @returns `true` if the component should suspend.
68 */
69export const shouldSuspend = (
70 defaultedOptions:
71 | DefaultedQueryObserverOptions<any, any, any, any, any>
72 | undefined,
73 result: QueryObserverResult<any, any>,
74) => defaultedOptions?.suspense && result.isPending
75​
76/**
77 * Fetches the query for a suspending component, without affecting the observer's own result. If
78 * the fetch fails, the reset state of the error boundary is cleared, so the error is thrown to it.
79 * @param defaultedOptions - The defaulted query options to fetch with.
80 * @param observer - The observer of the query.
81 * @param errorResetBoundary - The value of the nearest `QueryErrorResetBoundary`.
82 * @returns A promise that resolves once the fetch settles. It never rejects.
83 */
84export const fetchOptimistic = <
85 TQueryFnData,
86 TError,
87 TData,
88 TQueryData,
89 TQueryKey extends QueryKey,
90>(
91 defaultedOptions: DefaultedQueryObserverOptions<
92 TQueryFnData,
93 TError,
94 TData,
95 TQueryData,
96 TQueryKey
97 >,
98 observer: QueryObserver<TQueryFnData, TError, TData, TQueryData, TQueryKey>,
99 errorResetBoundary: QueryErrorResetBoundaryValue,
100) =>
101 observer.fetchOptimistic(defaultedOptions).catch(() => {
102 errorResetBoundary.clearReset()
103 })