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

useIsFetching.ts

37 lines990 Bytes

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