Open History to see the last commit
1import { assertInInjectionContext, inject } from '@angular/core'
2import { QueryClient } from '@tanstack/query-core'
3import { injectExternalStore } from './utils/inject-external-store'
4import type { MutationFilters } from '@tanstack/query-core'
5import type { Signal } from '@angular/core'
6β
7/**
8 * Injects a signal that tracks the number of mutations that your application currently has `pending`
9 * (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 A read-only signal with the number of fetching mutations.
14 */
15export function injectIsMutating(
16 filters: () => MutationFilters = () => ({}),
17): Signal<number> {
18 if (typeof ngDevMode === 'undefined' || ngDevMode) {
19 assertInInjectionContext(injectIsMutating)
20 }
21 const queryClient = inject(QueryClient)
22 const cache = queryClient.getMutationCache()
23β
24 return injectExternalStore(() => ({
25 getSnapshot: () => queryClient.isMutating(filters()),
26 subscribe: (onStoreChange) => cache.subscribe(onStoreChange),
27 }))
28}