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

shell.ts

106 lines4.2 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} 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/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 if (connection.phase === "backoff" && connection.desired && connection.attempt <= 2) {
44 return false;
45 }
46 }
47 return true;
48}).pipe(Atom.withLabel("web-all-environment-shells-bootstrapped"));
49​
50/**
51 * The persisted catalog can emit before platform discovery registers the primary environment.
52 * Neither that gap nor an empty catalog proves a project is absent.
53 */
54function catalogIsComplete(
55 catalog: EnvironmentCatalogState,
56 requiresPrimaryEnvironment: boolean,
57): boolean {
58 if (!catalog.isReady || catalog.entries.size === 0) return false;
59 return (
60 !requiresPrimaryEnvironment ||
61 Array.from(catalog.entries.values()).some(
62 (entry) => entry.target._tag === "PrimaryConnectionTarget",
63 )
64 );
66​
67export function createAllEnvironmentProjectSnapshotsReadyAtom(input: {
68 readonly catalogValueAtom: Atom.Atom<EnvironmentCatalogState>;
69 readonly shellStateValueAtom: (environmentId: EnvironmentId) => Atom.Atom<EnvironmentShellState>;
70 readonly requiresPrimaryEnvironment: boolean;
71}) {
72 return Atom.make((get) => {
73 const catalog = get(input.catalogValueAtom);
74 if (!catalogIsComplete(catalog, input.requiresPrimaryEnvironment)) return false;
75 for (const environmentId of enabledEnvironmentIds(catalog)) {
76 const shell = get(input.shellStateValueAtom(environmentId));
77 if (shell.status !== "live" || Option.isNone(shell.snapshot)) return false;
78 }
79 return true;
80 }).pipe(Atom.withLabel("web-all-environment-project-snapshots-ready"));
82​
83export const allEnvironmentProjectSnapshotsReadyAtom =
84 createAllEnvironmentProjectSnapshotsReadyAtom({
85 catalogValueAtom: environmentCatalog.catalogValueAtom,
86 shellStateValueAtom: environmentShell.stateValueAtom,
87 requiresPrimaryEnvironment: !isHostedStaticApp(),
88 });
89​
90export const allReachableShellsLiveAtom = Atom.make((get) => {
91 const catalog = get(environmentCatalog.catalogValueAtom);
92 if (!catalogIsComplete(catalog, !isHostedStaticApp())) return false;
93 for (const environmentId of enabledEnvironmentIds(catalog)) {
94 const shell = get(environmentShell.stateValueAtom(environmentId));
95 if (shell.status === "live" && Option.isSome(shell.snapshot)) continue;
96 const state = AsyncResult.value(get(environmentCatalog.stateAtom(environmentId)));
97 if (Option.isNone(state)) return false;
98 const connection = state.value;
99 const settledOffline =
100 connection.phase === "offline" ||
101 connection.phase === "blocked" ||
102 (connection.phase === "backoff" && connection.attempt > 2);
103 if (!settledOffline) return false;
104 }
105 return true;
106}).pipe(Atom.withLabel("web-all-reachable-shells-live"));