Open History to see the last commit
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 { connectionAtomRuntime } from "../connection/runtime";
23import { isHostedStaticApp } from "../hostedPairing";
24
25export const shellEnvironment = createShellEnvironmentAtoms(connectionAtomRuntime);
26export const environmentShell = createEnvironmentShellAtoms(connectionAtomRuntime);
27export const environmentSnapshotAtom = createEnvironmentSnapshotAtom(environmentShell.stateAtom);
28
29const NO_SHELL_STATUS_ATOM = Atom.make<EnvironmentShellStatus | null>(null).pipe(
30 Atom.withLabel("web-environment-shell-status:none"),
31);
32const NO_SHELL_SNAPSHOT_ATOM = Atom.make(false).pipe(
33 Atom.withLabel("web-environment-shell-has-snapshot:none"),
34);
35
36export function useEnvironmentShellStatus(
37 environmentId: EnvironmentId | null,
38): EnvironmentShellStatus | null {
39 return useAtomValue(
40 environmentId === null ? NO_SHELL_STATUS_ATOM : environmentShell.statusAtom(environmentId),
41 );
42}
43
44export function useEnvironmentShellHasSnapshot(environmentId: EnvironmentId | null): boolean {
45 return useAtomValue(
46 environmentId === null
47 ? NO_SHELL_SNAPSHOT_ATOM
48 : environmentShell.hasSnapshotAtom(environmentId),
49 );
50}
51
52let previousLiveEnvironmentIds: ReadonlySet<EnvironmentId> = new Set();
53/** Environments whose threads reflect the server now, not a cache or a resync in flight. */
54export const liveShellEnvironmentIdsAtom = Atom.make((get) => {
55 const next = new Set<EnvironmentId>();
56 for (const environmentId of enabledEnvironmentIds(get(environmentCatalog.catalogValueAtom))) {
57 if (get(environmentShell.statusAtom(environmentId)) === "live") next.add(environmentId);
58 }
59 if (
60 next.size === previousLiveEnvironmentIds.size &&
61 [...next].every((environmentId) => previousLiveEnvironmentIds.has(environmentId))
62 ) {
63 return previousLiveEnvironmentIds;
64 }
65 previousLiveEnvironmentIds = next;
66 return next;
67}).pipe(Atom.withLabel("web-live-shell-environment-ids"));
68
69export const allEnvironmentShellsBootstrappedAtom = Atom.make((get) => {
70 const catalog = AsyncResult.value(get(environmentCatalog.catalogAtom));
71 if (Option.isNone(catalog)) {
72 return false;
73 }
74 for (const environmentId of enabledEnvironmentIds(catalog.value)) {
75 if (Option.isSome(get(environmentShell.stateValueAtom(environmentId)).snapshot)) {
76 continue;
77 }
78 const connection = AsyncResult.getOrElse(
79 get(environmentCatalog.stateAtom(environmentId)),
80 () => AVAILABLE_CONNECTION_STATE,
81 );
82 if (connectionProjectionPhase(connection) !== "disconnected") {
83 return false;
84 }
85 if (connection.phase === "backoff" && connection.desired && connection.attempt <= 2) {
86 return false;
87 }
88 }
89 return true;
90}).pipe(Atom.withLabel("web-all-environment-shells-bootstrapped"));
91
92// Catalog can precede primary discovery; an empty or incomplete catalog cannot prove absence.
93function catalogIsComplete(
94 catalog: EnvironmentCatalogState,
95 requiresPrimaryEnvironment: boolean,
96): boolean {
97 if (!catalog.isReady || catalog.entries.size === 0) return false;
98 return (
99 !requiresPrimaryEnvironment ||
100 Array.from(catalog.entries.values()).some(
101 (entry) => entry.target._tag === "PrimaryConnectionTarget",
102 )
103 );
104}
105
106export function createAllEnvironmentProjectSnapshotsReadyAtom(input: {
107 readonly catalogValueAtom: Atom.Atom<EnvironmentCatalogState>;
108 readonly shellStateValueAtom: (environmentId: EnvironmentId) => Atom.Atom<EnvironmentShellState>;
109 readonly requiresPrimaryEnvironment: boolean;
110}) {
111 return Atom.make((get) => {
112 const catalog = get(input.catalogValueAtom);
113 if (!catalogIsComplete(catalog, input.requiresPrimaryEnvironment)) return false;
114 for (const environmentId of enabledEnvironmentIds(catalog)) {
115 const shell = get(input.shellStateValueAtom(environmentId));
116 if (shell.status !== "live" || Option.isNone(shell.snapshot)) return false;
117 }
118 return true;
119 }).pipe(Atom.withLabel("web-all-environment-project-snapshots-ready"));
120}
121
122export const allEnvironmentProjectSnapshotsReadyAtom =
123 createAllEnvironmentProjectSnapshotsReadyAtom({
124 catalogValueAtom: environmentCatalog.catalogValueAtom,
125 shellStateValueAtom: environmentShell.stateValueAtom,
126 requiresPrimaryEnvironment: !isHostedStaticApp(),
127 });
128
129export const allReachableShellsLiveAtom = Atom.make((get) => {
130 const catalog = get(environmentCatalog.catalogValueAtom);
131 if (!catalogIsComplete(catalog, !isHostedStaticApp())) return false;
132 for (const environmentId of enabledEnvironmentIds(catalog)) {
133 const shell = get(environmentShell.stateValueAtom(environmentId));
134 if (shell.status === "live" && Option.isSome(shell.snapshot)) continue;
135 const state = AsyncResult.value(get(environmentCatalog.stateAtom(environmentId)));
136 if (Option.isNone(state)) return false;
137 const connection = state.value;
138 const settledOffline =
139 connection.phase === "offline" ||
140 connection.phase === "blocked" ||
141 (connection.phase === "backoff" && connection.attempt > 2);
142 if (!settledOffline) return false;
143 }
144 return true;
145}).pipe(Atom.withLabel("web-all-reachable-shells-live"));