Open History to see the last commit
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}`;
24}
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 );
33}
34
35/**
36 * Environments hosted by this desktop app: the same-origin primary backend
37 * plus desktop-local secondaries (e.g. a parallel WSL backend). SSH, relay,
38 * and other remote targets are excluded; their agents keep running on their
39 * own host regardless of what this machine does.
40 */
41export function isDesktopHostedConnectionTarget(target: ConnectionTarget): boolean {
42 return target._tag === "PrimaryConnectionTarget" || isDesktopLocalConnectionTarget(target);
43}
44
45export function desktopLocalBackendId(target: ConnectionTarget): string | null {
46 return isDesktopLocalConnectionTarget(target)
47 ? target.connectionId.slice(DESKTOP_LOCAL_CONNECTION_ID_PREFIX.length)
48 : null;
49}
50
51export type DesktopSecondaryBootstrapsRead =
52 | {
53 readonly _tag: "Success";
54 readonly bootstraps: ReadonlyArray<DesktopEnvironmentBootstrap>;
55 }
56 | {
57 readonly _tag: "Failure";
58 readonly cause: unknown;
59 };
60
61export interface DesktopSecondaryBootstrapsReader {
62 readonly readResult: () => DesktopSecondaryBootstrapsRead;
63 readonly readSnapshot: () => ReadonlyArray<DesktopEnvironmentBootstrap>;
64}
65
66/**
67 * Build a topology reader whose snapshot advances only after successful bridge
68 * reads. A successful empty read is authoritative; a thrown read preserves the
69 * previous snapshot so UI consumers cannot temporarily disagree with the
70 * platform's retained registrations.
71 */
72export function createDesktopSecondaryBootstrapsReader(
73 resolveBridge: () => Pick<DesktopBridge, "getLocalEnvironmentBootstraps"> | undefined,
74): DesktopSecondaryBootstrapsReader {
75 let snapshot: ReadonlyArray<DesktopEnvironmentBootstrap> = [];
76
77 const readResult = (): DesktopSecondaryBootstrapsRead => {
78 const bridge = resolveBridge();
79 if (bridge === undefined) {
80 snapshot = [];
81 return { _tag: "Success", bootstraps: snapshot };
82 }
83 try {
84 snapshot = bridge
85 .getLocalEnvironmentBootstraps()
86 .filter((entry) => entry.id !== PRIMARY_LOCAL_ENVIRONMENT_ID);
87 return { _tag: "Success", bootstraps: snapshot };
88 } catch (cause) {
89 return { _tag: "Failure", cause };
90 }
91 };
92
93 return {
94 readResult,
95 readSnapshot: () => {
96 const result = readResult();
97 return result._tag === "Success" ? result.bootstraps : snapshot;
98 },
99 };
100}
101
102const desktopSecondaryBootstrapsReader = createDesktopSecondaryBootstrapsReader(
103 () => window.desktopBridge,
104);
105
106/** Read the topology while preserving failures for platform cache policy. */
107export function readDesktopSecondaryBootstrapsResult(): DesktopSecondaryBootstrapsRead {
108 return desktopSecondaryBootstrapsReader.readResult();
109}
110
111/** Read the latest successful topology snapshot for renderer consumers. */
112export function readDesktopSecondaryBootstraps(): ReadonlyArray<DesktopEnvironmentBootstrap> {
113 return desktopSecondaryBootstrapsReader.readSnapshot();
114}