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} from "@t3tools/client-runtime/state/shell";
11import {
12 type EnvironmentCatalogState,
13 enabledEnvironmentIds,
14} from "@t3tools/client-runtime/state/connections";
15import type { EnvironmentId } from "@t3tools/contracts";
16import * as Option from "effect/Option";
17import { AsyncResult, Atom } from "effect/unstable/reactivity";
18
19import { environmentCatalog } from "../connection/catalog";
20import { connectionAtomRuntime } from "../connection/runtime";
21import { isHostedStaticApp } from "../hostedPairing";
22
23export const shellEnvironment = createShellEnvironmentAtoms(connectionAtomRuntime);
24export const environmentShell = createEnvironmentShellAtoms(connectionAtomRuntime);
25export const environmentSnapshotAtom = createEnvironmentSnapshotAtom(environmentShell.stateAtom);
26
27export const allEnvironmentShellsBootstrappedAtom = Atom.make((get) => {
28 const catalog = AsyncResult.value(get(environmentCatalog.catalogAtom));
29 if (Option.isNone(catalog)) {
30 return false;
31 }
32 for (const environmentId of enabledEnvironmentIds(catalog.value)) {
33 if (Option.isSome(get(environmentShell.stateValueAtom(environmentId)).snapshot)) {
34 continue;
35 }
36 const connection = Option.getOrElse(
37 AsyncResult.value(get(environmentCatalog.stateAtom(environmentId))),
38 () => AVAILABLE_CONNECTION_STATE,
39 );
40 if (connectionProjectionPhase(connection) !== "disconnected") {
41 return false;
42 }
43 // A retrying environment is only transiently disconnected; give it its
44 // first retries before letting the landing settle without its snapshot.
45 if (connection.phase === "backoff" && connection.desired && connection.attempt <= 2) {
46 return false;
47 }
48 }
49 return true;
50}).pipe(Atom.withLabel("web-all-environment-shells-bootstrapped"));
51
52/** Cached or missing snapshots cannot establish that a saved project no longer exists. */
53export function createAllEnvironmentProjectSnapshotsReadyAtom(input: {
54 readonly catalogValueAtom: Atom.Atom<EnvironmentCatalogState>;
55 readonly shellStateValueAtom: (environmentId: EnvironmentId) => Atom.Atom<EnvironmentShellState>;
56 readonly requiresPrimaryEnvironment: boolean;
57}) {
58 return Atom.make((get) => {
59 const catalog = get(input.catalogValueAtom);
60 // The persisted catalog can emit before platform discovery registers the
61 // primary environment. Neither that gap nor an empty catalog proves absence.
62 if (!catalog.isReady || catalog.entries.size === 0) return false;
63 if (
64 input.requiresPrimaryEnvironment &&
65 !Array.from(catalog.entries.values()).some(
66 (entry) => entry.target._tag === "PrimaryConnectionTarget",
67 )
68 ) {
69 return false;
70 }
71 for (const environmentId of enabledEnvironmentIds(catalog)) {
72 const shell = get(input.shellStateValueAtom(environmentId));
73 if (shell.status !== "live" || Option.isNone(shell.snapshot)) return false;
74 }
75 return true;
76 }).pipe(Atom.withLabel("web-all-environment-project-snapshots-ready"));
77}
78
79export const allEnvironmentProjectSnapshotsReadyAtom =
80 createAllEnvironmentProjectSnapshotsReadyAtom({
81 catalogValueAtom: environmentCatalog.catalogValueAtom,
82 shellStateValueAtom: environmentShell.stateValueAtom,
83 requiresPrimaryEnvironment: !isHostedStaticApp(),
84 });