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

useIsFetching.ts

33 lines916 Bytes

1import {
2 type QueryClient,
3 type QueryFilters,
4 notifyManager,
5} from '@tanstack/query-core'
6import { readable } from 'svelte/store'
7import { useQueryClient } from './useQueryClient'
8import type { Readable } from 'svelte/store'
9​
10export function useIsFetching(
11 filters?: QueryFilters,
12 queryClient?: QueryClient,
13): Readable<number> {
14 const client = useQueryClient(queryClient)
15 const cache = client.getQueryCache()
16 // isFetching is the prev value initialized on mount *
17 let isFetching = client.isFetching(filters)
18​
19 const { subscribe } = readable(isFetching, (set) => {
20 return cache.subscribe(
21 notifyManager.batchCalls(() => {
22 const newIsFetching = client.isFetching(filters)
23 if (isFetching !== newIsFetching) {
24 // * and update with each change
25 isFetching = newIsFetching
26 set(isFetching)
27 }
28 }),
29 )
30 })
31​
32 return { subscribe }