-
-
Notifications
You must be signed in to change notification settings - Fork 3k
/
useIsFetching.ts
35 lines (31 loc) · 1.1 KB
/
useIsFetching.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import React from 'react'
import { useSyncExternalStore } from 'use-sync-external-store/shim/index.js'
import { ContextOptions } from './types'
import { QueryKey, notifyManager } from '../core'
import { parseFilterArgs, QueryFilters } from '../core/utils'
import { useQueryClient } from './QueryClientProvider'
interface Options extends ContextOptions {}
export function useIsFetching(filters?: QueryFilters, options?: Options): number
export function useIsFetching(
queryKey?: QueryKey,
filters?: QueryFilters,
options?: Options
): number
export function useIsFetching(
arg1?: QueryKey | QueryFilters,
arg2?: QueryFilters | Options,
arg3?: Options
): number {
const [filters, options = {}] = parseFilterArgs(arg1, arg2, arg3)
const queryClient = useQueryClient({ context: options.context })
const queryCache = queryClient.getQueryCache()
return useSyncExternalStore(
React.useCallback(
onStoreChange =>
queryCache.subscribe(notifyManager.batchCalls(onStoreChange)),
[queryCache]
),
() => queryClient.isFetching(filters),
() => queryClient.isFetching(filters)
)
}