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

inject-is-fetching.ts

28 lines1.1 KB

1import { assertInInjectionContext, inject } from '@angular/core'
2import { QueryClient } from '@tanstack/query-core'
3import { injectExternalStore } from './utils/inject-external-store'
4import type { QueryFilters } from '@tanstack/query-core'
5import type { Signal } from '@angular/core'
6​
7/**
8 * Injects a signal that tracks the number of queries that your application is loading or fetching in the
9 * background (useful for app-wide loading indicators).
10 *
11 * Can be used for app-wide loading indicators
12 * @param filters - A reactive factory for the filters.
13 * @returns signal with number of loading or fetching queries.
14 */
15export function injectIsFetching(
16 filters: () => QueryFilters = () => ({}),
17): Signal<number> {
18 if (typeof ngDevMode === 'undefined' || ngDevMode) {
19 assertInInjectionContext(injectIsFetching)
20 }
21 const queryClient = inject(QueryClient)
22 const cache = queryClient.getQueryCache()
23​
24 return injectExternalStore(() => ({
25 getSnapshot: () => queryClient.isFetching(filters()),
26 subscribe: (onStoreChange) => cache.subscribe(onStoreChange),
27 }))