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

desktopLocal.ts

104 lines3.6 KB

1import type { ConnectionTarget } from "@v12/client-runtime/connection";
2import {
3 PRIMARY_LOCAL_ENVIRONMENT_ID,
4 type DesktopBridge,
5 type DesktopEnvironmentBootstrap,
6} from "@v12/contracts";
7​
8/**
9 * Desktop-local secondary backends (e.g. a parallel WSL backend) are registered
10 * by the connection platform source as bearer connections whose id carries this
11 * prefix. It is the renderer's single signal that an environment is a
12 * host-managed local backend rather than a user-saved remote, SSH, or relay
13 * environment.
14 *
15 * Keep this the one source of truth: the producer (`connection/platform.ts`)
16 * mints ids via {@link desktopLocalConnectionId} and every consumer classifies
17 * via {@link isDesktopLocalConnectionTarget}, so the convention can never drift
18 * between the two.
19 */
20export const DESKTOP_LOCAL_CONNECTION_ID_PREFIX = "local:";
21​
22export function desktopLocalConnectionId(backendId: string): string {
23 return `${DESKTOP_LOCAL_CONNECTION_ID_PREFIX}${backendId}`;
25​
26export function isDesktopLocalConnectionTarget(
27 target: ConnectionTarget,
28): target is Extract<ConnectionTarget, { readonly _tag: "BearerConnectionTarget" }> {
29 return (
30 target._tag === "BearerConnectionTarget" &&
31 target.connectionId.startsWith(DESKTOP_LOCAL_CONNECTION_ID_PREFIX)
32 );
34​
35export function desktopLocalBackendId(target: ConnectionTarget): string | null {
36 return isDesktopLocalConnectionTarget(target)
37 ? target.connectionId.slice(DESKTOP_LOCAL_CONNECTION_ID_PREFIX.length)
38 : null;
40​
41export type DesktopSecondaryBootstrapsRead =
42 | {
43 readonly _tag: "Success";
44 readonly bootstraps: ReadonlyArray<DesktopEnvironmentBootstrap>;
45 }
46 | {
47 readonly _tag: "Failure";
48 readonly cause: unknown;
49 };
50​
51export interface DesktopSecondaryBootstrapsReader {
52 readonly readResult: () => DesktopSecondaryBootstrapsRead;
53 readonly readSnapshot: () => ReadonlyArray<DesktopEnvironmentBootstrap>;
55​
56/**
57 * Build a topology reader whose snapshot advances only after successful bridge
58 * reads. A successful empty read is authoritative; a thrown read preserves the
59 * previous snapshot so UI consumers cannot temporarily disagree with the
60 * platform's retained registrations.
61 */
62export function createDesktopSecondaryBootstrapsReader(
63 resolveBridge: () => Pick<DesktopBridge, "getLocalEnvironmentBootstraps"> | undefined,
64): DesktopSecondaryBootstrapsReader {
65 let snapshot: ReadonlyArray<DesktopEnvironmentBootstrap> = [];
66​
67 const readResult = (): DesktopSecondaryBootstrapsRead => {
68 const bridge = resolveBridge();
69 if (bridge === undefined) {
70 snapshot = [];
71 return { _tag: "Success", bootstraps: snapshot };
72 }
73 try {
74 snapshot = bridge
75 .getLocalEnvironmentBootstraps()
76 .filter((entry) => entry.id !== PRIMARY_LOCAL_ENVIRONMENT_ID);
77 return { _tag: "Success", bootstraps: snapshot };
78 } catch (cause) {
79 return { _tag: "Failure", cause };
80 }
81 };
82​
83 return {
84 readResult,
85 readSnapshot: () => {
86 const result = readResult();
87 return result._tag === "Success" ? result.bootstraps : snapshot;
88 },
89 };
91​
92const desktopSecondaryBootstrapsReader = createDesktopSecondaryBootstrapsReader(
93 () => window.desktopBridge,
94);
95​
96/** Read the topology while preserving failures for platform cache policy. */
97export function readDesktopSecondaryBootstrapsResult(): DesktopSecondaryBootstrapsRead {
98 return desktopSecondaryBootstrapsReader.readResult();
100​
101/** Read the latest successful topology snapshot for renderer consumers. */
102export function readDesktopSecondaryBootstraps(): ReadonlyArray<DesktopEnvironmentBootstrap> {
103 return desktopSecondaryBootstrapsReader.readSnapshot();