Download react-query/packages/angular-query-experimental/src/inject-is-mutating.ts from Devendra174/react-code-dataset: direct link, hf CLI and curl.
- Browser
- Download file 1.88 kB
-
https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/1e92f2d0b9e054abc23a53595fd89ad3e2b4a34e/react-query/packages/angular-query-experimental/src/inject-is-mutating.ts
- Command line
-
hf download hf://datasets/Devendra174/react-code-dataset@1e92f2d0b9e054abc23a53595fd89ad3e2b4a34e/react-query/packages/angular-query-experimental/src/inject-is-mutating.ts
-
curl -L -o inject-is-mutating.ts https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/1e92f2d0b9e054abc23a53595fd89ad3e2b4a34e/react-query/packages/angular-query-experimental/src/inject-is-mutating.ts
1.88 kB
| import { | |
| DestroyRef, | |
| Injector, | |
| NgZone, | |
| assertInInjectionContext, | |
| inject, | |
| signal, | |
| } from '@angular/core' | |
| import { QueryClient, notifyManager } from '@tanstack/query-core' | |
| import type { MutationFilters } from '@tanstack/query-core' | |
| import type { Signal } from '@angular/core' | |
| export interface InjectIsMutatingOptions { | |
| /** | |
| * The `Injector` in which to create the isMutating signal. | |
| * | |
| * If this is not provided, the current injection context will be used instead (via `inject`). | |
| */ | |
| injector?: Injector | |
| } | |
| /** | |
| * Injects a signal that tracks the number of mutations that your application is fetching. | |
| * | |
| * Can be used for app-wide loading indicators | |
| * @param filters - The filters to apply to the query. | |
| * @param options - Additional configuration | |
| * @returns signal with number of fetching mutations. | |
| * @public | |
| */ | |
| export function injectIsMutating( | |
| filters?: MutationFilters, | |
| options?: InjectIsMutatingOptions, | |
| ): Signal<number> { | |
| !options?.injector && assertInInjectionContext(injectIsMutating) | |
| const injector = options?.injector ?? inject(Injector) | |
| const destroyRef = injector.get(DestroyRef) | |
| const ngZone = injector.get(NgZone) | |
| const queryClient = injector.get(QueryClient) | |
| const cache = queryClient.getMutationCache() | |
| // isMutating is the prev value initialized on mount * | |
| let isMutating = queryClient.isMutating(filters) | |
| const result = signal(isMutating) | |
| const unsubscribe = ngZone.runOutsideAngular(() => | |
| cache.subscribe( | |
| notifyManager.batchCalls(() => { | |
| const newIsMutating = queryClient.isMutating(filters) | |
| if (isMutating !== newIsMutating) { | |
| // * and update with each change | |
| isMutating = newIsMutating | |
| ngZone.run(() => { | |
| result.set(isMutating) | |
| }) | |
| } | |
| }), | |
| ), | |
| ) | |
| destroyRef.onDestroy(unsubscribe) | |
| return result | |
| } | |