| ... | ... | @@ -256,6 +256,46 @@ export class BlockingMutation< |
| 256 | 256 | this.runWithOptions(...args, {}); |
| 257 | 257 | } |
| 258 | 258 | |
| 259 | /** |
| 260 | * Using this in any situation is likely incorrect. Use {@linkcode runWithOptions} |
| 261 | * to handle success and error. |
| 262 | * |
| 263 | * By using this, you must handle the success and error conditions of the |
| 264 | * promise, or else the user will never see the result on screen. If the |
| 265 | * mutation has snapshots, be aware that cancelled mutations are implemented |
| 266 | * with promises that never resolve. |
| 267 | */ |
| 268 | runAsHeadlessPromise(...array: [...Args, RunOptions<Result>]): Promise<Result> { |
| 269 | if (!this.#client.enabled) { |
| 270 | throw new Error( |
| 271 | "MutationClient was passed enabled: false. Are you trying to perform a mutation from SSR?", |
| 272 | ); |
| 273 | } |
| 274 | |
| 275 | const args = array.slice() as Args; |
| 276 | const { onSuccess, onSuccessDataOnly, onError, onSettled, onRestore } = args |
| 277 | .pop() as RunOptions<Result>; |
| 278 | const promise = this.#runWithOptions(args, onRestore, true); |
| 279 | return promise.then((result) => { |
| 280 | // Call user handlers |
| 281 | onSuccess?.(result); |
| 282 | onSuccessDataOnly?.(result); |
| 283 | onSettled?.({ status: "success", result }); |
| 284 | return result; |
| 285 | }).catch((caught: unknown) => { |
| 286 | // Extract error and description if this is a wrapped mutation error |
| 287 | const isMutationError = (caught as MutationError)?.__mutationError === true; |
| 288 | const error = isMutationError ? (caught as MutationError).error : caught; |
| 289 | const description = isMutationError ? (caught as MutationError).description : this.describe(...args); |
| 290 | |
| 291 | // Call user handlers with the unwrapped error |
| 292 | onError?.(error); |
| 293 | onSettled?.({ status: "error", error }); |
| 294 | |
| 295 | throw caught; |
| 296 | }); |
| 297 | } |
| 298 | |
| 259 | 299 | /** Calls the mutation with custom handlers that can suppress global handlers. */ |
| 260 | 300 | runWithOptions(...array: [...Args, RunOptions<Result>]): void { |
| 261 | 301 | if (!this.#client.enabled) { |