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

useBaseQuery.ts

175 lines4.5 KB

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