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

useBaseQuery.ts

124 lines3.5 KB

1'use client'
2import * as React from 'react'
3​
4import { notifyManager } from '@tanstack/query-core'
5import { useSyncExternalStore } from './useSyncExternalStore'
6import { useQueryErrorResetBoundary } from './QueryErrorResetBoundary'
7import { useQueryClient } from './QueryClientProvider'
8import { useIsRestoring } from './isRestoring'
9import {
10 ensurePreventErrorBoundaryRetry,
11 getHasError,
12 useClearResetErrorBoundary,
13} from './errorBoundaryUtils'
14import { ensureStaleTime, fetchOptimistic, shouldSuspend } from './suspense'
15import type { QueryKey, QueryObserver } from '@tanstack/query-core'
16import type { UseBaseQueryOptions } from './types'
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 const unsubscribe = isRestoring
82 ? () => undefined
83 : observer.subscribe(notifyManager.batchCalls(onStoreChange))
84​
85 // Update result to make sure we did not miss any query updates
86 // between creating the observer and subscribing to it.
87 observer.updateResult()
88​
89 return unsubscribe
90 },
91 [observer, isRestoring],
92 ),
93 () => observer.getCurrentResult(),
94 () => observer.getCurrentResult(),
95 )
96​
97 React.useEffect(() => {
98 // Do not notify on updates because of changes in the options because
99 // these changes should already be reflected in the optimistic result.
100 observer.setOptions(defaultedOptions, { listeners: false })
101 }, [defaultedOptions, observer])
102​
103 // Handle suspense
104 if (shouldSuspend(defaultedOptions, result, isRestoring)) {
105 throw fetchOptimistic(defaultedOptions, observer, errorResetBoundary)
106 }
107​
108 // Handle error boundary
109 if (
110 getHasError({
111 result,
112 errorResetBoundary,
113 useErrorBoundary: defaultedOptions.useErrorBoundary,
114 query: observer.getCurrentQuery(),
115 })
116 ) {
117 throw result.error
118 }
119​
120 // Handle result property usage tracking
121 return !defaultedOptions.notifyOnChangeProps
122 ? observer.trackResult(result)
123 : result