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
8const DESKTOP_LOCAL_CONNECTION_ID_PREFIX = "local:";
9
10export function desktopLocalConnectionId(backendId: string): string {
11 return `${DESKTOP_LOCAL_CONNECTION_ID_PREFIX}${backendId}`;
12}
13
14export function isDesktopLocalConnectionTarget(
15 target: ConnectionTarget,
16): target is Extract<ConnectionTarget, { readonly _tag: "BearerConnectionTarget" }> {
17 return (
18 target._tag === "BearerConnectionTarget" &&
19 target.connectionId.startsWith(DESKTOP_LOCAL_CONNECTION_ID_PREFIX)
20 );
21}
22
23export function desktopLocalBackendId(target: ConnectionTarget): string | null {
24 return isDesktopLocalConnectionTarget(target)
25 ? target.connectionId.slice(DESKTOP_LOCAL_CONNECTION_ID_PREFIX.length)
26 : null;
27}
28
29export function isWslConnectionTarget(target: ConnectionTarget): boolean {
30 return desktopLocalBackendId(target)?.startsWith("wsl:") === true;
31}
32
33export type DesktopSecondaryBootstrapsRead =
34 | {
35 readonly _tag: "Success";
36 readonly bootstraps: ReadonlyArray<DesktopEnvironmentBootstrap>;
37 }
38 | {
39 readonly _tag: "Failure";
40 readonly cause: unknown;
41 };
42
43export interface DesktopSecondaryBootstrapsReader {
44 readonly readResult: () => DesktopSecondaryBootstrapsRead;
45 readonly readSnapshot: () => ReadonlyArray<DesktopEnvironmentBootstrap>;
46}
47
48/** Empty reads clear the snapshot; failed reads retain platform registrations. */
49export function createDesktopSecondaryBootstrapsReader(
50 resolveBridge: () => Pick<DesktopBridge, "getLocalEnvironmentBootstraps"> | undefined,
51): DesktopSecondaryBootstrapsReader {
52 let snapshot: ReadonlyArray<DesktopEnvironmentBootstrap> = [];
53
54 const readResult = (): DesktopSecondaryBootstrapsRead => {
55 const bridge = resolveBridge();
56 if (bridge === undefined) {
57 if (snapshot.length > 0) snapshot = [];
58 return { _tag: "Success", bootstraps: snapshot };
59 }
60 try {
61 const next = bridge
62 .getLocalEnvironmentBootstraps()
63 .filter((entry) => entry.id !== PRIMARY_LOCAL_ENVIRONMENT_ID);
64 if (
65 next.length !== snapshot.length ||
66 next.some((entry, index) => {
67 const previous = snapshot[index];
68 return (
69 previous === undefined ||
70 entry.id !== previous.id ||
71 entry.label !== previous.label ||
72 entry.runningDistro !== previous.runningDistro ||
73 entry.httpBaseUrl !== previous.httpBaseUrl ||
74 entry.wsBaseUrl !== previous.wsBaseUrl ||
75 entry.bootstrapToken !== previous.bootstrapToken
76 );
77 })
78 ) {
79 snapshot = next;
80 }
81 return { _tag: "Success", bootstraps: snapshot };
82 } catch (cause) {
83 return { _tag: "Failure", cause };
84 }
85 };
86
87 return {
88 readResult,
89 readSnapshot: () => {
90 const result = readResult();
91 return result._tag === "Success" ? result.bootstraps : snapshot;
92 },
93 };
94}
95
96const desktopSecondaryBootstrapsReader = createDesktopSecondaryBootstrapsReader(
97 () => window.desktopBridge,
98);
99
100export function readDesktopSecondaryBootstrapsResult(): DesktopSecondaryBootstrapsRead {
101 return desktopSecondaryBootstrapsReader.readResult();
102}
103
104export function readDesktopSecondaryBootstraps(): ReadonlyArray<DesktopEnvironmentBootstrap> {
105 return desktopSecondaryBootstrapsReader.readSnapshot();
106}