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

useMutation.ts

142 lines3.8 KB

1import {
2 computed,
3 getCurrentScope,
4 onScopeDispose,
5 readonly,
6 shallowReactive,
7 shallowReadonly,
8 toRefs,
9 watch,
10} from 'vue-demi'
11import { MutationObserver } from '@tanstack/query-core'
12import { cloneDeepUnref, shouldThrowError, updateState } from './utils'
13import { useQueryClient } from './useQueryClient'
14import type { ToRefs } from 'vue-demi'
15import type {
16 DefaultError,
17 MutateFunction,
18 MutateOptions,
19 MutationObserverOptions,
20 MutationObserverResult,
21} from '@tanstack/query-core'
22import type { DistributiveOmit, MaybeRefDeep } from './types'
23import type { QueryClient } from './queryClient'
24​
25type MutationResult<TData, TError, TVariables, TContext> = DistributiveOmit<
26 MutationObserverResult<TData, TError, TVariables, TContext>,
27 'mutate' | 'reset'
29​
30type UseMutationOptionsBase<TData, TError, TVariables, TContext> =
31 MutationObserverOptions<TData, TError, TVariables, TContext> & {
32 /**
33 * Return data in a shallow ref object (it is `false` by default). It can be set to `true` to return data in a shallow ref object, which can improve performance if your data does not need to be deeply reactive.
34 */
35 shallow?: boolean
36 }
37​
38export type UseMutationOptions<
39 TData = unknown,
40 TError = DefaultError,
41 TVariables = void,
42 TContext = unknown,
43> = MaybeRefDeep<UseMutationOptionsBase<TData, TError, TVariables, TContext>>
44​
45type MutateSyncFunction<
46 TData = unknown,
47 TError = DefaultError,
48 TVariables = void,
49 TContext = unknown,
50> = (
51 ...options: Parameters<MutateFunction<TData, TError, TVariables, TContext>>
52) => void
53​
54export type UseMutationReturnType<
55 TData,
56 TError,
57 TVariables,
58 TContext,
59 TResult = MutationResult<TData, TError, TVariables, TContext>,
60> = ToRefs<Readonly<TResult>> & {
61 mutate: MutateSyncFunction<TData, TError, TVariables, TContext>
62 mutateAsync: MutateFunction<TData, TError, TVariables, TContext>
63 reset: MutationObserverResult<TData, TError, TVariables, TContext>['reset']
65​
66export function useMutation<
67 TData = unknown,
68 TError = DefaultError,
69 TVariables = void,
70 TContext = unknown,
71>(
72 mutationOptions: MaybeRefDeep<
73 UseMutationOptionsBase<TData, TError, TVariables, TContext>
74 >,
75 queryClient?: QueryClient,
76): UseMutationReturnType<TData, TError, TVariables, TContext> {
77 if (process.env.NODE_ENV === 'development') {
78 if (!getCurrentScope()) {
79 console.warn(
80 'vue-query composable like "useQuery()" should only be used inside a "setup()" function or a running effect scope. They might otherwise lead to memory leaks.',
81 )
82 }
83 }
84​
85 const client = queryClient || useQueryClient()
86 const options = computed(() => {
87 return client.defaultMutationOptions(cloneDeepUnref(mutationOptions))
88 })
89 const observer = new MutationObserver(client, options.value)
90 const state = shallowReactive(observer.getCurrentResult())
91​
92 const unsubscribe = observer.subscribe((result) => {
93 updateState(state, result)
94 })
95​
96 const mutate = (
97 variables: TVariables,
98 mutateOptions?: MutateOptions<TData, TError, TVariables, TContext>,
99 ) => {
100 observer.mutate(variables, mutateOptions).catch(() => {
101 // This is intentional
102 })
103 }
104​
105 watch(options, () => {
106 observer.setOptions(options.value)
107 })
108​
109 onScopeDispose(() => {
110 unsubscribe()
111 })
112​
113 const readonlyState =
114 process.env.NODE_ENV === 'production'
115 ? state
116 : options.value.shallow
117 ? shallowReadonly(state)
118 : readonly(state)
119​
120 const resultRefs = toRefs(readonlyState) as ToRefs<
121 Readonly<MutationResult<TData, TError, TVariables, TContext>>
122 >
123​
124 watch(
125 () => state.error,
126 (error) => {
127 if (
128 error &&
129 shouldThrowError(options.value.throwOnError, [error as TError])
130 ) {
131 throw error
132 }
133 },
134 )
135​
136 return {
137 ...resultRefs,
138 mutate,
139 mutateAsync: state.mutate,
140 reset: state.reset,
141 }