Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .changeset/rename-public-types.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
"rollbackit": patch
---

Rename two public types so they match the `Rollback*` naming of their siblings and signal their scope:

- `FailedRollback` → `RollbackFailure` (a failure record, grouping with `RollbackResult`).
- `OperationOptions` → `RollbackOperationOptions` (the per-operation options on a `RollbackOperation`, distinct from the run-level `RollbackOptions`).

The old names are removed (no consumers yet).
2 changes: 1 addition & 1 deletion CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ The entire library is a handful of small files — `src/lib/` (`operations.ts`,

1. **`commit()` seals a *batch*, it does not finalize the instance.** It just clears the ops array (`ops.length = 0`) and leaves the instance open. Registering after a commit is *allowed* and starts a fresh batch — a later `rollback()` only unwinds operations added since the most recent commit. This is the "progressive commit" feature; don't regress it into a one-shot finalize.
2. **`rollback()` finalizes** by setting `rolledBack = true`. After that, `add()` throws `RolledBackError`, and repeat `rollback()`/`commit()` calls are safe no-ops. Only `add`-after-`rollback` throws.
3. **Rollback runs newest-first and is failure-tolerant by default.** `runRollback` iterates the ops array in reverse, collecting throwing operations into `result.failures` and continuing. A run-level `stopOnFailure: true` halts at the first failure and returns the older, un-run operations in `result.pending` (in registration order). There is also a *per-operation* `stopOnFailure`, set via the third arg to `add` (`OperationOptions`): the unwind halts only if *that* operation's rollback throws — `runRollback` stops on `stopOnFailure || op.stopOnFailure`, so the per-operation flag only matters when the run-level flag is `false`. Rollbacks always run sequentially — never parallelize the loop; concurrency is the caller's job inside a single `add`.
3. **Rollback runs newest-first and is failure-tolerant by default.** `runRollback` iterates the ops array in reverse, collecting throwing operations into `result.failures` and continuing. A run-level `stopOnFailure: true` halts at the first failure and returns the older, un-run operations in `result.pending` (in registration order). There is also a *per-operation* `stopOnFailure`, set via the third arg to `add` (`RollbackOperationOptions`): the unwind halts only if *that* operation's rollback throws — `runRollback` stops on `stopOnFailure || op.stopOnFailure`, so the per-operation flag only matters when the run-level flag is `false`. Rollbacks always run sequentially — never parallelize the loop; concurrency is the caller's job inside a single `add`.

### Errors

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ Creates a rollback instance.

| Field | Type | Description |
| --- | --- | --- |
| `failures` | `readonly FailedRollback[]` | Operations that threw while rolling back (`{ description, error }`). |
| `failures` | `readonly RollbackFailure[]` | Operations that threw while rolling back (`{ description, error }`). |
| `pending` | `readonly RollbackOperation[]` | Operations never run because `stopOnFailure` halted early (carries the `rollback` fns, so you can log or retry them). Empty unless an early stop occurred. |

### `withRollback<T>(fn, options?): Promise<T>`
Expand Down
5 changes: 3 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
export { RollbackError, RolledBackError } from "./lib/errors";
export { RolledBackError } from "./lib/errors";
export { withRollback } from "./lib/helpers";
export { createRollback } from "./lib/operations";
export type {
FailedRollback,
Rollback,
RollbackFailure,
RollbackOperation,
RollbackOperationOptions,
RollbackOptions,
RollbackResult,
WithRollbackOptions,
Expand Down
2 changes: 1 addition & 1 deletion src/lib/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const createMessage = (message: string) => `${PREFIX} ${message}`;
* and `name` reflects the concrete subclass so `instanceof` and stack traces
* report the right type.
*/
export class RollbackError extends Error {
class RollbackError extends Error {
constructor(message: string, options?: ErrorOptions) {
super(createMessage(message), options);
this.name = new.target.name;
Expand Down
4 changes: 2 additions & 2 deletions src/lib/operations.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type {
FailedRollback,
Rollback,
RollbackFailure,
RollbackOperation,
RollbackResult,
} from "../types";
Expand All @@ -22,7 +22,7 @@ const runRollback = async (
ops: RollbackOperation[],
stopOnFailure: boolean,
): Promise<RollbackResult> => {
const failures: FailedRollback[] = [];
const failures: RollbackFailure[] = [];

// run the rollback operations in reverse order
for (let i = ops.length - 1; i >= 0; i--) {
Expand Down
14 changes: 7 additions & 7 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
* Per-operation options, set via the third argument to `add`.
*
* `stopOnFailure` here is the operation-level counterpart of the run-level
* {@link RollbackOptions.stopOnFailure}: when *this* operation's rollback
* {@link RollbackOptions#stopOnFailure}: when *this* operation's rollback
* throws, the unwind halts and the older operations are returned as `pending`.
* It only matters when the run-level flag is `false`; a run-level `true` halts
* on every failure regardless.
*/
export type OperationOptions = Pick<RollbackOptions, "stopOnFailure">;
export type RollbackOperationOptions = Pick<RollbackOptions, "stopOnFailure">;

/**
* A rollback operation.
Expand All @@ -21,12 +21,12 @@ export type RollbackOperation = {
* The rollback function.
*/
rollback: () => Promise<void>;
} & OperationOptions;
} & RollbackOperationOptions;

/**
* A failed rollback operation.
* A failure record for a rollback operation that threw while unwinding.
*/
export type FailedRollback = {
export type RollbackFailure = {
/**
* The description of the failed rollback operation.
*/
Expand All @@ -44,7 +44,7 @@ export type RollbackResult = {
/**
* Operations that threw while rolling back.
*/
failures: readonly FailedRollback[];
failures: readonly RollbackFailure[];
/**
* Operations that were never run because `stopOnFailure` halted the
* sequence early. Carries the `rollback` functions, so the caller can log,
Expand Down Expand Up @@ -84,7 +84,7 @@ export type Rollback = {
add: (
description: string,
rollback: () => Promise<void>,
options?: OperationOptions,
options?: RollbackOperationOptions,
) => void;
/**
* Executes the rollback operations registered since the last `commit`, in
Expand Down
Loading