/** * `Scope` — engine-agnostic nested-lifetime primitive, generalizing the * Connection/Disposable semantics in this package (foundation.md §4). * * A `Scope` owns a single "lifetime segment". Resources bound via `own()` and * sub-scopes created via `child()` are released together — both on `reset()` * (soft reuse: dispose the segment, open a fresh one, stay alive) and `close()` * (terminal: dispose everything, die). Teardown is LIFO and, crucially, * `reset()`/`close()` are COMPLETION FENCES: their Promise resolves (and the * `'reset'`/`'closed'` listeners fire) only AFTER the underlying async * disposeAll has fully finished — unlike `connection.ts`, which fires the * teardown and forgets (`void ...disposeAll()`). * * Cross-layer LIFO: a scope's teardown disposes its child sub-scopes (deepest * first, recursively) BEFORE its own directly-owned resources, so a grandchild * tears down before a child, which tears down before the root. */ import { type Disposable } from './disposable.js'; export interface Scope { readonly alive: boolean; /** Bind a resource to the current lifetime segment; released by both reset() * and close() (LIFO). The returned Disposable releases it early (once). After * the scope is closed, the resource is disposed immediately (leak protection) * and a no-op handle is returned. */ own(d: Disposable | (() => void)): Disposable; /** Create a sub-scope bound to the current segment: a parent reset()/close() * cascades into it. A child close() does not affect the parent. */ child(): Scope; /** Soft reuse: await LIFO disposeAll of the current segment (children first, * then owned resources — async disposers truly complete), THEN open a fresh * segment and fire 'reset'. Scope stays alive. */ reset(): Promise; /** Terminal: await LIFO disposeAll of the segment, mark dead, fire 'closed' * once. Idempotent. */ close(): Promise; /** Subscribe to a lifecycle event; returns an unsubscribe Disposable. */ on(event: 'reset' | 'closed', cb: () => void): Disposable; /** * Re-parent `child` from THIS scope's current segment onto `newParent`'s * current segment WITHOUT resetting or closing it. The child's own()ed * resources stay live and neither 'reset' nor 'closed' fire on it; only the * cascade ownership moves (who tears it down from now on). * * If `this` or `newParent` has a teardown in flight, adopt WAITS for that * fence (it does not throw), then re-reads/re-validates against the fresh * segment. Validation failures (dead this/newParent, non-direct-child, cycle) * reject. On every path the child stays attached to EXACTLY one segment. */ adopt(child: Scope, newParent: Scope): Promise; } export declare function createScope(): Scope; //# sourceMappingURL=scope.d.ts.map