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

useBaseQuery.ts

117 lines3.2 KB

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