Open History to see the last commit
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
44}