Open History to see the last commit
1import { ReactiveValue } from './containers.svelte.js'
2import { useQueryClient } from './useQueryClient.js'
3import type { MutationFilters, QueryClient } from '@tanstack/query-core'
4β
5/**
6 * `useIsMutating` is an optional function that returns the `number` of mutations that your application is
7 * running (useful for app-wide loading indicators).
8 * @param filters - {@link MutationFilters} to narrow down which mutations to count.
9 * @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will
10 * be used.
11 * @returns A reactive value β read `.current` to get how many matching mutations are currently running.
12 * @example
13 * ```svelte
14 * <script lang="ts">
15 * import { useIsMutating } from '@tanstack/svelte-query'
16 *
17 * // How many mutations matching the posts prefix are in progress?
18 * const isMutatingPosts = useIsMutating({ mutationKey: ['posts'] })
19 * </script>
20 *
21 * {#if isMutatingPosts.current}
22 * <span>Saving posts...</span>
23 * {/if}
24 * ```
25 */
26export function useIsMutating(
27 filters?: Omit<MutationFilters, 'status'>,
28 queryClient?: QueryClient,
29): ReactiveValue<number> {
30 const client = useQueryClient(queryClient)
31 const cache = client.getMutationCache()
32β
33 return new ReactiveValue(
34 () => client.isMutating(filters),
35 (update) => cache.subscribe(update),
36 )
37}