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

useBaseQuery.ts

154 lines3.4 KB

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