import { NAME, UI } from "@commontools/runner/shared"; import { $conn, type RuntimeClient } from "./runtime-client.ts"; import { PageRef, RequestType } from "./protocol/mod.ts"; import { InitializedRuntimeConnection } from "./client/connection.ts"; import { VNode } from "./vnode-types.ts"; import { CellHandle } from "./cell-handle.ts"; export type PageType = { [NAME]?: CellHandle | string; [UI]?: CellHandle | VNode; }; export class PageHandle { private _conn: InitializedRuntimeConnection; private _cell: CellHandle; constructor( rt: RuntimeClient, ref: PageRef, ) { this._conn = rt[$conn](); this._cell = new CellHandle(rt, ref.cell); } cell(): CellHandle { return this._cell; } id(): string { return this._cell.id(); } name(): string | undefined { const data = this._cell.get() as Record | undefined; if (data && typeof data === "object" && NAME in data) { return data[NAME] as string; } } async start(): Promise { const res = await this._conn.request({ type: RequestType.PageStart, pageId: this.id(), }); return res.value; } async stop(): Promise { const res = await this._conn.request({ type: RequestType.PageStop, pageId: this.id(), }); return res.value; } } export function isPageHandle( value: unknown, ): value is PageHandle { return value instanceof PageHandle; }