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