* 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)
| 299 | * ``` |
| 300 | */ |
| 301 | async execute(variables: TVariables): Promise<TData> { |
| 302 | const onContinue = () => { |
| 303 | this.#dispatch({ type: 'continue' }) |
| 304 | } |
| 305 | |
| 306 | const mutationFnContext = { |
| 307 | client: this.#client, |
| 308 | meta: this.options.meta, |
| 309 | mutationKey: this.options.mutationKey, |
| 310 | } satisfies MutationFunctionContext |
| 311 | |
| 312 | const retryer = (this.#retryer = createRetryer({ |
| 313 | fn: () => { |
| 314 | if (!this.options.mutationFn) { |
| 315 | return Promise.reject(new Error('No mutationFn found')) |
| 316 | } |
| 317 | |
| 318 | return this.options.mutationFn(variables, mutationFnContext) |
| 319 | }, |
| 320 | onFail: (failureCount, error) => { |
| 321 | this.#dispatch({ type: 'failed', failureCount, error }) |
| 322 | }, |
| 323 | onPause: () => { |
| 324 | this.#dispatch({ type: 'pause' }) |
| 325 | }, |
| 326 | onContinue, |
| 327 | retry: this.options.retry ?? 0, |
| 328 | retryDelay: this.options.retryDelay, |
| 329 | networkMode: this.options.networkMode, |
| 330 | canRun: () => this.#mutationCache.canRun(this), |
| 331 | })) |
| 332 | |
| 333 | const restored = this.state.status === 'pending' |
| 334 | const isPaused = !retryer.canStart() |
| 335 | |
| 336 | try { |
| 337 | if (restored) { |
| 338 | // Dispatch continue action to unpause restored mutation |
| 339 | onContinue() |
| 340 | } else { |
| 341 | this.#dispatch({ type: 'pending', variables, isPaused }) |
| 342 | // Notify cache callback |
| 343 | if (this.#mutationCache.config.onMutate) { |
| 344 | await this.#mutationCache.config.onMutate( |
| 345 | variables, |
| 346 | this as Mutation<unknown, unknown, unknown, unknown>, |
| 347 | mutationFnContext, |
| 348 | ) |
| 349 | } |
| 350 | const context = await this.options.onMutate?.( |
| 351 | variables, |
| 352 | mutationFnContext, |
| 353 | ) |
| 354 | if (context !== this.state.context) { |
| 355 | this.#dispatch({ |
| 356 | type: 'pending', |
| 357 | context, |
| 358 | variables, |