Something went wrong. Try again.
Monorepo for the nibling.run platform
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041import { localUrl } from "./process-table.ts";import type { Sandbox } from "./sandbox.ts";import { keyOf } from "./shell/kinds.ts";import type { PanelId } from "./shell/layout.ts";import type { Unsubscribe, Workbench } from "./shell/workbench.ts";import { openBrowser } from "./views/browser.ts";
/** * Opens a browser panel when a port starts listening and none shows it, binding a preview that waits * for a server first when there is one. A port whose auto-opened or auto-bound panel the person closed * is left alone for the rest of the page's life; a port listening again with its panel open needs * nothing here, since the panel reloads itself. */export function openListeningPorts(sandbox: Sandbox, workbench: Workbench): Unsubscribe { const opened = new Map<number, PanelId>(); const dismissed = new Set<number>(); let stopped = false; let offListening = () => {}; void sandbox.kernel.then((kernel) => { if (stopped) return; offListening = kernel.onListening((port) => { const url = localUrl(port); const key = keyOf({ kind: "browser", input: { url } }); if (dismissed.has(port) || workbench.panels("browser").some((panel) => keyOf(panel) === key)) return; opened.set(port, openBrowser(workbench, url)); }); }, () => {}); const offLayout = workbench.subscribe((layout) => { for (const [port, panel] of opened) { if (panel in layout.panels) continue; opened.delete(port); dismissed.add(port); } }); return () => { stopped = true; offListening(); offLayout(); };}