import * as Service from "../plan/service"; import type { Room } from "../rooms"; type OpeningRoom

= { plan?: P; opening?: Promise

}; /** Complete all setup before exposing a plan to concurrent socket frames. */ export async function publishOpenedPlan

( room: OpeningRoom

, opened: P, prepare: () => Promise | void, ): Promise

{ await prepare(); room.plan = opened; return opened; } /** Preparation owns an unpublished document until close intent has been checked. */ export async function prepareOpenedPlan( room: Room, opened: Service.Plan, prepare: () => Promise | void, ): Promise { try { if (room.closing) throw new Error("document is unavailable"); await prepare(); if (room.closing) throw new Error("document is unavailable"); room.plan = opened; return opened; } catch (error) { try { await Service.abortOpening(opened); } catch (cleanupError) { let failure = new AggregateError( [error, cleanupError], "document preparation and cleanup failed", { cause: cleanupError, }, ); throw failure; } throw error; } }