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 {
16 QueryClient,
17 QueryKey,
18 QueryObserver,
19 QueryObserverResult,
20} from '@tanstack/query-core'
21β
22export function useBaseQuery<
23 TQueryFnData,
24 TError,
25 TData,
26 TQueryData,
27 TQueryKey extends QueryKey,
28>(
29 options: UseBaseQueryOptions<
30 TQueryFnData,
31 TError,
32 TData,
33 TQueryData,
34 TQueryKey
35 >,
36 Observer: typeof QueryObserver,
37 queryClient?: QueryClient,
38): QueryObserverResult<TData, TError> {
39 if (process.env.NODE_ENV !== 'production') {
40 if (typeof options !== 'object' || Array.isArray(options)) {
41 throw new Error(
42 'Bad argument type. Starting with v5, only the "Object" form is allowed when calling query related functions. Please use the error stack to find the culprit call. More info here: https://tanstack.com/query/latest/docs/react/guides/migrating-to-v5#supports-a-single-signature-one-object',
43 )
44 }
45 }
46β
47 const client = useQueryClient(queryClient)
48 const isRestoring = useIsRestoring()
49 const errorResetBoundary = useQueryErrorResetBoundary()
50 const defaultedOptions = client.defaultQueryOptions(options)
51β
52 // Make sure results are optimistically set in fetching state before subscribing or updating options
53 defaultedOptions._optimisticResults = isRestoring
54 ? 'isRestoring'
55 : 'optimistic'
56β
57 ensureStaleTime(defaultedOptions)
58 ensurePreventErrorBoundaryRetry(defaultedOptions, errorResetBoundary)
59β
60 useClearResetErrorBoundary(errorResetBoundary)
61β
62 const [observer] = React.useState(
63 () =>
64 new Observer<TQueryFnData, TError, TData, TQueryData, TQueryKey>(
65 client,
66 defaultedOptions,
67 ),
68 )
69β
70 const result = observer.getOptimisticResult(defaultedOptions)
71β
72 React.useSyncExternalStore(
73 React.useCallback(
74 (onStoreChange) => {
75 const unsubscribe = isRestoring
76 ? () => undefined
77 : observer.subscribe(notifyManager.batchCalls(onStoreChange))
78β
79 // Update result to make sure we did not miss any query updates
80 // between creating the observer and subscribing to it.
81 observer.updateResult()
82β
83 return unsubscribe
84 },
85 [observer, isRestoring],
86 ),
87 () => observer.getCurrentResult(),
88 () => observer.getCurrentResult(),
89 )
90β
91 React.useEffect(() => {
92 // Do not notify on updates because of changes in the options because
93 // these changes should already be reflected in the optimistic result.
94 observer.setOptions(defaultedOptions, { listeners: false })
95 }, [defaultedOptions, observer])
96β
97 // Handle suspense
98 if (shouldSuspend(defaultedOptions, result)) {
99 // Do the same thing as the effect right above because the effect won't run
100 // when we suspend but also, the component won't re-mount so our observer would
101 // be out of date.
102 throw fetchOptimistic(defaultedOptions, observer, errorResetBoundary)
103 }
104β
105 // Handle error boundary
106 if (
107 getHasError({
108 result,
109 errorResetBoundary,
110 throwOnError: defaultedOptions.throwOnError,
111 query: client
112 .getQueryCache()
113 .get<
114 TQueryFnData,
115 TError,
116 TQueryData,
117 TQueryKey
118 >(defaultedOptions.queryHash),
119 })
120 ) {
121 throw result.error
122 }
123β
124 // Handle result property usage tracking
125 return !defaultedOptions.notifyOnChangeProps
126 ? observer.trackResult(result)
127 : result
128}