* Rollback the transaction and any conflicting transactions * @param config - Configuration for rollback behavior * @returns This transaction for chaining * @example * // Manual rollback * const tx = createTransaction({ mutationFn: async () => { * // Send to API * }}) *
(config?: { isSecondaryRollback?: boolean })
| 408 | * } |
| 409 | */ |
| 410 | rollback(config?: { isSecondaryRollback?: boolean }): Transaction<T> { |
| 411 | const isSecondaryRollback = config?.isSecondaryRollback ?? false |
| 412 | if (this.state === `completed`) { |
| 413 | throw new TransactionAlreadyCompletedRollbackError() |
| 414 | } |
| 415 | |
| 416 | this.setState(`failed`) |
| 417 | |
| 418 | // See if there's any other transactions w/ mutations on the same ids |
| 419 | // and roll them back as well. |
| 420 | if (!isSecondaryRollback) { |
| 421 | const mutationIds = new Set() |
| 422 | this.mutations.forEach((m) => mutationIds.add(m.globalKey)) |
| 423 | for (const t of transactions) { |
| 424 | t.state === `pending` && |
| 425 | t.mutations.some((m) => mutationIds.has(m.globalKey)) && |
| 426 | t.rollback({ isSecondaryRollback: true }) |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | // Reject the promise |
| 431 | this.isPersisted.reject(this.error?.error) |
| 432 | this.touchCollection() |
| 433 | |
| 434 | return this |
| 435 | } |
| 436 | |
| 437 | // Tell collection that something has changed with the transaction |
| 438 | touchCollection(): void { |
no test coverage detected