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 * Keep this the one source of truth: the producer (`connection/platform.ts`) mints ids via
10 * {@link desktopLocalConnectionId} and every consumer classifies via {@link
11 * isDesktopLocalConnectionTarget}, so the convention can never drift between the two.
12 */
13const DESKTOP_LOCAL_CONNECTION_ID_PREFIX = "local:";
14
15export function desktopLocalConnectionId(backendId: string): string {
16 return `${DESKTOP_LOCAL_CONNECTION_ID_PREFIX}${backendId}`;
17}
18
19export function isDesktopLocalConnectionTarget(
20 target: ConnectionTarget,
21): target is Extract<ConnectionTarget, { readonly _tag: "BearerConnectionTarget" }> {
22 return (
23 target._tag === "BearerConnectionTarget" &&
24 target.connectionId.startsWith(DESKTOP_LOCAL_CONNECTION_ID_PREFIX)
25 );
26}
27
28export function desktopLocalBackendId(target: ConnectionTarget): string | null {
29 return isDesktopLocalConnectionTarget(target)
30 ? target.connectionId.slice(DESKTOP_LOCAL_CONNECTION_ID_PREFIX.length)
31 : null;
32}
33
34export function isWslConnectionTarget(target: ConnectionTarget): boolean {
35 return desktopLocalBackendId(target)?.startsWith("wsl:") === true;
36}
37
38export type DesktopSecondaryBootstrapsRead =
39 | {
40 readonly _tag: "Success";
41 readonly bootstraps: ReadonlyArray<DesktopEnvironmentBootstrap>;
42 }
43 | {
44 readonly _tag: "Failure";
45 readonly cause: unknown;
46 };
47
48export interface DesktopSecondaryBootstrapsReader {
49 readonly readResult: () => DesktopSecondaryBootstrapsRead;
50 readonly readSnapshot: () => ReadonlyArray<DesktopEnvironmentBootstrap>;
51}
52
53/**
54 * A successful empty read is authoritative; a thrown read preserves the previous snapshot so UI
55 * consumers cannot temporarily disagree with the platform's retained registrations.
56 */
57export function createDesktopSecondaryBootstrapsReader(
58 resolveBridge: () => Pick<DesktopBridge, "getLocalEnvironmentBootstraps"> | undefined,
59): DesktopSecondaryBootstrapsReader {
60 let snapshot: ReadonlyArray<DesktopEnvironmentBootstrap> = [];
61
62 const readResult = (): DesktopSecondaryBootstrapsRead => {
63 const bridge = resolveBridge();
64 if (bridge === undefined) {
65 if (snapshot.length > 0) snapshot = [];
66 return { _tag: "Success", bootstraps: snapshot };
67 }
68 try {
69 const next = bridge
70 .getLocalEnvironmentBootstraps()
71 .filter((entry) => entry.id !== PRIMARY_LOCAL_ENVIRONMENT_ID);
72 if (
73 next.length !== snapshot.length ||
74 next.some((entry, index) => {
75 const previous = snapshot[index];
76 return (
77 previous === undefined ||
78 entry.id !== previous.id ||
79 entry.label !== previous.label ||
80 entry.runningDistro !== previous.runningDistro ||
81 entry.httpBaseUrl !== previous.httpBaseUrl ||
82 entry.wsBaseUrl !== previous.wsBaseUrl ||
83 entry.bootstrapToken !== previous.bootstrapToken
84 );
85 })
86 ) {
87 snapshot = next;
88 }
89 return { _tag: "Success", bootstraps: snapshot };
90 } catch (cause) {
91 return { _tag: "Failure", cause };
92 }
93 };
94
95 return {
96 readResult,
97 readSnapshot: () => {
98 const result = readResult();
99 return result._tag === "Success" ? result.bootstraps : snapshot;
100 },
101 };
102}
103
104const desktopSecondaryBootstrapsReader = createDesktopSecondaryBootstrapsReader(
105 () => window.desktopBridge,
106);
107
108export function readDesktopSecondaryBootstrapsResult(): DesktopSecondaryBootstrapsRead {
109 return desktopSecondaryBootstrapsReader.readResult();
110}
111
112export function readDesktopSecondaryBootstraps(): ReadonlyArray<DesktopEnvironmentBootstrap> {
113 return desktopSecondaryBootstrapsReader.readSnapshot();
114}