Open History to see the last commit
1import {
2 computed,
3 getCurrentScope,
4 onScopeDispose,
5 ref,
6 unref,
7 watchEffect,
8} from 'vue-demi'
9import { useQueryClient } from './useQueryClient'
10import { cloneDeepUnref, isQueryKey } from './utils'
11import type { Ref } from 'vue-demi'
12import type { MutationFilters as MF, MutationKey } from '@tanstack/query-core'
13β
14import type { MaybeRef, MaybeRefDeep, WithQueryClientKey } from './types'
15β
16export type MutationFilters = MaybeRefDeep<WithQueryClientKey<MF>>
17β
18export function useIsMutating(filters?: MutationFilters): Ref<number>
19export function useIsMutating(
20 mutationKey?: MaybeRef<MutationKey>,
21 filters?: Omit<MutationFilters, 'mutationKey'>,
22): Ref<number>
23export function useIsMutating(
24 arg1?: MaybeRef<MutationKey> | MutationFilters,
25 arg2?: Omit<MutationFilters, 'mutationKey'>,
26): Ref<number> {
27 if (process.env.NODE_ENV === 'development') {
28 if (!getCurrentScope()) {
29 console.warn(
30 'vue-query composables like "useQuery()" should only be used inside a "setup()" function or a running effect scope. They might otherwise lead to memory leaks.',
31 )
32 }
33 }
34β
35 const filters = computed(() => parseFilterArgs(arg1, arg2))
36 const queryClient =
37 filters.value.queryClient ?? useQueryClient(filters.value.queryClientKey)
38β
39 const isMutating = ref()
40β
41 const listener = () => {
42 isMutating.value = queryClient.isMutating(filters)
43 }
44β
45 const unsubscribe = queryClient.getMutationCache().subscribe(listener)
46β
47 watchEffect(listener)
48β
49 onScopeDispose(() => {
50 unsubscribe()
51 })
52β
53 return isMutating
54}
55β
56export function parseFilterArgs(
57 arg1?: MaybeRef<MutationKey> | MutationFilters,
58 arg2: MutationFilters = {},
59) {
60 const plainArg1 = unref(arg1)
61 const plainArg2 = unref(arg2)
62β
63 let options = plainArg1
64β
65 if (isQueryKey(plainArg1)) {
66 options = { ...plainArg2, mutationKey: plainArg1 }
67 } else {
68 // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
69 options = plainArg1 || {}
70 }
71β
72 return cloneDeepUnref(options) as WithQueryClientKey<MF>
73}