Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

DesktopBrowserHost.ts

613 lines23.6 KB

1// @effect-diagnostics nodeBuiltinImport:off - Names download files on the shared disk.
2/** The manager owns the single debugger attachment; the relay shares it. */
3import {
4 DesktopBrowserCommand,
5 DesktopBrowserEvent,
6 type DesktopBrowserEvent as DesktopBrowserEventType,
7 type PreviewAutomationLoadError,
8} from "@t3tools/contracts";
9import * as Context from "effect/Context";
10import * as NodePath from "node:path";
11import * as Effect from "effect/Effect";
12import * as Fiber from "effect/Fiber";
13import * as Layer from "effect/Layer";
14import * as Option from "effect/Option";
15import * as PubSub from "effect/PubSub";
16import * as Schema from "effect/Schema";
17import * as Stream from "effect/Stream";
18​
19import { createCdpRelayConnection, type CdpRelayConnection } from "./CdpRelay.ts";
20import { sendPageCommand } from "./GuestInput.ts";
21​
22const encodeEvent = Schema.encodeSync(Schema.fromJsonString(DesktopBrowserEvent));
23const decodeCommand = Schema.decodeUnknownOption(Schema.fromJsonString(DesktopBrowserCommand));
24const lineEncoder = new TextEncoder();
25const AGENT_INPUT_COMMAND =
26 /"method":"(?:Input\.|Page\.navigate"|Runtime\.(?:evaluate|callFunctionOn)")/;
27/** The same commands as above, by method, for the relay's sends. */
28const AGENT_INPUT_METHOD = /^(?:Input\.|Page\.navigate$|Runtime\.(?:evaluate|callFunctionOn)$)/;
29const AGENT_DOWNLOAD_WINDOW_MS = 5_000;
30const FOCUS_IDLE = "5 seconds";
31/** Well inside the server's 10 s DESKTOP_ATTACH_TIMEOUT, for a first page that never reports it is up. */
32const FIRST_PAGE_TIMEOUT = "5 seconds";
33const ERR_ABORTED = -3;
34​
35export interface DesktopBrowserTabKey {
36 readonly threadId: string;
37 readonly tabId: string;
39​
40export interface DesktopBrowserTabDebugger {
41 readonly webContents: Electron.WebContents;
42 readonly debugger: Electron.Debugger;
44​
45const keyOf = ({ threadId, tabId }: DesktopBrowserTabKey) => `${threadId}\u0000${tabId}`;
46​
47interface AttachedTab {
48 readonly key: DesktopBrowserTabKey;
49 readonly debuggee: DesktopBrowserTabDebugger;
50 relay: CdpRelayConnection | null;
51 screenshotTurn: object | null;
52 /** Each keeps the guest painting while whoever queued it still waits, and focused while `held`. */
53 readonly waitingScreenshots: Array<{
54 readonly start: () => void;
55 readonly live: () => boolean;
56 held: boolean;
57 }>;
58 downloadDirectory: string | null;
59 pendingDownloadGuid: string | null;
60 agentInputAt: number;
61 focusEmulated: boolean;
62 focusChange: Promise<void> | null;
63 focusUses: number;
64 /** Each pending command, by the extra session waiting on it; the main connection's have none. */
65 readonly focusPending: Map<object, string | undefined>;
66 /** Screenshots Chromium was sent and has not answered; each keeps its wake lock until then. */
67 readonly capturing: Set<object>;
68 /** Whether the client keeps the guest painting, as published on `painting`. */
69 painting: boolean;
70 /**
71 * Driven by the agent since its thread's last turn ended or the guest last throttled. A
72 * turn's start can reach us after its first command, so this holds focus once it does.
73 */
74 agentUsed: boolean;
75 focusReleasing: boolean;
76 /** The sessions streaming the guest; one stopping leaves the others' screencasts running. */
77 readonly screencasts: Set<string | undefined>;
78 readonly onMessage: (
79 event: Electron.Event,
80 method: string,
81 params: unknown,
82 sessionId: string,
83 ) => void;
84 readonly onNewDocument: () => void;
85 /** Stops waiting for the first page; set until the tab is announced. */
86 stopWaiting: (() => void) | null;
88​
89export class DesktopBrowserHost extends Context.Service<
90 DesktopBrowserHost,
91 {
92 readonly events: Stream.Stream<Uint8Array>;
93 readonly handleCommandLine: (line: string) => Effect.Effect<void>;
94 readonly attach: (key: DesktopBrowserTabKey, debuggee: DesktopBrowserTabDebugger) => void;
95 readonly detach: (key: DesktopBrowserTabKey) => void;
96 readonly loadFailed: (key: DesktopBrowserTabKey, failure: PreviewAutomationLoadError) => void;
97 readonly setShown: (key: DesktopBrowserTabKey, shown: boolean | undefined) => void;
98 readonly humanStartedDownload: (source: Electron.WebContents) => boolean;
99 readonly placeDownload: (source: Electron.WebContents, item: Electron.DownloadItem) => boolean;
100 readonly pointers: Stream.Stream<{
101 readonly key: DesktopBrowserTabKey;
102 readonly phase: "move" | "click";
103 readonly x: number;
104 readonly y: number;
105 }>;
106 /** Whether a guest needs its frames painted while the panel hides it. */
107 readonly painting: Stream.Stream<{
108 readonly key: DesktopBrowserTabKey;
109 readonly active: boolean;
110 }>;
111 }
112>()("@t3tools/desktop/preview/DesktopBrowserHost") {}
113​
114export const make = Effect.gen(function* () {
115 const outbox = yield* PubSub.unbounded<DesktopBrowserEventType>();
116 const pointers = yield* PubSub.sliding<{
117 readonly key: DesktopBrowserTabKey;
118 readonly phase: "move" | "click";
119 readonly x: number;
120 readonly y: number;
121 }>(16);
122 const painting = yield* PubSub.unbounded<{
123 readonly key: DesktopBrowserTabKey;
124 readonly active: boolean;
125 }>();
126 const runFork = Effect.runForkWith(yield* Effect.context<never>());
127 const tabs = new Map<string, AttachedTab>();
128 /** Threads whose agent turn runs, by the server's account. */
129 const agentTurns = new Set<string>();
130 // Kept apart from attachment: the renderer can show a webview before its debugger attaches,
131 // and the server forgets `shown` on detach, for example while DevTools holds the debugger.
132 const shownTabs = new Map<string, boolean>();
133 const emit = (event: DesktopBrowserEventType) => runFork(PubSub.publish(outbox, event));
134​
135 const nextScreenshot = (tab: AttachedTab) => {
136 tab.screenshotTurn = null;
137 tab.waitingScreenshots.shift()?.start();
138 releaseFocusWhenIdle(tab);
139 syncPainting(tab);
140 };
141​
142 /**
143 * Chromium below 157 crashes the whole app when a guest's renderer exits with a
144 * screenshot copy in flight (crbug.com/567321049). Overlapping screenshots make that
145 * far likelier, and one the server gave up on stays pending in Chromium, so each guest
146 * gets one at a time. One whose relay or session has since gone is dropped unsent.
147 */
148 const takeScreenshot = (
149 tab: AttachedTab,
150 relay: CdpRelayConnection,
151 ownerLive: () => boolean,
152 /** Sends only while `current` holds; holds focus only while `held`. */
153 capture: (current: () => boolean, held: boolean) => Promise<unknown>,
154 ) =>
155 new Promise<unknown>((resolve, reject) => {
156 const live = () => tab.relay === relay && ownerLive();
157 const waiting = {
158 start: () => {
159 if (!live()) {
160 reject(new Error("The preview connection that asked for this screenshot closed."));
161 nextScreenshot(tab);
162 return;
163 }
164 const turn = {};
165 tab.screenshotTurn = turn;
166 capture(() => tab.screenshotTurn === turn && live(), waiting.held)
167 .finally(() => {
168 if (tab.screenshotTurn === turn) nextScreenshot(tab);
169 })
170 .then(resolve, reject);
171 },
172 live,
173 held: true,
174 };
175 if (tab.screenshotTurn === null) waiting.start();
176 else {
177 // The one ahead may be abandoned mid-capture; it needs frames to finish and free its turn.
178 tab.waitingScreenshots.push(waiting);
179 setFocusEmulation(tab, true);
180 syncPainting(tab);
181 }
182 });
183​
184 /**
185 * Chromium counts a focus-emulated guest as captured, so a guest the panel hides would
186 * never throttle. Without it, a hidden guest paints no frames and focus() fires no focus
187 * events, so the guest is emulated focused while the server drives it or streams it.
188 */
189 const setFocusEmulation = (tab: AttachedTab, enabled: boolean) => {
190 if (tab.focusEmulated === enabled) return;
191 tab.focusEmulated = enabled;
192 // A guest that refuses it, or a debugger already gone, behaves as unfocused.
193 const change: Promise<void> = tab.debuggee.debugger
194 .sendCommand("Emulation.setFocusEmulationEnabled", { enabled })
195 .then(
196 () => undefined,
197 () => undefined,
198 )
199 .finally(() => {
200 if (tab.focusChange === change) tab.focusChange = null;
201 });
202 tab.focusChange = change;
203 };
204​
205 const waitingHolds = (tab: AttachedTab) =>
206 tab.waitingScreenshots.some((waiting) => waiting.held && waiting.live());
207​
208 const releaseFocusWhenIdle = (tab: AttachedTab) => {
209 if (tab.focusReleasing) return;
210 tab.focusReleasing = true;
211 runFork(
212 Effect.gen(function* () {
213 let uses: number;
214 do {
215 uses = tab.focusUses;
216 yield* Effect.sleep(FOCUS_IDLE);
217 } while (uses !== tab.focusUses);
218 tab.focusReleasing = false;
219 // The last pending command to settle starts another idle wait.
220 if (tab.focusPending.size > 0 || tab.screencasts.size > 0 || waitingHolds(tab)) {
221 return;
222 }
223 // The agent pauses to think between steps; its tab stays focused until the turn ends.
224 if (tab.agentUsed && agentTurns.has(tab.key.threadId)) return;
225 tab.agentUsed = false;
226 if (tabs.get(keyOf(tab.key)) === tab) setFocusEmulation(tab, false);
227 }),
228 );
229 };
230​
231 /**
232 * Electron composites no offscreen guest, so its screenshot or screencast waits for a frame
233 * that never comes. The client paints the guest behind the app while one is pending; the
234 * capture then finishes on its own, and the guest goes back offscreen once none is.
235 */
236 const syncPainting = (tab: AttachedTab) => {
237 const active =
238 tabs.get(keyOf(tab.key)) === tab &&
239 (tab.screencasts.size > 0 ||
240 tab.capturing.size > 0 ||
241 tab.waitingScreenshots.some((waiting) => waiting.live()));
242 if (tab.painting === active) return;
243 tab.painting = active;
244 runFork(PubSub.publish(painting, { key: tab.key, active }));
245 };
246​
247 /** A command may wait on the page's next frame, so the guest stays focused until it settles. */
248 const holdFocus = <A>(tab: AttachedTab, owner: string | undefined, work: () => Promise<A>) => {
249 const pending = {};
250 tab.focusPending.set(pending, owner);
251 tab.focusUses += 1;
252 setFocusEmulation(tab, true);
253 releaseFocusWhenIdle(tab);
254 return work().finally(() => {
255 if (!tab.focusPending.delete(pending)) return;
256 tab.focusUses += 1;
257 releaseFocusWhenIdle(tab);
258 });
259 };
260​
261 /** Chromium answers a capture, and frees its wake lock, only once the guest paints a frame. */
262 const paintUntilAnswered = <A>(tab: AttachedTab, send: () => Promise<A>) => {
263 const sent = {};
264 tab.capturing.add(sent);
265 syncPainting(tab);
266 return send().finally(() => {
267 tab.capturing.delete(sent);
268 syncPainting(tab);
269 });
270 };
271​
272 /**
273 * Chromium can leave a command unanswered, so the screenshot guard's release points
274 * also end every focus hold: a released relay has no one waiting, and a new document
275 * answers nothing pending from the old one. A released relay's screencast has no viewer.
276 */
277 const dropFocusHolds = (tab: AttachedTab) => {
278 tab.focusPending.clear();
279 releaseFocusWhenIdle(tab);
280 syncPainting(tab);
281 };
282​
283 const dropRelay = (tab: AttachedTab) => {
284 tab.relay = null;
285 tab.screencasts.clear();
286 tab.agentUsed = false;
287 dropFocusHolds(tab);
288 };
289​
290 /** A detached session, such as a closed viewer, waits on nothing it sent. */
291 const dropSessionHolds = (tab: AttachedTab, owner: string) => {
292 tab.screencasts.delete(owner);
293 for (const [pending, holder] of tab.focusPending) {
294 if (holder === owner) tab.focusPending.delete(pending);
295 }
296 releaseFocusWhenIdle(tab);
297 syncPainting(tab);
298 };
299​
300 /**
301 * A command the server stopped waiting on, such as a timed-out screenshot or evaluation,
302 * may never settle; the turn's end ends every focus hold. The agent also sends on its own extra
303 * session, so this cannot spare other sessions; a viewer's screencast still holds.
304 */
305 const endAgentTurn = (threadId: string) => {
306 agentTurns.delete(threadId);
307 for (const tab of tabs.values()) {
308 if (tab.key.threadId !== threadId) continue;
309 tab.agentUsed = false;
310 // Still sent in turn, so the queue keeps moving, but no longer kept focused.
311 for (const waiting of tab.waitingScreenshots) waiting.held = false;
312 dropFocusHolds(tab);
313 }
314 };
315​
316 const relayFor = (tab: AttachedTab) => {
317 if (tab.relay) return tab.relay;
318 const { webContents, debugger: debuggee } = tab.debuggee;
319 const send = (method: string, params: Record<string, unknown>, sessionId?: string) =>
320 sendPageCommand(webContents, debuggee, method, params, sessionId);
321 const detachedOwners = new Set<string>();
322 /** Viewers and recordings stream; the agent's own sessions never do. */
323 const streamingOwners = new Set<string>();
324 const relay: CdpRelayConnection = createCdpRelayConnection(
325 {
326 send: (method, params, sessionId, owner) => {
327 // A child session's screencast is not the page's, such as a late one on a detached session.
328 if (sessionId === undefined && method === "Page.startScreencast") {
329 tab.screencasts.add(owner);
330 if (owner !== undefined) streamingOwners.add(owner);
331 syncPainting(tab);
332 } else if (sessionId === undefined && method === "Page.stopScreencast") {
333 tab.screencasts.delete(owner);
334 syncPainting(tab);
335 }
336 // A person's input through a viewer, or Playwright's own housekeeping, is not the agent's.
337 if (
338 AGENT_INPUT_METHOD.test(method) &&
339 (owner === undefined || !streamingOwners.has(owner))
340 ) {
341 tab.agentUsed = true;
342 }
343 // Waits for focus emulation; insertText and other non-CDP input would not queue behind it.
344 const forward = (current?: () => boolean) => {
345 const change = tab.focusChange;
346 const run = () =>
347 tab.relay !== relay || (current !== undefined && !current())
348 ? Promise.reject(new Error("The page this screenshot was for has moved on."))
349 : current === undefined
350 ? send(method, params, sessionId)
351 : paintUntilAnswered(tab, () => send(method, params, sessionId));
352 return change === null ? run() : change.then(run);
353 };
354 return method === "Page.captureScreenshot"
355 ? takeScreenshot(
356 tab,
357 relay,
358 () => owner === undefined || !detachedOwners.has(owner),
359 (current, held) =>
360 held ? holdFocus(tab, owner, () => forward(current)) : forward(current),
361 )
362 : method === "Page.startScreencast"
363 ? holdFocus(tab, owner, forward).catch((cause: unknown) => {
364 // A viewer can outlive a refused stream; nothing is streaming for it.
365 if (sessionId === undefined && tab.relay === relay) tab.screencasts.delete(owner);
366 releaseFocusWhenIdle(tab);
367 syncPainting(tab);
368 throw cause;
369 })
370 : holdFocus(tab, owner, forward);
371 },
372 sessionDetached: (owner) => {
373 detachedOwners.add(owner);
374 dropSessionHolds(tab, owner);
375 },
376 targetId: () =>
377 debuggee
378 .sendCommand("Target.getTargetInfo")
379 .then((result: { targetInfo: { targetId: string } }) => result.targetInfo.targetId),
380 url: () => webContents.getURL(),
381 title: () => webContents.getTitle(),
382 userAgent: () => webContents.getUserAgent(),
383 setDownloadDirectory: (directory) => {
384 tab.downloadDirectory = directory;
385 },
386 },
387 (message) => {
388 if (tab.relay === relay && tabs.get(keyOf(tab.key)) === tab) {
389 emit({ type: "cdp", ...tab.key, message });
390 }
391 },
392 // Announcing the tab again would reconnect Playwright, and the page could
393 // replay the same oversized event, so the tab stays detached.
394 () => {
395 if (tab.relay !== relay || tabs.get(keyOf(tab.key)) !== tab) return;
396 // Disabling auto-attach detaches child sessions and releases their debugger pauses.
397 void debuggee
398 .sendCommand("Target.setAutoAttach", {
399 autoAttach: false,
400 waitForDebuggerOnStart: false,
401 flatten: true,
402 })
403 .catch(() => {});
404 if (tab.screencasts.size > 0) {
405 void debuggee.sendCommand("Page.stopScreencast").catch(() => {});
406 }
407 setFocusEmulation(tab, false);
408 detach(tab.key);
409 },
410 );
411 tab.relay = relay;
412 return relay;
413 };
414​
415 // CDP supplies the path before this runs; otherwise Electron opens a Save dialog.
416 const placeDownload = (source: Electron.WebContents, item: Electron.DownloadItem) => {
417 const tab = [...tabs.values()].find(
418 (candidate) => candidate.debuggee.webContents === source && candidate.downloadDirectory,
419 );
420 if (!tab?.downloadDirectory || !tab.pendingDownloadGuid) return false;
421 item.setSavePath(NodePath.join(tab.downloadDirectory, tab.pendingDownloadGuid));
422 tab.pendingDownloadGuid = null;
423 return true;
424 };
425​
426 const detach = (key: DesktopBrowserTabKey) => {
427 const id = keyOf(key);
428 const tab = tabs.get(id);
429 if (!tab) return;
430 tabs.delete(id);
431 const announced = tab.stopWaiting === null;
432 tab.stopWaiting?.();
433 tab.relay = null;
434 tab.debuggee.debugger.off("message", tab.onMessage);
435 tab.debuggee.webContents.off("did-navigate", tab.onNewDocument);
436 syncPainting(tab);
437 if (announced) emit({ type: "detached", ...key });
438 };
439​
440 const attach = (key: DesktopBrowserTabKey, debuggee: DesktopBrowserTabDebugger) => {
441 const id = keyOf(key);
442 if (tabs.get(id)?.debuggee.webContents === debuggee.webContents) return;
443 detach(key);
444 const tab: AttachedTab = {
445 key,
446 debuggee,
447 relay: null,
448 screenshotTurn: null,
449 waitingScreenshots: [],
450 downloadDirectory: null,
451 pendingDownloadGuid: null,
452 agentInputAt: Number.NEGATIVE_INFINITY,
453 focusEmulated: false,
454 focusChange: null,
455 focusUses: 0,
456 focusPending: new Map(),
457 capturing: new Set(),
458 painting: false,
459 agentUsed: false,
460 focusReleasing: false,
461 screencasts: new Set(),
462 onMessage: (_event, method, params, sessionId) => {
463 if (method === "Page.downloadWillBegin") {
464 const guid = (params as { guid?: unknown } | undefined)?.guid;
465 tab.pendingDownloadGuid = typeof guid === "string" ? guid : null;
466 }
467 tab.relay?.event(method, params, sessionId);
468 },
469 // A screenshot pending when the page loads a new document, a reload included, can
470 // stay unanswered. A crashed page lands here once it reloads, well after its renderer died.
471 onNewDocument: () => {
472 // Before the next screenshot starts, so its own holds survive.
473 tab.capturing.clear();
474 dropFocusHolds(tab);
475 if (tab.screenshotTurn !== null) nextScreenshot(tab);
476 },
477 stopWaiting: null,
478 };
479 tabs.set(id, tab);
480 debuggee.debugger.on("message", tab.onMessage);
481 debuggee.webContents.on("did-navigate", tab.onNewDocument);
482 const announce = () => {
483 emit({ type: "attached", ...key });
484 const shown = shownTabs.get(id);
485 if (shown !== undefined) emit({ type: "shown", ...key, shown });
486 };
487 if (!debuggee.webContents.isLoadingMainFrame()) {
488 announce();
489 return;
490 }
491 // Electron does not pause a guest for the debugger, so Playwright connecting during its first
492 // commit builds its utility world in the discarded document and every wait on it hangs.
493 const { webContents } = debuggee;
494 // ERR_ABORTED is a newer navigation replacing this one, whose commit is still to come.
495 const onFailLoad = (
496 _event: Electron.Event,
497 code: number,
498 _description: string,
499 _url: string,
500 isMainFrame: boolean,
501 ) => {
502 if (isMainFrame && code !== ERR_ABORTED) ready();
503 };
504 const ready = () => {
505 if (tab.stopWaiting === null) return;
506 // The manager's cleanup for a destroyed guest can still be queued behind another tab's attach.
507 if (webContents.isDestroyed()) return detach(key);
508 tab.stopWaiting();
509 announce();
510 };
511 const timeout = runFork(
512 Effect.sleep(FIRST_PAGE_TIMEOUT).pipe(Effect.andThen(Effect.sync(ready))),
513 );
514 tab.stopWaiting = () => {
515 tab.stopWaiting = null;
516 webContents.off("dom-ready", ready);
517 webContents.off("did-stop-loading", ready);
518 webContents.off("did-fail-load", onFailLoad);
519 runFork(Fiber.interrupt(timeout));
520 };
521 webContents.on("dom-ready", ready);
522 webContents.on("did-stop-loading", ready);
523 webContents.on("did-fail-load", onFailLoad);
524 };
525​
526 const setShown = (key: DesktopBrowserTabKey, shown: boolean | undefined) => {
527 const id = keyOf(key);
528 if (shown === undefined) {
529 shownTabs.delete(id);
530 return;
531 }
532 if (shownTabs.get(id) === shown) return;
533 shownTabs.set(id, shown);
534 if (tabs.get(id)?.stopWaiting === null) emit({ type: "shown", ...key, shown });
535 };
536​
537 const loadFailed: DesktopBrowserHost["Service"]["loadFailed"] = (key, failure) => {
538 if (tabs.has(keyOf(key))) emit({ type: "loadFailed", ...key, ...failure });
539 };
540​
541 const handleCommandLine = (line: string) =>
542 Effect.sync(() => {
543 const command = decodeCommand(line);
544 if (Option.isNone(command)) return;
545 if (command.value.type === "turn") {
546 if (command.value.active) agentTurns.add(command.value.threadId);
547 else endAgentTurn(command.value.threadId);
548 return;
549 }
550 const tab = tabs.get(keyOf(command.value));
551 if (!tab) return;
552 if (command.value.type === "pointer") {
553 const { threadId, tabId, phase, x, y } = command.value;
554 runFork(PubSub.publish(pointers, { key: { threadId, tabId }, phase, x, y }));
555 return;
556 }
557 if (command.value.type === "release") {
558 dropRelay(tab);
559 return;
560 }
561 if (AGENT_INPUT_COMMAND.test(command.value.message)) tab.agentInputAt = performance.now();
562 relayFor(tab).receive(command.value.message);
563 });
564​
565 const humanStartedDownload = (source: Electron.WebContents) => {
566 const tab = [...tabs.values()].find((candidate) => candidate.debuggee.webContents === source);
567 return tab !== undefined && performance.now() - tab.agentInputAt > AGENT_DOWNLOAD_WINDOW_MS;
568 };
569​
570 const announceAll = Effect.suspend(() => {
571 // A new backend: the old one's turns ended without telling us.
572 agentTurns.clear();
573 return Effect.forEach(
574 [...tabs.values()],
575 (tab) => {
576 dropRelay(tab);
577 // Announced once its first page is up.
578 if (tab.stopWaiting !== null) return Effect.void;
579 const shown = shownTabs.get(keyOf(tab.key));
580 return PubSub.publish(outbox, { type: "attached", ...tab.key }).pipe(
581 Effect.andThen(
582 shown !== undefined
583 ? PubSub.publish(outbox, { type: "shown", ...tab.key, shown })
584 : Effect.void,
585 ),
586 );
587 },
588 { discard: true },
589 );
590 });
591​
592 return DesktopBrowserHost.of({
593 pointers: Stream.fromPubSub(pointers),
594 painting: Stream.fromPubSub(painting),
595 // Subscribes before announcing, so no attach falls between the two.
596 events: Stream.unwrap(
597 Effect.gen(function* () {
598 const subscription = yield* PubSub.subscribe(outbox);
599 yield* announceAll;
600 return Stream.fromSubscription(subscription);
601 }),
602 ).pipe(Stream.map((event) => lineEncoder.encode(`${encodeEvent(event)}\n`))),
603 handleCommandLine,
604 attach,
605 detach,
606 loadFailed,
607 setShown,
608 placeDownload,
609 humanStartedDownload,
610 });
611});
612​
613export const layer = Layer.effect(DesktopBrowserHost, make);