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

suspense.ts

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