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

useBaseQuery.ts

139 lines3.7 KB

1import React from 'react'
2​
3import { QueryKey } from '../core'
4import { notifyManager } from '../core/notifyManager'
5import { QueryObserver } from '../core/queryObserver'
6import { useQueryErrorResetBoundary } from './QueryErrorResetBoundary'
7import { useQueryClient } from './QueryClientProvider'
8import { UseBaseQueryOptions } from './types'
9​
10export function useBaseQuery<
11 TQueryFnData,
12 TError,
13 TData,
14 TQueryData,
15 TQueryKey extends QueryKey
16>(
17 options: UseBaseQueryOptions<
18 TQueryFnData,
19 TError,
20 TData,
21 TQueryData,
22 TQueryKey
23 >,
24 Observer: typeof QueryObserver
25) {
26 const mountedRef = React.useRef(false)
27 const [, forceUpdate] = React.useState(0)
28​
29 const queryClient = useQueryClient()
30 const errorResetBoundary = useQueryErrorResetBoundary()
31 const defaultedOptions = queryClient.defaultQueryObserverOptions(options)
32​
33 // Make sure results are optimistically set in fetching state before subscribing or updating options
34 defaultedOptions.optimisticResults = true
35​
36 // Include callbacks in batch renders
37 if (defaultedOptions.onError) {
38 defaultedOptions.onError = notifyManager.batchCalls(
39 defaultedOptions.onError
40 )
41 }
42​
43 if (defaultedOptions.onSuccess) {
44 defaultedOptions.onSuccess = notifyManager.batchCalls(
45 defaultedOptions.onSuccess
46 )
47 }
48​
49 if (defaultedOptions.onSettled) {
50 defaultedOptions.onSettled = notifyManager.batchCalls(
51 defaultedOptions.onSettled
52 )
53 }
54​
55 if (defaultedOptions.suspense) {
56 // Always set stale time when using suspense to prevent
57 // fetching again when directly mounting after suspending
58 if (typeof defaultedOptions.staleTime !== 'number') {
59 defaultedOptions.staleTime = 1000
60 }
61 }
62​
63 if (defaultedOptions.suspense || defaultedOptions.useErrorBoundary) {
64 // Prevent retrying failed query if the error boundary has not been reset yet
65 if (!errorResetBoundary.isReset()) {
66 defaultedOptions.retryOnMount = false
67 }
68 }
69​
70 const [observer] = React.useState(
71 () =>
72 new Observer<TQueryFnData, TError, TData, TQueryData, TQueryKey>(
73 queryClient,
74 defaultedOptions
75 )
76 )
77​
78 let result = observer.getOptimisticResult(defaultedOptions)
79​
80 React.useEffect(() => {
81 mountedRef.current = true
82​
83 errorResetBoundary.clearReset()
84​
85 const unsubscribe = observer.subscribe(
86 notifyManager.batchCalls(() => {
87 if (mountedRef.current) {
88 forceUpdate(x => x + 1)
89 }
90 })
91 )
92​
93 // Update result to make sure we did not miss any query updates
94 // between creating the observer and subscribing to it.
95 observer.updateResult()
96​
97 return () => {
98 mountedRef.current = false
99 unsubscribe()
100 }
101 }, [errorResetBoundary, observer])
102​
103 React.useEffect(() => {
104 // Do not notify on updates because of changes in the options because
105 // these changes should already be reflected in the optimistic result.
106 observer.setOptions(defaultedOptions, { listeners: false })
107 }, [defaultedOptions, observer])
108​
109 // Handle suspense
110 if (defaultedOptions.suspense && result.isLoading) {
111 throw observer
112 .fetchOptimistic(defaultedOptions)
113 .then(({ data }) => {
114 defaultedOptions.onSuccess?.(data as TData)
115 defaultedOptions.onSettled?.(data, null)
116 })
117 .catch(error => {
118 errorResetBoundary.clearReset()
119 defaultedOptions.onError?.(error)
120 defaultedOptions.onSettled?.(undefined, error)
121 })
122 }
123​
124 // Handle error boundary
125 if (
126 (defaultedOptions.suspense || defaultedOptions.useErrorBoundary) &&
127 result.isError &&
128 !result.isFetching
129 ) {
130 throw result.error
131 }
132​
133 // Handle result property usage tracking
134 if (defaultedOptions.notifyOnChangeProps === 'tracked') {
135 result = observer.trackResult(result)
136 }
137​
138 return result