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

useIsFetching.ts

44 lines1.3 KB

1import { getCurrentScope, onScopeDispose, ref, watchEffect } from 'vue-demi'
2import { useQueryClient } from './useQueryClient'
3import { cloneDeepUnref } from './utils'
4import type { Ref } from 'vue-demi'
5import type { QueryFilters as QF } from '@tanstack/query-core'
6import type { MaybeRefDeep } from './types'
7import type { QueryClient } from './queryClient'
8​
9export type QueryFilters = MaybeRefDeep<QF> | (() => MaybeRefDeep<QF>)
10​
11export function useIsFetching(
12 fetchingFilters: QueryFilters = {},
13 queryClient?: QueryClient,
14): Ref<number> {
15 if (process.env.NODE_ENV === 'development') {
16 if (!getCurrentScope()) {
17 console.warn(
18 '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.',
19 )
20 }
21 }
22​
23 const client = queryClient || useQueryClient()
24​
25 const isFetching = ref()
26​
27 const listener = () => {
28 const resolvedFilters =
29 typeof fetchingFilters === 'function'
30 ? fetchingFilters()
31 : fetchingFilters
32 isFetching.value = client.isFetching(cloneDeepUnref(resolvedFilters))
33 }
34​
35 const unsubscribe = client.getQueryCache().subscribe(listener)
36​
37 watchEffect(listener)
38​
39 onScopeDispose(() => {
40 unsubscribe()
41 })
42​
43 return isFetching