* Runs the mutation function for the given variables through a retryer, and * drives the mutation's state and lifecycle callbacks through to settlement. * * If this mutation's state is already `pending` when `execute` is called * (i.e. it was restored, still in-flight, from a dehydrated
(variables: TVariables)
| 302 | * ``` |
| 303 | */ |
| 304 | async execute(variables: TVariables): Promise<TData> { |
| 305 | const onContinue = () => { |
| 306 | this.#dispatch({ type: 'continue' }) |
| 307 | } |
| 308 | |
| 309 | const mutationFnContext = { |
| 310 | client: this.#client, |
| 311 | meta: this.options.meta, |
| 312 | mutationKey: this.options.mutationKey, |
| 313 | } satisfies MutationFunctionContext |
| 314 | |
| 315 | const retryer = (this.#retryer = createRetryer({ |
| 316 | fn: () => { |
| 317 | if (!this.options.mutationFn) { |
| 318 | return Promise.reject(new Error('No mutationFn found')) |
| 319 | } |
| 320 | |
| 321 | return this.options.mutationFn(variables, mutationFnContext) |
| 322 | }, |
| 323 | onFail: (failureCount, error) => { |
| 324 | this.#dispatch({ type: 'failed', failureCount, error }) |
| 325 | }, |
| 326 | onPause: () => { |
| 327 | this.#dispatch({ type: 'pause' }) |
| 328 | }, |
| 329 | onContinue, |
| 330 | retry: this.options.retry ?? 0, |
| 331 | retryDelay: this.options.retryDelay, |
| 332 | networkMode: this.options.networkMode, |
| 333 | canRun: () => this.#mutationCache.canRun(this), |
| 334 | })) |
| 335 | |
| 336 | const restored = this.state.status === 'pending' |
| 337 | const isPaused = !retryer.canStart() |
| 338 | |
| 339 | try { |
| 340 | if (restored) { |
| 341 | // Dispatch continue action to unpause restored mutation |
| 342 | onContinue() |
| 343 | } else { |
| 344 | this.#dispatch({ type: 'pending', variables, isPaused }) |
| 345 | // Notify cache callback |
| 346 | if (this.#mutationCache.config.onMutate) { |
| 347 | await this.#mutationCache.config.onMutate( |
| 348 | variables, |
| 349 | this as Mutation<unknown, unknown, unknown, unknown>, |
| 350 | mutationFnContext, |
| 351 | ) |
| 352 | } |
| 353 | const context = await this.options.onMutate?.( |
| 354 | variables, |
| 355 | mutationFnContext, |
| 356 | ) |
| 357 | if (context !== this.state.context) { |
| 358 | this.#dispatch({ |
| 359 | type: 'pending', |
| 360 | context, |
| 361 | variables, |