Open History to see the last commit
1import * as React from 'react'
2β
3import type { QueryClient, QueryKey, QueryObserver } from '@tanstack/query-core'
4import { notifyManager } from '@tanstack/query-core'
5import { useQueryErrorResetBoundary } from './QueryErrorResetBoundary'
6import { useQueryClient } from './QueryClientProvider'
7import type { UseBaseQueryOptions } from './types'
8import { useIsRestoring } from './isRestoring'
9import {
10 ensurePreventErrorBoundaryRetry,
11 getHasError,
12 useClearResetErrorBoundary,
13} from './errorBoundaryUtils'
14import { ensureStaleTime, shouldSuspend, fetchOptimistic } from './suspense'
15β
16export function useBaseQuery<
17 TQueryFnData,
18 TError,
19 TData,
20 TQueryData,
21 TQueryKey extends QueryKey,
22>(
23 options: UseBaseQueryOptions<
24 TQueryFnData,
25 TError,
26 TData,
27 TQueryData,
28 TQueryKey
29 >,
30 Observer: typeof QueryObserver,
31 queryClient?: QueryClient,
32) {
33 const client = useQueryClient(queryClient)
34 const isRestoring = useIsRestoring()
35 const errorResetBoundary = useQueryErrorResetBoundary()
36 const defaultedOptions = client.defaultQueryOptions(options)
37β
38 // Make sure results are optimistically set in fetching state before subscribing or updating options
39 defaultedOptions._optimisticResults = isRestoring
40 ? 'isRestoring'
41 : 'optimistic'
42β
43 // Include callbacks in batch renders
44 if (defaultedOptions.onError) {
45 defaultedOptions.onError = notifyManager.batchCalls(
46 defaultedOptions.onError,
47 )
48 }
49β
50 if (defaultedOptions.onSuccess) {
51 defaultedOptions.onSuccess = notifyManager.batchCalls(
52 defaultedOptions.onSuccess,
53 )
54 }
55β
56 if (defaultedOptions.onSettled) {
57 defaultedOptions.onSettled = notifyManager.batchCalls(
58 defaultedOptions.onSettled,
59 )
60 }
61β
62 ensureStaleTime(defaultedOptions)
63 ensurePreventErrorBoundaryRetry(defaultedOptions, errorResetBoundary)
64β
65 useClearResetErrorBoundary(errorResetBoundary)
66β
67 const [observer] = React.useState(
68 () =>
69 new Observer<TQueryFnData, TError, TData, TQueryData, TQueryKey>(
70 client,
71 defaultedOptions,
72 ),
73 )
74β
75 const result = observer.getOptimisticResult(defaultedOptions)
76β
77 React.useSyncExternalStore(
78 React.useCallback(
79 (onStoreChange) =>
80 isRestoring
81 ? () => undefined
82 : observer.subscribe(notifyManager.batchCalls(onStoreChange)),
83 [observer, isRestoring],
84 ),
85 () => observer.getCurrentResult(),
86 () => observer.getCurrentResult(),
87 )
88β
89 React.useEffect(() => {
90 // Do not notify on updates because of changes in the options because
91 // these changes should already be reflected in the optimistic result.
92 observer.setOptions(defaultedOptions, { listeners: false })
93 }, [defaultedOptions, observer])
94β
95 // Handle suspense
96 if (shouldSuspend(defaultedOptions, result, isRestoring)) {
97 throw fetchOptimistic(defaultedOptions, observer, errorResetBoundary)
98 }
99β
100 // Handle error boundary
101 if (
102 getHasError({
103 result,
104 errorResetBoundary,
105 throwErrors: defaultedOptions.throwErrors,
106 query: observer.getCurrentQuery(),
107 })
108 ) {
109 throw result.error
110 }
111β
112 // Handle result property usage tracking
113 return !defaultedOptions.notifyOnChangeProps
114 ? observer.trackResult(result)
115 : result
116}