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