# `Writer.collection`

> 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 [`Writer`](Writer.md) · inherited by [`Tx`](Tx.md), [`Session`](Session.md), [`testing.FakeSession`](testing.FakeSession.md) · stable · since 2.0.0 · called as `session.collection`

```ts
collection(name: string, options?: CollectionOptions): Collection<C>;
```

The live collection with this name, bound to this writer; created when absent. A lifecycle
call: immediate, never staged or queued. A new one reads `{ role, items: [] }` in
[`state().look.collections`](LookState.collections.md) from this call, and creating it commits nothing (no `seq`, no
event, no feed entry). `options` apply at creation only ([`CollectionOptions`](CollectionOptions.md));
to create it anew with other options, `dispose()` it first. Errors: throws HoloInputError
'collection-role-mismatch' or 'collection-options-mismatch' when the name is live with a
different [`role`](Collection.role.md) or `attachTo` (compared by value; a [`Part`](Part.md) as the part it stands for).

## Options

Through [`Session`](Session.md):

| Option | Type | Default | What it does |
| --- | --- | --- | --- |
| `attachTo` | `Query` | undefined (world) | Follow this part: re-attached after reloads. |
| `role` | `Role` | 'product' (a new collection) | Applies at creation only; on a live name, an omitted role is no check and a different one throws 'collection-role-mismatch'. |
| `signal` | `AbortSignal` | undefined | Aborting disposes the collection; already aborted, the new collection is born disposed ([`SignalOption`](SignalOption.md)). |

Through [`Tx`](Tx.md):

| Option | Type | Default | What it does |
| --- | --- | --- | --- |
| `attachTo` | `Query` | undefined (world) | Follow this part: re-attached after reloads. |
| `role` | `Role` | 'product' (a new collection) | Applies at creation only; on a live name, an omitted role is no check and a different one throws 'collection-role-mismatch'. |
| `signal` | `AbortSignal` | undefined | Aborting disposes the collection; already aborted, the new collection is born disposed ([`SignalOption`](SignalOption.md)). |

## Abort (`signal`)

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

> A creator (`collection` for a new name, `materials.create`, `on`, `onFrame`, `beforeCommit`, `follow`, `input.drag`, `labs.lightLinking`) given a signal already aborted returns at once, already ended: a `Collection` or `Material` reading `disposed: true` that holds no content and no live name, or a registration that never runs and whose disposer does nothing.
>
> It records no Problem for the abort and throws only what it would throw anyway, as the DOM's `addEventListener` with an aborted signal adds nothing (spec section 6.5).

## Generic-API test (spec 3.1)

| Rule | Result |
| --- | --- |
| 1. Neutral name | pass: `collection` names no product |
| 2. A precedent | pass: [Unity Mesh.SetVertices](https://docs.unity3d.com/ScriptReference/Mesh.SetVertices.html) (row `G63`); [glTF 2.0 node hierarchy](https://registry.khronos.org/glTF/specs/2.0/glTF-2.0.html#nodes-and-hierarchy) (row `K1`); [Unity Object.Destroy](https://docs.unity3d.com/ScriptReference/Object.Destroy.html) (row `K1`) |
| 3. Two apps, or labs | pass: 28 apps (ledger: app-04c2774145, app-0c773a4ee3, app-37356bf09f, app-378d190f1b, app-4762caacdc, app-4f16d2f6da, app-4fd23bc869, app-510d750508 and 20 more) |
| 4. The cheaper fix first | decided per need in Appendix B: rows `G63`, `K1` at tier app, stable (a member only where no rule, data, asset or app code serves the need) |
