Open History to see the last commit
1import {
2 computed,
3 getCurrentScope,
4 onScopeDispose,
5 reactive,
6 readonly,
7 shallowReactive,
8 shallowReadonly,
9 toRefs,
10 watch,
11} from 'vue-demi'
12import { shouldThrowError } from '@tanstack/query-core'
13import { useQueryClient } from './useQueryClient'
14import { cloneDeepUnref, updateState } from './utils'
15import type { Ref } from 'vue-demi'
16import type {
17 DefaultedQueryObserverOptions,
18 QueryKey,
19 QueryObserver,
20 QueryObserverResult,
21} from '@tanstack/query-core'
22import type { QueryClient } from './queryClient'
23import type { UseQueryOptions } from './useQuery'
24import type { UseInfiniteQueryOptions } from './useInfiniteQuery'
25β
26export type UseBaseQueryReturnType<
27 TData,
28 TError,
29 TResult = QueryObserverResult<TData, TError>,
30> = {
31 [K in keyof TResult]: K extends
32 | 'fetchNextPage'
33 | 'fetchPreviousPage'
34 | 'refetch'
35 ? TResult[K]
36 : Ref<Readonly<TResult>[K]>
37} & {
38 suspense: () => Promise<TResult>
39}
40β
41type UseQueryOptionsGeneric<
42 TQueryFnData,
43 TError,
44 TData,
45 TQueryData,
46 TQueryKey extends QueryKey = QueryKey,
47 TPageParam = unknown,
48> =
49 | UseQueryOptions<TQueryFnData, TError, TData, TQueryData, TQueryKey>
50 | UseInfiniteQueryOptions<
51 TQueryFnData,
52 TError,
53 TData,
54 TQueryData,
55 TQueryKey,
56 TPageParam
57 >
58β
59export function useBaseQuery<
60 TQueryFnData,
61 TError,
62 TData,
63 TQueryData,
64 TQueryKey extends QueryKey,
65 TPageParam,
66>(
67 Observer: typeof QueryObserver,
68 options: UseQueryOptionsGeneric<
69 TQueryFnData,
70 TError,
71 TData,
72 TQueryData,
73 TQueryKey,
74 TPageParam
75 >,
76 queryClient?: QueryClient,
77): UseBaseQueryReturnType<TData, TError> {
78 if (process.env.NODE_ENV === 'development') {
79 if (!getCurrentScope()) {
80 console.warn(
81 'vue-query composable like "useQuery()" should only be used inside a "setup()" function or a running effect scope. They might otherwise lead to memory leaks.',
82 )
83 }
84 }
85β
86 const client = queryClient || useQueryClient()
87β
88 const defaultedOptions = computed(() => {
89 const clonedOptions = cloneDeepUnref(options as any)
90β
91 if (typeof clonedOptions.enabled === 'function') {
92 clonedOptions.enabled = clonedOptions.enabled()
93 }
94β
95 const defaulted: DefaultedQueryObserverOptions<
96 TQueryFnData,
97 TError,
98 TData,
99 TQueryData,
100 TQueryKey
101 > = client.defaultQueryOptions(clonedOptions)
102β
103 defaulted._optimisticResults = client.isRestoring.value
104 ? 'isRestoring'
105 : 'optimistic'
106β
107 return defaulted
108 })
109β
110 const observer = new Observer(client, defaultedOptions.value)
111 // @ts-expect-error
112 const state = defaultedOptions.value.shallow
113 ? shallowReactive(observer.getCurrentResult())
114 : reactive(observer.getCurrentResult())
115β
116 let unsubscribe = () => {
117 // noop
118 }
119β
120 watch(
121 client.isRestoring,
122 (isRestoring) => {
123 if (!isRestoring) {
124 unsubscribe()
125 unsubscribe = observer.subscribe((result) => {
126 updateState(state, result)
127 })
128 }
129 },
130 { immediate: true },
131 )
132β
133 const updater = () => {
134 observer.setOptions(defaultedOptions.value)
135 updateState(state, observer.getCurrentResult())
136 }
137β
138 watch(defaultedOptions, updater)
139β
140 onScopeDispose(() => {
141 unsubscribe()
142 })
143β
144 // fix #5910
145 const refetch = (...args: Parameters<(typeof state)['refetch']>) => {
146 updater()
147 return state.refetch(...args)
148 }
149β
150 const suspense = () => {
151 return new Promise<QueryObserverResult<TData, TError>>(
152 (resolve, reject) => {
153 let stopWatch = () => {
154 // noop
155 }
156 const run = () => {
157 if (defaultedOptions.value.enabled !== false) {
158 // fix #6133
159 observer.setOptions(defaultedOptions.value)
160 const optimisticResult = observer.getOptimisticResult(
161 defaultedOptions.value,
162 )
163 if (optimisticResult.isStale) {
164 stopWatch()
165 observer
166 .fetchOptimistic(defaultedOptions.value)
167 .then(resolve, (error: TError) => {
168 if (
169 shouldThrowError(defaultedOptions.value.throwOnError, [
170 error,
171 observer.getCurrentQuery(),
172 ])
173 ) {
174 reject(error)
175 } else {
176 resolve(observer.getCurrentResult())
177 }
178 })
179 } else {
180 stopWatch()
181 resolve(optimisticResult)
182 }
183 }
184 }
185β
186 run()
187β
188 stopWatch = watch(defaultedOptions, run)
189 },
190 )
191 }
192β
193 // Handle error boundary
194 watch(
195 () => state.error,
196 (error) => {
197 if (
198 state.isError &&
199 !state.isFetching &&
200 shouldThrowError(defaultedOptions.value.throwOnError, [
201 error as TError,
202 observer.getCurrentQuery(),
203 ])
204 ) {
205 throw error
206 }
207 },
208 )
209β
210 // @ts-expect-error
211 const readonlyState = defaultedOptions.value.shallow
212 ? shallowReadonly(state)
213 : readonly(state)
214β
215 const object: any = toRefs(readonlyState)
216 for (const key in state) {
217 if (typeof state[key as keyof typeof state] === 'function') {
218 object[key] = state[key as keyof typeof state]
219 }
220 }
221β
222 object.suspense = suspense
223 object.refetch = refetch
224β
225 return object as UseBaseQueryReturnType<TData, TError>
226}