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

useBaseQuery.ts

116 lines3.2 KB

1import * as React from 'react'
2import { useSyncExternalStore } from './useSyncExternalStore'
3​
4import type { QueryKey, QueryObserver } from '@tanstack/query-core'
5import { notifyManager } from '@tanstack/query-core'
6import { useQueryErrorResetBoundary } from './QueryErrorResetBoundary'
7import { useQueryClient } from './QueryClientProvider'
8import type { UseBaseQueryOptions } from './types'
9import { useIsRestoring } from './isRestoring'
10import {
11 ensurePreventErrorBoundaryRetry,
12 getHasError,
13 useClearResetErrorBoundary,
14} from './errorBoundaryUtils'
15import { ensureStaleTime, shouldSuspend, fetchOptimistic } from './suspense'
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) {
33 const queryClient = useQueryClient({ context: options.context })
34 const isRestoring = useIsRestoring()
35 const errorResetBoundary = useQueryErrorResetBoundary()
36 const defaultedOptions = queryClient.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 queryClient,
71 defaultedOptions,
72 ),
73 )
74​
75 const result = observer.getOptimisticResult(defaultedOptions)
76​
77 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 useErrorBoundary: defaultedOptions.useErrorBoundary,
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