Open History to see the last commit
1import { getDefaultState } from './mutation'
2import { notifyManager } from './notifyManager'
3import { Subscribable } from './subscribable'
4import { hashKey, shallowEqualObjects } from './utils'
5import type { QueryClient } from './queryClient'
6import type {
7 DefaultError,
8 MutateOptions,
9 MutationFunctionContext,
10 MutationObserverOptions,
11 MutationObserverResult,
12} from './types'
13import type { Action, Mutation } from './mutation'
14β
15// TYPES
16β
17type MutationObserverListener<TData, TError, TVariables, TOnMutateResult> = (
18 result: MutationObserverResult<TData, TError, TVariables, TOnMutateResult>,
19) => void
20β
21// CLASS
22β
23export class MutationObserver<
24 TData = unknown,
25 TError = DefaultError,
26 TVariables = void,
27 TOnMutateResult = unknown,
28> extends Subscribable<
29 MutationObserverListener<TData, TError, TVariables, TOnMutateResult>
30> {
31 options!: MutationObserverOptions<TData, TError, TVariables, TOnMutateResult>
32β
33 #client: QueryClient
34 #currentResult: MutationObserverResult<
35 TData,
36 TError,
37 TVariables,
38 TOnMutateResult
39 > = undefined!
40 #currentMutation?: Mutation<TData, TError, TVariables, TOnMutateResult>
41 #mutateOptions?: MutateOptions<TData, TError, TVariables, TOnMutateResult>
42β
43 constructor(
44 client: QueryClient,
45 options: MutationObserverOptions<
46 TData,
47 TError,
48 TVariables,
49 TOnMutateResult
50 >,
51 ) {
52 super()
53β
54 this.#client = client
55 this.setOptions(options)
56 this.bindMethods()
57 this.#updateResult()
58 }
59β
60 protected bindMethods(): void {
61 this.mutate = this.mutate.bind(this)
62 this.reset = this.reset.bind(this)
63 }
64β
65 setOptions(
66 options: MutationObserverOptions<
67 TData,
68 TError,
69 TVariables,
70 TOnMutateResult
71 >,
72 ) {
73 const prevOptions = this.options as
74 | MutationObserverOptions<TData, TError, TVariables, TOnMutateResult>
75 | undefined
76 this.options = this.#client.defaultMutationOptions(options)
77 if (!shallowEqualObjects(this.options, prevOptions)) {
78 this.#client.getMutationCache().notify({
79 type: 'observerOptionsUpdated',
80 mutation: this.#currentMutation,
81 observer: this,
82 })
83 }
84β
85 if (
86 prevOptions?.mutationKey &&
87 this.options.mutationKey &&
88 hashKey(prevOptions.mutationKey) !== hashKey(this.options.mutationKey)
89 ) {
90 this.reset()
91 } else if (this.#currentMutation?.state.status === 'pending') {
92 this.#currentMutation.setOptions(this.options)
93 }
94 }
95β
96 protected onUnsubscribe(): void {
97 if (!this.hasListeners()) {
98 this.#currentMutation?.removeObserver(this)
99 }
100 }
101β
102 onMutationUpdate(
103 action: Action<TData, TError, TVariables, TOnMutateResult>,
104 ): void {
105 this.#updateResult()
106β
107 this.#notify(action)
108 }
109β
110 getCurrentResult(): MutationObserverResult<
111 TData,
112 TError,
113 TVariables,
114 TOnMutateResult
115 > {
116 return this.#currentResult
117 }
118β
119 reset(): void {
120 // reset needs to remove the observer from the mutation because there is no way to "get it back"
121 // another mutate call will yield a new mutation!
122 this.#currentMutation?.removeObserver(this)
123 this.#currentMutation = undefined
124 this.#updateResult()
125 this.#notify()
126 }
127β
128 mutate(
129 variables: TVariables,
130 options?: MutateOptions<TData, TError, TVariables, TOnMutateResult>,
131 ): Promise<TData> {
132 this.#mutateOptions = options
133β
134 this.#currentMutation?.removeObserver(this)
135β
136 this.#currentMutation = this.#client
137 .getMutationCache()
138 .build(this.#client, this.options)
139β
140 this.#currentMutation.addObserver(this)
141β
142 return this.#currentMutation.execute(variables)
143 }
144β
145 #updateResult(): void {
146 const state =
147 this.#currentMutation?.state ??
148 getDefaultState<TData, TError, TVariables, TOnMutateResult>()
149β
150 this.#currentResult = {
151 ...state,
152 isPending: state.status === 'pending',
153 isSuccess: state.status === 'success',
154 isError: state.status === 'error',
155 isIdle: state.status === 'idle',
156 mutate: this.mutate,
157 reset: this.reset,
158 } as MutationObserverResult<TData, TError, TVariables, TOnMutateResult>
159 }
160β
161 #notify(action?: Action<TData, TError, TVariables, TOnMutateResult>): void {
162 notifyManager.batch(() => {
163 // First trigger the mutate callbacks
164 if (this.#mutateOptions && this.hasListeners()) {
165 const variables = this.#currentResult.variables!
166 const onMutateResult = this.#currentResult.context
167β
168 const context = {
169 client: this.#client,
170 meta: this.options.meta,
171 mutationKey: this.options.mutationKey,
172 } satisfies MutationFunctionContext
173β
174 if (action?.type === 'success') {
175 try {
176 this.#mutateOptions.onSuccess?.(
177 action.data,
178 variables,
179 onMutateResult,
180 context,
181 )
182 } catch (e) {
183 void Promise.reject(e)
184 }
185 try {
186 this.#mutateOptions.onSettled?.(
187 action.data,
188 null,
189 variables,
190 onMutateResult,
191 context,
192 )
193 } catch (e) {
194 void Promise.reject(e)
195 }
196 } else if (action?.type === 'error') {
197 try {
198 this.#mutateOptions.onError?.(
199 action.error,
200 variables,
201 onMutateResult,
202 context,
203 )
204 } catch (e) {
205 void Promise.reject(e)
206 }
207 try {
208 this.#mutateOptions.onSettled?.(
209 undefined,
210 action.error,
211 variables,
212 onMutateResult,
213 context,
214 )
215 } catch (e) {
216 void Promise.reject(e)
217 }
218 }
219 }
220β
221 // Then trigger the listeners
222 this.listeners.forEach((listener) => {
223 listener(this.#currentResult)
224 })
225 })
226 }
227}