forked from TanStack/query
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuseMutation.ts
142 lines (127 loc) · 3.81 KB
/
useMutation.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import {
computed,
getCurrentScope,
onScopeDispose,
readonly,
shallowReactive,
shallowReadonly,
toRefs,
watch,
} from 'vue-demi'
import { MutationObserver } from '@tanstack/query-core'
import { cloneDeepUnref, shouldThrowError, updateState } from './utils'
import { useQueryClient } from './useQueryClient'
import type { ToRefs } from 'vue-demi'
import type {
DefaultError,
MutateFunction,
MutateOptions,
MutationObserverOptions,
MutationObserverResult,
} from '@tanstack/query-core'
import type { DistributiveOmit, MaybeRefDeep } from './types'
import type { QueryClient } from './queryClient'
type MutationResult<TData, TError, TVariables, TContext> = DistributiveOmit<
MutationObserverResult<TData, TError, TVariables, TContext>,
'mutate' | 'reset'
>
type UseMutationOptionsBase<TData, TError, TVariables, TContext> =
MutationObserverOptions<TData, TError, TVariables, TContext> & {
/**
* 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.
*/
shallow?: boolean
}
export type UseMutationOptions<
TData = unknown,
TError = DefaultError,
TVariables = void,
TContext = unknown,
> = MaybeRefDeep<UseMutationOptionsBase<TData, TError, TVariables, TContext>>
type MutateSyncFunction<
TData = unknown,
TError = DefaultError,
TVariables = void,
TContext = unknown,
> = (
...options: Parameters<MutateFunction<TData, TError, TVariables, TContext>>
) => void
export type UseMutationReturnType<
TData,
TError,
TVariables,
TContext,
TResult = MutationResult<TData, TError, TVariables, TContext>,
> = ToRefs<Readonly<TResult>> & {
mutate: MutateSyncFunction<TData, TError, TVariables, TContext>
mutateAsync: MutateFunction<TData, TError, TVariables, TContext>
reset: MutationObserverResult<TData, TError, TVariables, TContext>['reset']
}
export function useMutation<
TData = unknown,
TError = DefaultError,
TVariables = void,
TContext = unknown,
>(
mutationOptions: MaybeRefDeep<
UseMutationOptionsBase<TData, TError, TVariables, TContext>
>,
queryClient?: QueryClient,
): UseMutationReturnType<TData, TError, TVariables, TContext> {
if (process.env.NODE_ENV === 'development') {
if (!getCurrentScope()) {
console.warn(
'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.',
)
}
}
const client = queryClient || useQueryClient()
const options = computed(() => {
return client.defaultMutationOptions(cloneDeepUnref(mutationOptions))
})
const observer = new MutationObserver(client, options.value)
const state = shallowReactive(observer.getCurrentResult())
const unsubscribe = observer.subscribe((result) => {
updateState(state, result)
})
const mutate = (
variables: TVariables,
mutateOptions?: MutateOptions<TData, TError, TVariables, TContext>,
) => {
observer.mutate(variables, mutateOptions).catch(() => {
// This is intentional
})
}
watch(options, () => {
observer.setOptions(options.value)
})
onScopeDispose(() => {
unsubscribe()
})
const readonlyState =
process.env.NODE_ENV === 'production'
? state
: options.value.shallow
? shallowReadonly(state)
: readonly(state)
const resultRefs = toRefs(readonlyState) as ToRefs<
Readonly<MutationResult<TData, TError, TVariables, TContext>>
>
watch(
() => state.error,
(error) => {
if (
error &&
shouldThrowError(options.value.throwOnError, [error as TError])
) {
throw error
}
},
)
return {
...resultRefs,
mutate,
mutateAsync: state.mutate,
reset: state.reset,
}
}