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

useIsFetching.ts

41 lines1.1 KB

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