Open History to see the last commit
1import { computed, onScopeDispose, ref, watch } from 'vue-demi'
2import type { Ref } from 'vue-demi'
3import type { QueryFilters as QF } from '@tanstack/query-core'
4import { useQueryClient } from './useQueryClient'
5import { cloneDeepUnref } from './utils'
6import type { MaybeRefDeep } from './types'
7import type { QueryClient } from './queryClient'
8β
9export type QueryFilters = MaybeRefDeep<QF>
10β
11export function useIsFetching(
12 fetchingFilters: MaybeRefDeep<QF> = {},
13 queryClient?: QueryClient,
14): Ref<number> {
15 const filters = computed(() => cloneDeepUnref(fetchingFilters))
16 const client = queryClient || useQueryClient()
17β
18 const isFetching = ref(client.isFetching(filters))
19β
20 const unsubscribe = client.getQueryCache().subscribe(() => {
21 isFetching.value = client.isFetching(filters)
22 })
23β
24 watch(
25 filters,
26 () => {
27 isFetching.value = client.isFetching(filters)
28 },
29 { deep: true },
30 )
31β
32 onScopeDispose(() => {
33 unsubscribe()
34 })
35β
36 return isFetching
37}