(action?: Action<TData, TError, TVariables, TOnMutateResult>)
| 248 | } |
| 249 | |
| 250 | #notify(action?: Action<TData, TError, TVariables, TOnMutateResult>): void { |
| 251 | notifyManager.batch(() => { |
| 252 | // First trigger the mutate callbacks |
| 253 | // Snapshot mutateOptions before dispatching — a callback (e.g. onSuccess) |
| 254 | // may call mutate() again, which overwrites this.#mutateOptions in place. |
| 255 | // Without the snapshot, onSettled would fire with the *next* mutation's |
| 256 | // options but the *current* mutation's data/variables. |
| 257 | const mutateOptions = this.#mutateOptions |
| 258 | if (mutateOptions && this.hasListeners()) { |
| 259 | const variables = this.#currentResult.variables! |
| 260 | const onMutateResult = this.#currentResult.context |
| 261 | |
| 262 | const context = { |
| 263 | client: this.#client, |
| 264 | meta: this.options.meta, |
| 265 | mutationKey: this.options.mutationKey, |
| 266 | } satisfies MutationFunctionContext |
| 267 | |
| 268 | if (action?.type === 'success') { |
| 269 | try { |
| 270 | mutateOptions.onSuccess?.( |
| 271 | action.data, |
| 272 | variables, |
| 273 | onMutateResult, |
| 274 | context, |
| 275 | ) |
| 276 | } catch (e) { |
| 277 | void Promise.reject(e) |
| 278 | } |
| 279 | try { |
| 280 | mutateOptions.onSettled?.( |
| 281 | action.data, |
| 282 | null, |
| 283 | variables, |
| 284 | onMutateResult, |
| 285 | context, |
| 286 | ) |
| 287 | } catch (e) { |
| 288 | void Promise.reject(e) |
| 289 | } |
| 290 | } else if (action?.type === 'error') { |
| 291 | try { |
| 292 | mutateOptions.onError?.( |
| 293 | action.error, |
| 294 | variables, |
| 295 | onMutateResult, |
| 296 | context, |
| 297 | ) |
| 298 | } catch (e) { |
| 299 | void Promise.reject(e) |
| 300 | } |
| 301 | try { |
| 302 | mutateOptions.onSettled?.( |
| 303 | undefined, |
| 304 | action.error, |
| 305 | variables, |
| 306 | onMutateResult, |
| 307 | context, |
no test coverage detected