/** * An ownership claim moved out of an {@link Owner}. * * @public */ export class OwnerMoved { private disposing?: Promise; constructor( /** The owned value. */ readonly value: T, private readonly disposer: (value: T) => PromiseLike | void, ) {} /** Disposes this ownership claim. */ dispose(): Promise { this.disposing ??= Promise.resolve().then(() => this.disposer(this.value)); return this.disposing; } } const ownerId = Symbol.for("@ckb-ccc/core.Owner"); /** * Owns the lifecycle of a value. * * @example * ```ts * class ConnectionOwner extends Owner { * constructor(protected readonly value_: Connection) { * super(); * } * * protected release(connection: Connection): Promise { * return connection.close(); * } * } * ``` * * @public */ export abstract class Owner { protected abstract readonly value_: T; private readonly [ownerId] = true; private disposing?: Promise; /** Whether this ownership claim has not been moved or disposed. */ get isValid(): boolean { return this.disposing === undefined; } /** Returns whether a value implements CCC Owner semantics. */ static is(value: unknown): value is Owner { return ( typeof value === "object" && value !== null && (value as { [ownerId]?: unknown })[ownerId] === true ); } /** * The owned value. * * @throws If this ownership has been moved or disposed. */ get value(): T { if (this.disposing) { throw new Error("Cannot access a moved or disposed Owner"); } return this.value_; } /** * Releases this ownership claim. * * Implementations decide whether this disposes the value immediately or, * for example, only decrements a reference count. */ protected abstract release(value: T): PromiseLike | void; /** * Disposes this ownership claim. Repeated calls are safe. */ dispose(): Promise { this.disposing ??= Promise.resolve().then(() => this.release(this.value_)); return this.disposing; } /** * Maps the owned value while transferring this ownership claim. * * The mapper runs before the claim is moved, so a mapper failure leaves this * Owner active. After a successful mapping, this Owner is invalidated and the * returned Owner retains its release semantics. */ map(mapper: (value: T) => U): Owner { const value = mapper(this.value); return new OwnerMapped(value, this.move()); } /** * Moves the ownership claim out of this owner without disposing it. * * Access is invalidated synchronously and the moved claim retains this * Owner's release semantics. * * @throws If this owner no longer has an ownership claim. */ protected move(): OwnerMoved { if (this.disposing) { throw new Error("Owner has already been moved or disposed"); } this.disposing = Promise.resolve(); return new OwnerMoved(this.value_, (value) => this.release(value)); } /** * Moves an ownership claim between Owner implementations. * * @throws If the source owner has already been moved or disposed. */ protected static moveFrom(owner: Owner): OwnerMoved { return owner.move(); } } class OwnerMapped extends Owner { constructor( protected readonly value_: TValue, private readonly source: OwnerMoved, ) { super(); } protected release() { return this.source.dispose(); } }