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

suspense.ts

102 lines3.4 KB

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