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

useIsMutating.svelte.ts

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