/** * The per-shell windows hub (API §10). `ViewStage` mounts once near the root while * `ViewHost`s live anywhere, so they rendezvous through a hub keyed by the shared * `Shell` instance (a `WeakMap`), not context. It owns the `WindowRegistry` * (reactive store behind `useWindows`/`useWindow`, fed by the hosts), the stage * controller (the slot layer, published by the stage), and the adopt stash. */ import type { Shell, Window } from '@astrale-os/shell' import type { AdoptRecord } from './adopt.js' import type { WindowState } from './lifecycle/states.js' import type { StageController } from './placement.js' import { createAdoptStash } from './adopt.js' // ── Window registry (reactive store behind useWindows/useWindow) ───────────── /** A host-tracked window: the shell `Window` plus the phase the host observes. */ interface WindowEntry { readonly window: Window readonly state: WindowState } /** A `useWindow` projection — a window's phase plus its shell `Window` (or none). */ export type WindowView = WindowState & { readonly window: Window | null } export interface WindowRegistry { /** Referentially stable between mutations (useSyncExternalStore contract). */ getSnapshot(): ReadonlyMap /** Stable projection for one window id (null id → a stable "no window"). */ select(windowId: string | null): WindowView subscribe(onChange: () => void): () => void /** Host writes: register/transition/drop a window. */ upsert(windowId: string, window: Window, state: WindowState): void patch(windowId: string, state: WindowState): void remove(windowId: string): void /** Shell writes: replace host and imperatively mounted windows from authoritative snapshots. */ sync(windows: ReadonlyMap): void } const NO_WINDOW: WindowView = { phase: 'closed', windowId: '', window: null } function createWindowRegistry(): WindowRegistry { const entries = new Map() const listeners = new Set<() => void>() // Cached snapshots: rebuilt only on mutation, so repeated getSnapshot/select // calls between changes return identical references (no render loop). let windowsSnapshot: ReadonlyMap | null = null let selectCache = new Map() function invalidate() { windowsSnapshot = null selectCache = new Map() for (const l of listeners) l() } return { getSnapshot() { if (windowsSnapshot === null) { const map = new Map() for (const [id, entry] of entries) map.set(id, entry.window) windowsSnapshot = map } return windowsSnapshot }, select(windowId) { if (windowId === null) return NO_WINDOW const cached = selectCache.get(windowId) if (cached !== undefined) return cached const entry = entries.get(windowId) const view: WindowView = entry ? { ...entry.state, window: entry.window } : { phase: 'closed', windowId, window: null } selectCache.set(windowId, view) return view }, subscribe(onChange) { listeners.add(onChange) return () => { listeners.delete(onChange) } }, upsert(windowId, window, state) { entries.set(windowId, { window, state }) invalidate() }, patch(windowId, state) { const entry = entries.get(windowId) if (entry === undefined) return entries.set(windowId, { window: entry.window, state }) invalidate() }, remove(windowId) { if (!entries.delete(windowId)) return invalidate() }, sync(windows) { let changed = false for (const windowId of entries.keys()) { if (!windows.has(windowId)) { entries.delete(windowId) changed = true } } for (const [windowId, window] of windows) { const current = entries.get(windowId) if (current?.window === window) continue entries.set(windowId, { window, state: current?.state ?? phaseFor(window), }) changed = true } if (changed) invalidate() }, } } function phaseFor(window: Window): WindowState { if (window.state === 'opening') return { phase: 'mounting' } if (window.state === 'failed') { return { phase: 'crashed', windowId: window.windowId } } if (window.state === 'closed') return { phase: 'closed', windowId: window.windowId } return { phase: 'ready', windowId: window.windowId } } // ── Per-shell hub ──────────────────────────────────────────────────────────── export interface StageHub { readonly registry: WindowRegistry getController(): StageController | null /** Publish (stage mount) or retract (stage unmount) the slot layer. */ setController(controller: StageController | null): void /** Await the controller — fires immediately if one is already published. */ onController(cb: (controller: StageController | null) => void): () => void /** Stash an unmounting host's live window; disposes after one macrotask if unadopted. */ stashForAdopt(key: string, record: AdoptRecord): void /** Reclaim a stashed window for a remount of the same source, cancelling its dispose. */ takeAdopt(key: string): AdoptRecord | null } const HUBS = new WeakMap() /** The windows hub for a shell — one per shell object, created on first touch. */ export function getStageHub(shell: Shell): StageHub { const existing = HUBS.get(shell) if (existing !== undefined) return existing const registry = createWindowRegistry() let controller: StageController | null = null const waiters = new Set<(c: StageController | null) => void>() const adopt = createAdoptStash() const hub: StageHub = { registry, getController() { return controller }, setController(next) { controller = next for (const w of waiters) w(next) }, onController(cb) { waiters.add(cb) if (controller !== null) cb(controller) return () => { waiters.delete(cb) } }, stashForAdopt(key, record) { adopt.stash(key, record) }, takeAdopt(key) { return adopt.take(key) }, } registry.sync(shell.windows) shell.onWindowChange(() => registry.sync(shell.windows)) HUBS.set(shell, hub) return hub }