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

shell.ts

154 lines5.8 KB

1import {
2 AVAILABLE_CONNECTION_STATE,
3 connectionProjectionPhase,
4} from "@t3tools/client-runtime/connection";
5import {
6 createEnvironmentShellAtoms,
7 createEnvironmentSnapshotAtom,
8 createShellEnvironmentAtoms,
9 type EnvironmentShellState,
10 type EnvironmentShellStatus,
11} from "@t3tools/client-runtime/state/shell";
12import {
13 type EnvironmentCatalogState,
14 enabledEnvironmentIds,
15} from "@t3tools/client-runtime/state/connections";
16import type { EnvironmentId } from "@t3tools/contracts";
17import { useAtomValue } from "@effect/atom-react";
18import * as Option from "effect/Option";
19import { AsyncResult, Atom } from "effect/reactivity";
20​
21import { environmentCatalog } from "../connection/catalog";
22import { primaryEnvironmentMissingAtom } from "../connection/platform";
23import { connectionAtomRuntime } from "../connection/runtime";
24import { isHostedStaticApp } from "../hostedPairing";
25import { isLocalEnvironmentDisabled } from "../localEnvironment";
26import { appAtomRegistry } from "../rpc/atomRegistry";
27​
28export const shellEnvironment = createShellEnvironmentAtoms(connectionAtomRuntime);
29export const environmentShell = createEnvironmentShellAtoms(connectionAtomRuntime);
30export const environmentSnapshotAtom = createEnvironmentSnapshotAtom(environmentShell.stateAtom);
31​
32const NO_SHELL_STATUS_ATOM = Atom.make<EnvironmentShellStatus | null>(null).pipe(
33 Atom.withLabel("web-environment-shell-status:none"),
34);
35const NO_SHELL_SNAPSHOT_ATOM = Atom.make(false).pipe(
36 Atom.withLabel("web-environment-shell-has-snapshot:none"),
37);
38​
39export function useEnvironmentShellStatus(
40 environmentId: EnvironmentId | null,
41): EnvironmentShellStatus | null {
42 return useAtomValue(
43 environmentId === null ? NO_SHELL_STATUS_ATOM : environmentShell.statusAtom(environmentId),
44 );
46​
47export function useEnvironmentShellHasSnapshot(environmentId: EnvironmentId | null): boolean {
48 return useAtomValue(
49 environmentId === null
50 ? NO_SHELL_SNAPSHOT_ATOM
51 : environmentShell.hasSnapshotAtom(environmentId),
52 );
54​
55export const allEnvironmentShellsBootstrappedAtom = Atom.make((get) => {
56 const catalog = AsyncResult.value(get(environmentCatalog.catalogAtom));
57 if (Option.isNone(catalog)) {
58 return false;
59 }
60 // Wait for primary discovery before treating a persisted catalog as complete.
61 if (
62 !isHostedStaticApp() &&
63 !isLocalEnvironmentDisabled() &&
64 !get(primaryEnvironmentMissingAtom) &&
65 !Array.from(catalog.value.entries.values()).some(
66 (entry) => entry.target._tag === "PrimaryConnectionTarget",
67 )
68 ) {
69 return false;
70 }
71 for (const environmentId of enabledEnvironmentIds(catalog.value)) {
72 if (Option.isSome(get(environmentShell.stateValueAtom(environmentId)).snapshot)) {
73 continue;
74 }
75 const connection = Option.getOrElse(
76 AsyncResult.value(get(environmentCatalog.stateAtom(environmentId))),
77 () => AVAILABLE_CONNECTION_STATE,
78 );
79 if (connectionProjectionPhase(connection) !== "disconnected") {
80 return false;
81 }
82 if (connection.phase === "backoff" && connection.desired && connection.attempt <= 2) {
83 return false;
84 }
85 }
86 return true;
87}).pipe(Atom.withLabel("web-all-environment-shells-bootstrapped"));
88​
89let environmentShellsStarted = false;
90​
91// Keep the shell mounted for the session, starting cache reads and connections before render.
92export function startEnvironmentShells(): void {
93 if (environmentShellsStarted) return;
94 environmentShellsStarted = true;
95 appAtomRegistry.mount(allEnvironmentShellsBootstrappedAtom);
97​
98/**
99 * The persisted catalog can emit before platform discovery registers the primary environment.
100 * Neither that gap nor an empty catalog proves a project is absent.
101 */
102function catalogIsComplete(
103 catalog: EnvironmentCatalogState,
104 requiresPrimaryEnvironment: boolean,
105): boolean {
106 if (!catalog.isReady || catalog.entries.size === 0) return false;
107 return (
108 !requiresPrimaryEnvironment ||
109 Array.from(catalog.entries.values()).some(
110 (entry) => entry.target._tag === "PrimaryConnectionTarget",
111 )
112 );
114​
115export function createAllEnvironmentProjectSnapshotsReadyAtom(input: {
116 readonly catalogValueAtom: Atom.Atom<EnvironmentCatalogState>;
117 readonly shellStateValueAtom: (environmentId: EnvironmentId) => Atom.Atom<EnvironmentShellState>;
118 readonly requiresPrimaryEnvironment: boolean;
119}) {
120 return Atom.make((get) => {
121 const catalog = get(input.catalogValueAtom);
122 if (!catalogIsComplete(catalog, input.requiresPrimaryEnvironment)) return false;
123 for (const environmentId of enabledEnvironmentIds(catalog)) {
124 const shell = get(input.shellStateValueAtom(environmentId));
125 if (shell.status !== "live" || Option.isNone(shell.snapshot)) return false;
126 }
127 return true;
128 }).pipe(Atom.withLabel("web-all-environment-project-snapshots-ready"));
130​
131export const allEnvironmentProjectSnapshotsReadyAtom =
132 createAllEnvironmentProjectSnapshotsReadyAtom({
133 catalogValueAtom: environmentCatalog.catalogValueAtom,
134 shellStateValueAtom: environmentShell.stateValueAtom,
135 requiresPrimaryEnvironment: !isHostedStaticApp(),
136 });
137​
138export const allReachableShellsLiveAtom = Atom.make((get) => {
139 const catalog = get(environmentCatalog.catalogValueAtom);
140 if (!catalogIsComplete(catalog, !isHostedStaticApp())) return false;
141 for (const environmentId of enabledEnvironmentIds(catalog)) {
142 const shell = get(environmentShell.stateValueAtom(environmentId));
143 if (shell.status === "live" && Option.isSome(shell.snapshot)) continue;
144 const state = AsyncResult.value(get(environmentCatalog.stateAtom(environmentId)));
145 if (Option.isNone(state)) return false;
146 const connection = state.value;
147 const settledOffline =
148 connection.phase === "offline" ||
149 connection.phase === "blocked" ||
150 (connection.phase === "backoff" && connection.attempt > 2);
151 if (!settledOffline) return false;
152 }
153 return true;
154}).pipe(Atom.withLabel("web-all-reachable-shells-live"));