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

useBaseQuery.ts

152 lines3.3 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: DefaultedQueryObserverOptions<
72 TQueryFnData,
73 TError,
74 TData,
75 TQueryData,
76 TQueryKey
77 > = client.defaultQueryOptions(cloneDeepUnref(options as any))
78​
79 defaulted._optimisticResults = client.isRestoring.value
80 ? 'isRestoring'
81 : 'optimistic'
82​
83 return defaulted
84 })
85​
86 const observer = new Observer(client, defaultedOptions.value)
87 const state = reactive(observer.getCurrentResult())
88​
89 const unsubscribe = ref(() => {
90 // noop
91 })
92​
93 watch(
94 client.isRestoring,
95 (isRestoring) => {
96 // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
97 if (!isRestoring) {
98 unsubscribe.value()
99 unsubscribe.value = observer.subscribe((result) => {
100 updateState(state, result)
101 })
102 }
103 },
104 { immediate: true },
105 )
106​
107 watch(
108 defaultedOptions,
109 () => {
110 observer.setOptions(defaultedOptions.value)
111 updateState(state, observer.getCurrentResult())
112 },
113 { deep: true },
114 )
115​
116 onScopeDispose(() => {
117 unsubscribe.value()
118 })
119​
120 const suspense = () => {
121 return new Promise<QueryObserverResult<TData, TError>>((resolve) => {
122 let stopWatch = () => {
123 //noop
124 }
125 const run = () => {
126 if (defaultedOptions.value.enabled !== false) {
127 const optimisticResult = observer.getOptimisticResult(
128 defaultedOptions.value,
129 )
130 if (optimisticResult.isStale) {
131 stopWatch()
132 resolve(observer.fetchOptimistic(defaultedOptions.value))
133 } else {
134 stopWatch()
135 resolve(optimisticResult)
136 }
137 }
138 }
139​
140 run()
141​
142 stopWatch = watch(defaultedOptions, run, { deep: true })
143 })
144 }
145​
146 return {
147 ...(toRefs(readonly(state)) as ToRefs<
148 Readonly<QueryObserverResult<TData, TError>>
149 >),
150 suspense,
151 }