Open History to see the last commit
1import { createMemo } from 'solid-js'
2import { createCacheAggregate } from './cacheAggregate'
3import { useQueryClient } from './QueryClientProvider'
4import type { MutationFilters } from '@tanstack/query-core'
5import type { QueryClient } from './QueryClient'
6import type { Accessor } from 'solid-js'
7β
8/**
9 * The `useIsMutating` primitive returns the `number` of mutations that your application currently has `pending`
10 * (useful for app-wide loading indicators).
11 * @param filters - An accessor returning the {@link MutationFilters} to narrow down the matched mutations.
12 * @param queryClient - An accessor for a custom `QueryClient`. Otherwise, the one from the nearest context
13 * will be used.
14 * @returns An accessor for the `number` of the mutations that your application currently has `pending`.
15 * @example
16 * ```tsx
17 * import { useIsMutating } from '@tanstack/solid-query'
18 *
19 * function PostsMutatingIndicator() {
20 * // How many mutations matching the posts prefix are in progress?
21 * const isMutatingPosts = useIsMutating(() => ({ mutationKey: ['posts'] }))
22 *
23 * return isMutatingPosts() > 0 ? <span>Saving posts...</span> : null
24 * }
25 * ```
26 */
27export function useIsMutating(
28 filters?: Accessor<Omit<MutationFilters, 'status'>>,
29 queryClient?: Accessor<QueryClient>,
30): Accessor<number> {
31 const client = createMemo(() => useQueryClient(queryClient?.()))
32 return createCacheAggregate(
33 (onEvent) => client().getMutationCache().subscribe(onEvent),
34 () => client().isMutating(filters?.()),
35 0,
36 )
37}