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

useBaseQuery.ts

123 lines3.6 KB

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