File size: 945 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { notifyManager } from '@tanstack/query-core'
import { readable } from 'svelte/store'
import { useQueryClient } from './useQueryClient.js'
import type { Readable } from 'svelte/store'
import type { QueryClient, QueryFilters } from '@tanstack/query-core'

export function useIsFetching(
  filters?: QueryFilters,
  queryClient?: QueryClient,
): Readable<number> {
  const client = useQueryClient(queryClient)
  const cache = client.getQueryCache()
  // isFetching is the prev value initialized on mount *
  let isFetching = client.isFetching(filters)

  const { subscribe } = readable(isFetching, (set) => {
    return cache.subscribe(
      notifyManager.batchCalls(() => {
        const newIsFetching = client.isFetching(filters)
        if (isFetching !== newIsFetching) {
          // * and update with each change
          isFetching = newIsFetching
          set(isFetching)
        }
      }),
    )
  })

  return { subscribe }
}