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

desktopLocal.ts

121 lines4.2 KB

1import type { ConnectionTarget } from "@t3tools/client-runtime/connection";
2import {
3 PRIMARY_LOCAL_ENVIRONMENT_ID,
4 type DesktopBridge,
5 type DesktopEnvironmentBootstrap,
6} from "@t3tools/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 if (snapshot.length > 0) snapshot = [];
71 return { _tag: "Success", bootstraps: snapshot };
72 }
73 try {
74 const next = bridge
75 .getLocalEnvironmentBootstraps()
76 .filter((entry) => entry.id !== PRIMARY_LOCAL_ENVIRONMENT_ID);
77 if (
78 next.length !== snapshot.length ||
79 next.some((entry, index) => {
80 const previous = snapshot[index];
81 return (
82 previous === undefined ||
83 entry.id !== previous.id ||
84 entry.label !== previous.label ||
85 entry.runningDistro !== previous.runningDistro ||
86 entry.httpBaseUrl !== previous.httpBaseUrl ||
87 entry.wsBaseUrl !== previous.wsBaseUrl ||
88 entry.bootstrapToken !== previous.bootstrapToken
89 );
90 })
91 ) {
92 snapshot = next;
93 }
94 return { _tag: "Success", bootstraps: snapshot };
95 } catch (cause) {
96 return { _tag: "Failure", cause };
97 }
98 };
99​
100 return {
101 readResult,
102 readSnapshot: () => {
103 const result = readResult();
104 return result._tag === "Success" ? result.bootstraps : snapshot;
105 },
106 };
108​
109const desktopSecondaryBootstrapsReader = createDesktopSecondaryBootstrapsReader(
110 () => window.desktopBridge,
111);
112​
113/** Read the topology while preserving failures for platform cache policy. */
114export function readDesktopSecondaryBootstrapsResult(): DesktopSecondaryBootstrapsRead {
115 return desktopSecondaryBootstrapsReader.readResult();
117​
118/** Read the latest successful topology snapshot for renderer consumers. */
119export function readDesktopSecondaryBootstraps(): ReadonlyArray<DesktopEnvironmentBootstrap> {
120 return desktopSecondaryBootstrapsReader.readSnapshot();