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

useIsFetching.ts

39 lines1.1 KB

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