# `Tx.transaction`

> Contract 2.0.0 · package sha256 `a857eca95b35921109faf997094bb4192f12527d9d0fcd4be7e97f06425bad6e` · generated from `configurator-runtime.d.ts` by `services/onsite-scripter/scripts/build-contract-docs.mjs`; edit the source, not this file.

Method of [`Tx`](Tx.md) · stable · since 2.0.0

```ts
transaction<T>(work: (sp: Tx) => T | Promise<T>, options?: SignalOption): Promise<TransactionReport<T>>;
```

A savepoint. Writes through `sp` are its own; its throw undoes only them, then rejects with
that error: each path they wrote takes the latest write to it that no rolled-back savepoint
made, or its value when the transaction opened (spec section 6.1, O7). A node, rule, item or
connection that no surviving write created goes whole, with every path under it, and a
surviving write under it is dropped (O7). It has no timeout,
label or revision. `sp` closes when the savepoint settles: a later write through it throws
HoloTransactionClosedError. An enlisted rejection its `work` never observed fails it, as a
throw would. Outcome: 'staged', with `value`.

## Options

| Option | Type | Default | What it does |
| --- | --- | --- | --- |
| `signal` | `AbortSignal` | undefined | Cancels this call; the member's JSDoc says what an abort does. |

## Abort (`signal`)

This call is a write (`SignalOption.signal`, [the kinds of call](../read-this-first.md#conventions)).

> A write: before the commit point it rolls back and rejects AbortError; after it, no effect, except that a running camera move stops where the last frame drew it (`CameraMoveOptions`).
>
> Through a `tx` the commit point is the transaction's: aborting a write still loading rejects AbortError and stages nothing (catch it: unobserved, it rolls the transaction back, spec section 6.1, O6); aborting one already staged unstages it, its paths keeping the latest other write to them as after a failed savepoint (O7), and its Promise stays resolved 'staged'.

## Example

Imports: values (`has`, `validate`, the `Holo*Error` classes, the pure helpers) from `@holostep-configurators/runtime-client`, types from `@holostep-configurators/runtime-client/contract`, `/math` and `/testing` names from those entries; `boot` from the runtime module the root's `loadRuntimeAtFloor` resolves to. A type your page defines is your app's: declare it there, never import it from the package.

Compiled under the contract's strict settings (`examples/declaration/05-Tx.transaction.ts`):

```ts
import type { DeepReadonly, SceneModel, Session } from '@holostep-configurators/runtime-client/contract';
declare const session: Session;
declare const signal: AbortSignal;
// For an app whose scene model is its source of truth (a store app: spec section 11.2).
declare function withSofa(model: DeepReadonly<SceneModel> | null): SceneModel;
declare function nodeIdsOf(model: DeepReadonly<SceneModel> | null): ReadonlySet<string>;
declare const pillowUrl: string;
await session.transaction(async (tx) => {
  // inside `work`, read through `tx`, never `session`: a queued write's target reads unspecified
  const had = nodeIdsOf(tx.getSceneModel());
  const set = await tx.setSceneModel(withSofa(tx.getSceneModel()));
  // The sofa is all or nothing. A node the model already held lists its own failure too.
  if (set.problems.some((p) => p.code === 'asset-failed' && p.node && !had.has(p.node))) {
    throw new Error('sofa not loaded');    // everything rolls back; transaction() rejects it
  }
  try {
    await tx.transaction(async (sp) => {                   // an optional extra, as a savepoint
      await sp.collection('pillows').load(pillowUrl, { key: 'pillow-1' });
    });
  } catch {
    // The load failed: only the savepoint's writes were undone. The sofa still commits.
  }
}, { undo: 'Add sofa', origin: 'catalogue' });
```

## Generic-API test (spec 3.1)

| Rule | Result |
| --- | --- |
| 1. Neutral name | pass: `transaction` names no product |
| 2. A precedent | pass (through its owner [`Tx`](Tx.md)): [Unity AnimationClip.SampleAnimation](https://docs.unity3d.com/ScriptReference/AnimationClip.SampleAnimation.html) (row `G36`) |
| 3. Two apps, or labs | pass: 18 apps (ledger: app-f9a32142f1; fleet cross-check: app-04c2774145, app-37356bf09f, app-4762caacdc, app-4fd23bc869, app-510d750508, app-822bb9d6aa, app-8675b3225c, app-bcb7ef21da and 9 more) |
| 4. The cheaper fix first | decided per need in Appendix B: rows `G36` (through its owner [`Tx`](Tx.md)) at tier stable (a member only where no rule, data, asset or app code serves the need) |
