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

remote-environment-projections.ts

120 lines4.4 KB

1import type {
2 EnvironmentPresentation,
3 PreparedConnection,
4} from "@t3tools/client-runtime/connection";
5import { connectionCatalogDisplayUrl } from "@t3tools/client-runtime/connection";
6import type { EnvironmentId, ServerConfig } from "@t3tools/contracts";
7import * as Option from "effect/Option";
8import { Atom } from "effect/unstable/reactivity";
9​
10import type { SavedRemoteConnection } from "../lib/connection";
11import type { EnvironmentRuntimeState } from "./remote-runtime-types";
12​
13export function createRemoteEnvironmentProjectionAtoms(input: {
14 readonly presentationAtom: (
15 environmentId: EnvironmentId,
16 ) => Atom.Atom<EnvironmentPresentation | null>;
17 readonly preparedConnectionAtom: (
18 environmentId: EnvironmentId,
19 ) => Atom.Atom<Option.Option<PreparedConnection>>;
20 readonly serverConfigAtom: (environmentId: EnvironmentId) => Atom.Atom<ServerConfig | null>;
21}) {
22 const savedConnectionAtom = Atom.family((environmentId: EnvironmentId) => {
23 let previousEntry: EnvironmentPresentation["entry"] | null = null;
24 let previousPrepared: PreparedConnection | null = null;
25 let previous: SavedRemoteConnection | null = null;
26​
27 return Atom.make((get) => {
28 const presentation = get(input.presentationAtom(environmentId));
29 if (presentation === null) {
30 previousEntry = null;
31 previousPrepared = null;
32 previous = null;
33 return null;
34 }
35​
36 const prepared = Option.getOrNull(get(input.preparedConnectionAtom(environmentId)));
37 if (
38 previous !== null &&
39 presentation.entry === previousEntry &&
40 prepared === previousPrepared
41 ) {
42 return previous;
43 }
44​
45 const displayUrl = connectionCatalogDisplayUrl(presentation.entry) ?? "";
46 const httpBaseUrl = prepared?.httpBaseUrl ?? displayUrl;
47 const socketUrl = prepared?.socketUrl ?? "";
48 const wsBaseUrl =
49 socketUrl === ""
50 ? displayUrl.startsWith("https://")
51 ? displayUrl.replace(/^https:/, "wss:")
52 : displayUrl.replace(/^http:/, "ws:")
53 : new URL(socketUrl).origin;
54 const authorization = prepared?.httpAuthorization ?? null;
55 const relayManaged = presentation.entry.target._tag === "RelayConnectionTarget";
56​
57 previousEntry = presentation.entry;
58 previousPrepared = prepared;
59 previous = {
60 environmentId,
61 environmentLabel: presentation.entry.target.label,
62 pairingUrl: displayUrl,
63 displayUrl,
64 httpBaseUrl,
65 wsBaseUrl,
66 bearerToken: authorization?._tag === "Bearer" ? authorization.token : null,
67 ...(relayManaged
68 ? {
69 authenticationMethod: "dpop" as const,
70 relayManaged: true as const,
71 ...(authorization?._tag === "Dpop"
72 ? { dpopAccessToken: authorization.accessToken }
73 : {}),
74 }
75 : { authenticationMethod: "bearer" as const }),
76 };
77 return previous;
78 }).pipe(Atom.withLabel(`mobile:saved-connection:${environmentId}`));
79 });
80​
81 const runtimeStateAtom = Atom.family((environmentId: EnvironmentId) => {
82 let previousConnection: EnvironmentPresentation["connection"] | null = null;
83 let previousServerConfig: ServerConfig | null = null;
84 let previous: EnvironmentRuntimeState | null = null;
85​
86 return Atom.make((get) => {
87 const presentation = get(input.presentationAtom(environmentId));
88 if (presentation === null) {
89 previousConnection = null;
90 previousServerConfig = null;
91 previous = null;
92 return null;
93 }
94​
95 const connection = presentation.connection;
96 const serverConfig = get(input.serverConfigAtom(environmentId));
97 if (
98 previous !== null &&
99 connection.phase === previousConnection?.phase &&
100 connection.error === previousConnection?.error &&
101 connection.traceId === previousConnection?.traceId &&
102 serverConfig === previousServerConfig
103 ) {
104 return previous;
105 }
106​
107 previousConnection = connection;
108 previousServerConfig = serverConfig;
109 previous = {
110 connectionState: connection.phase,
111 connectionError: connection.error,
112 connectionErrorTraceId: connection.traceId,
113 serverConfig,
114 };
115 return previous;
116 }).pipe(Atom.withLabel(`mobile:environment-runtime-state:${environmentId}`));
117 });
118​
119 return { savedConnectionAtom, runtimeStateAtom };