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

desktopLocal.ts

130 lines4.3 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 * 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}`;
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 );
27​
28export function desktopLocalBackendId(target: ConnectionTarget): string | null {
29 return isDesktopLocalConnectionTarget(target)
30 ? target.connectionId.slice(DESKTOP_LOCAL_CONNECTION_ID_PREFIX.length)
31 : null;
33​
34export function isWslConnectionTarget(target: ConnectionTarget): boolean {
35 return desktopLocalBackendId(target)?.startsWith("wsl:") === true;
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 refresh: () => Promise<DesktopSecondaryBootstrapsRead>;
51 readonly readSnapshot: () => ReadonlyArray<DesktopEnvironmentBootstrap>;
53​
54/**
55 * A successful empty read is authoritative; a thrown read preserves the previous snapshot so UI
56 * consumers cannot temporarily disagree with the platform's retained registrations.
57 */
58export function createDesktopSecondaryBootstrapsReader(
59 resolveBridge: () =>
60 | Pick<DesktopBridge, "getLocalEnvironmentBootstraps" | "readLocalEnvironmentBootstraps">
61 | undefined,
62): DesktopSecondaryBootstrapsReader {
63 let snapshot: ReadonlyArray<DesktopEnvironmentBootstrap> = [];
64​
65 const updateSnapshot = (
66 entries: ReadonlyArray<DesktopEnvironmentBootstrap>,
67 ): DesktopSecondaryBootstrapsRead => {
68 const next = entries.filter((entry) => entry.id !== PRIMARY_LOCAL_ENVIRONMENT_ID);
69 if (
70 next.length !== snapshot.length ||
71 next.some((entry, index) => {
72 const previous = snapshot[index];
73 return (
74 previous === undefined ||
75 entry.id !== previous.id ||
76 entry.label !== previous.label ||
77 entry.runningDistro !== previous.runningDistro ||
78 entry.httpBaseUrl !== previous.httpBaseUrl ||
79 entry.wsBaseUrl !== previous.wsBaseUrl ||
80 entry.bootstrapToken !== previous.bootstrapToken
81 );
82 })
83 ) {
84 snapshot = next;
85 }
86 return { _tag: "Success", bootstraps: snapshot };
87 };
88​
89 const readResult = (): DesktopSecondaryBootstrapsRead => {
90 const bridge = resolveBridge();
91 if (bridge === undefined) {
92 if (snapshot.length > 0) snapshot = [];
93 return { _tag: "Success", bootstraps: snapshot };
94 }
95 try {
96 return updateSnapshot(bridge.getLocalEnvironmentBootstraps());
97 } catch (cause) {
98 return { _tag: "Failure", cause };
99 }
100 };
101​
102 return {
103 readResult,
104 refresh: async () => {
105 const bridge = resolveBridge();
106 if (bridge?.readLocalEnvironmentBootstraps === undefined) return readResult();
107 try {
108 return updateSnapshot(await bridge.readLocalEnvironmentBootstraps());
109 } catch (cause) {
110 return { _tag: "Failure", cause };
111 }
112 },
113 readSnapshot: () => {
114 const result = readResult();
115 return result._tag === "Success" ? result.bootstraps : snapshot;
116 },
117 };
119​
120const desktopSecondaryBootstrapsReader = createDesktopSecondaryBootstrapsReader(
121 () => window.desktopBridge,
122);
123​
124export function readDesktopSecondaryBootstrapsResult(): Promise<DesktopSecondaryBootstrapsRead> {
125 return desktopSecondaryBootstrapsReader.refresh();
127​
128export function readDesktopSecondaryBootstraps(): ReadonlyArray<DesktopEnvironmentBootstrap> {
129 return desktopSecondaryBootstrapsReader.readSnapshot();