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

useIsMutating.svelte.ts

42 lines1.3 KB

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