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