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