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

useIsFetching.ts

30 lines945 Bytes

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