repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by bugabinga

Add ui runtime lifecycle

d7e4fabf420e38fa758d9582f0b46de57c953d54

Oliver Krylow <o.krylow@isp-insoft.de> ·

unsigned

Browse filesCompare with HEAD

Changed files (2)

extensions/ui/__tests__/runtime.test.ts

Modified
1 -import { describe, expect, it } from "vitest";
1 +import { afterEach, describe, expect, it } from "vitest";
2 2 import { openerCandidates } from "../open";
3 +import { createUiRuntime, type UiRuntime } from "../runtime";
4 5 describe("ui browser opener", () => {
5 6 it("uses platform openers", () => {
40 +const runtimes: UiRuntime[] = [];
42 +afterEach(async () => {
43 + await Promise.all(
44 + runtimes.splice(0).map((runtime) => runtime.stop("test cleanup")),
45 + );
46 +});
48 +describe("ui runtime", () => {
49 + it("starts, reports status, redraws QR, and stops", async () => {
50 + const runtime = await createUiRuntime({
51 + sessionLabel: "ui: test",
52 + address: "127.0.0.1",
53 + openBrowser: false,
54 + });
55 + runtimes.push(runtime);
57 + expect(runtime.status().active).toBe(true);
58 + expect(runtime.status().url).toContain("token=");
59 + expect(runtime.widgetLines().join("\n")).toContain("ui: test");
61 + await runtime.redrawQr();
62 + expect(runtime.widgetLines().join("\n")).toContain("mobile qr");
64 + await runtime.stop("test");
65 + expect(runtime.status().active).toBe(false);
66 + });
68 + it("is safe to stop twice", async () => {
69 + const runtime = await createUiRuntime({
70 + sessionLabel: "ui: test",
71 + address: "127.0.0.1",
72 + openBrowser: false,
73 + });
74 + await runtime.stop("first");
75 + await runtime.stop("second");
76 + expect(runtime.status().active).toBe(false);
77 + });
78 +});

extensions/ui/runtime.ts

Added
new file mode 100644
1 +import { chooseLanAddress } from "./network";
2 +import { openBrowser } from "./open";
3 +import { renderQr } from "./qr";
4 +import { startUiServer, type UiServer, type UiServerState } from "./server";
5 +import { renderUiWidget } from "./terminal-widget";
7 +export type UiRuntimeStatus = {
8 + active: boolean;
9 + url?: string;
10 + state: UiServerState | "starting" | "stopped" | "error";
11 + clients: number;
12 + lastError?: string;
13 +};
15 +export type UiRuntime = {
16 + status(): UiRuntimeStatus;
17 + widgetLines(): string[];
18 + redrawQr(): Promise<void>;
19 + open(): Promise<void>;
20 + stop(reason: string): Promise<void>;
21 +};
23 +export type CreateUiRuntimeOptions = {
24 + sessionLabel: string;
25 + address?: string;
26 + preferredPort?: number;
27 + openBrowser?: boolean;
28 + onChange?: () => void;
29 +};
31 +export async function createUiRuntime(
32 + options: CreateUiRuntimeOptions,
33 +): Promise<UiRuntime> {
34 + let state: UiRuntimeStatus = { active: false, state: "starting", clients: 0 };
35 + let qr = "";
36 + let server: UiServer | undefined;
38 + const address = options.address ?? chooseLanAddress();
39 + server = await startUiServer({
40 + address,
41 + preferredPort: options.preferredPort,
42 + onClientState: () => {
43 + state = currentStatus();
44 + options.onChange?.();
45 + },
46 + });
47 + qr = await renderQr(server.publicUrl);
48 + state = currentStatus();
49 + if (options.openBrowser !== false) await openBrowser(server.publicUrl);
51 + function currentStatus(): UiRuntimeStatus {
52 + if (!server) return { active: false, state: "stopped", clients: 0 };
53 + return {
54 + active: server.state() !== "stopped",
55 + url: server.publicUrl,
56 + state: server.state(),
57 + clients: server.clients(),
58 + };
59 + }
61 + return {
62 + status: () => state,
63 + widgetLines: () => {
64 + if (!server || !state.url) return [];
65 + return renderUiWidget({
66 + url: state.url,
67 + qr,
68 + state: state.state,
69 + sessionLabel: options.sessionLabel,
70 + clients: state.clients,
71 + lastError: state.lastError,
72 + });
73 + },
74 + async redrawQr() {
75 + if (!server) return;
76 + qr = await renderQr(server.publicUrl);
77 + state = currentStatus();
78 + options.onChange?.();
79 + },
80 + async open() {
81 + if (!server) return;
82 + await openBrowser(server.publicUrl);
83 + },
84 + async stop(reason: string) {
85 + const current = server;
86 + server = undefined;
87 + state = { active: false, state: "stopped", clients: 0 };
88 + options.onChange?.();
89 + if (current) await current.close(reason);
90 + },
91 + };