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 ()
=> {
44 + runtimes.
splice(
0).
map((runtime)
=> runtime.
stop(
"test cleanup")),
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",
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);
68 + it(
"is safe to stop twice",
async ()
=> {
69 + const runtime
= await createUiRuntime({
70 + sessionLabel:
"ui: test",
71 + address:
"127.0.0.1",
74 + await runtime.
stop(
"first");
75 + await runtime.
stop(
"second");
76 + expect(runtime.
status().active).
toBe(
false);
extensions/ui/runtime.ts
Addednew 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 = {
10 + state
: UiServerState | "starting" | "stopped" | "error";
15 +export type UiRuntime = {
16 + status()
: UiRuntimeStatus;
17 + widgetLines()
: string[];
18 + redrawQr()
: Promise<
void>;
19 + open()
: Promise<
void>;
20 + stop(reason
: string)
: Promise<
void>;
23 +export type CreateUiRuntimeOptions = {
24 + sessionLabel
: string;
26 + preferredPort
?: number;
27 + openBrowser
?: boolean;
28 + onChange?: ()
=> void;
31 +export async function createUiRuntime(
32 + options
: CreateUiRuntimeOptions,
33 +)
: Promise<
UiRuntime> {
34 + let state
: UiRuntimeStatus = { active:
false, state:
"starting", clients:
0 };
36 + let server
: UiServer | undefined;
38 + const address
= options.address
?? chooseLanAddress();
39 + server
= await startUiServer({
41 + preferredPort: options.preferredPort,
42 + onClientState: ()
=> {
43 + state
= currentStatus();
44 + options.
onChange?.();
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 };
54 + active: server.
state()
!== "stopped",
55 + url: server.publicUrl,
56 + state: server.
state(),
57 + clients: server.
clients(),
62 + status: ()
=> state,
63 + widgetLines: ()
=> {
64 + if (
!server
|| !state.url)
return [];
65 + return renderUiWidget({
69 + sessionLabel: options.sessionLabel,
70 + clients: state.clients,
71 + lastError: state.lastError,
75 + if (
!server)
return;
76 + qr
= await renderQr(server.publicUrl);
77 + state
= currentStatus();
78 + options.
onChange?.();
81 + if (
!server)
return;
82 + await openBrowser(server.publicUrl);
84 + async stop(reason
: string) {
85 + const current
= server;
87 + state
= { active:
false, state:
"stopped", clients:
0 };
88 + options.
onChange?.();
89 + if (current)
await current.
close(reason);