Open History to see the last commit
1import {
2 onScopeDispose,
3 toRefs,
4 readonly,
5 reactive,
6 watch,
7 computed,
8} from 'vue-demi'
9import type { ToRefs } from 'vue-demi'
10import type {
11 QueryObserver,
12 QueryKey,
13 QueryObserverResult,
14 DefaultedQueryObserverOptions,
15} from '@tanstack/query-core'
16import { useQueryClient } from './useQueryClient'
17import { updateState, cloneDeepUnref } from './utils'
18import type { QueryClient } from './queryClient'
19import type { UseQueryOptions } from './useQuery'
20import type { UseInfiniteQueryOptions } from './useInfiniteQuery'
21β
22export type UseBaseQueryReturnType<
23 TData,
24 TError,
25 Result = QueryObserverResult<TData, TError>,
26> = ToRefs<Readonly<Result>> & {
27 suspense: () => Promise<Result>
28}
29β
30type UseQueryOptionsGeneric<
31 TQueryFnData,
32 TError,
33 TData,
34 TQueryData,
35 TQueryKey extends QueryKey = QueryKey,
36 TPageParam = unknown,
37> =
38 | UseQueryOptions<TQueryFnData, TError, TData, TQueryData, TQueryKey>
39 | UseInfiniteQueryOptions<
40 TQueryFnData,
41 TError,
42 TData,
43 TQueryData,
44 TQueryKey,
45 TPageParam
46 >
47β
48export function useBaseQuery<
49 TQueryFnData,
50 TError,
51 TData,
52 TQueryData,
53 TQueryKey extends QueryKey,
54 TPageParam,
55>(
56 Observer: typeof QueryObserver,
57 options: UseQueryOptionsGeneric<
58 TQueryFnData,
59 TError,
60 TData,
61 TQueryData,
62 TQueryKey,
63 TPageParam
64 >,
65 queryClient?: QueryClient,
66): UseBaseQueryReturnType<TData, TError> {
67 const client = queryClient || useQueryClient()
68β
69 const defaultedOptions = computed(() => {
70 const defaulted: DefaultedQueryObserverOptions<
71 TQueryFnData,
72 TError,
73 TData,
74 TQueryData,
75 TQueryKey
76 > = client.defaultQueryOptions(cloneDeepUnref(options as any))
77β
78 defaulted._optimisticResults = client.isRestoring.value
79 ? 'isRestoring'
80 : 'optimistic'
81β
82 return defaulted
83 })
84β
85 const observer = new Observer(client, defaultedOptions.value)
86 const state = reactive(observer.getCurrentResult())
87β
88 let unsubscribe = () => {
89 // noop
90 }
91β
92 watch(
93 client.isRestoring,
94 (isRestoring) => {
95 // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
96 if (!isRestoring) {
97 unsubscribe()
98 unsubscribe = observer.subscribe((result) => {
99 updateState(state, result)
100 })
101 }
102 },
103 { immediate: true },
104 )
105β
106 watch(
107 defaultedOptions,
108 () => {
109 observer.setOptions(defaultedOptions.value)
110 updateState(state, observer.getCurrentResult())
111 },
112 { deep: true },
113 )
114β
115 onScopeDispose(() => {
116 unsubscribe()
117 })
118β
119 const suspense = () => {
120 return new Promise<QueryObserverResult<TData, TError>>((resolve) => {
121 let stopWatch = () => {
122 //noop
123 }
124 const run = () => {
125 if (defaultedOptions.value.enabled !== false) {
126 const optimisticResult = observer.getOptimisticResult(
127 defaultedOptions.value,
128 )
129 if (optimisticResult.isStale) {
130 stopWatch()
131 resolve(observer.fetchOptimistic(defaultedOptions.value))
132 } else {
133 stopWatch()
134 resolve(optimisticResult)
135 }
136 }
137 }
138β
139 run()
140β
141 stopWatch = watch(defaultedOptions, run, { deep: true })
142 })
143 }
144β
145 return {
146 ...(toRefs(readonly(state)) as ToRefs<
147 Readonly<QueryObserverResult<TData, TError>>
148 >),
149 suspense,
150 }
151}