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

shell.ts

131 lines5.0 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 { 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 );
43​
44export function useEnvironmentShellHasSnapshot(environmentId: EnvironmentId | null): boolean {
45 return useAtomValue(
46 environmentId === null
47 ? NO_SHELL_SNAPSHOT_ATOM
48 : environmentShell.hasSnapshotAtom(environmentId),
49 );
51​
52export const allEnvironmentShellsBootstrappedAtom = Atom.make((get) => {
53 const catalog = AsyncResult.value(get(environmentCatalog.catalogAtom));
54 if (Option.isNone(catalog)) {
55 return false;
56 }
57 for (const environmentId of enabledEnvironmentIds(catalog.value)) {
58 if (Option.isSome(get(environmentShell.stateValueAtom(environmentId)).snapshot)) {
59 continue;
60 }
61 const connection = Option.getOrElse(
62 AsyncResult.value(get(environmentCatalog.stateAtom(environmentId))),
63 () => AVAILABLE_CONNECTION_STATE,
64 );
65 if (connectionProjectionPhase(connection) !== "disconnected") {
66 return false;
67 }
68 if (connection.phase === "backoff" && connection.desired && connection.attempt <= 2) {
69 return false;
70 }
71 }
72 return true;
73}).pipe(Atom.withLabel("web-all-environment-shells-bootstrapped"));
74​
75/**
76 * The persisted catalog can emit before platform discovery registers the primary environment.
77 * Neither that gap nor an empty catalog proves a project is absent.
78 */
79function catalogIsComplete(
80 catalog: EnvironmentCatalogState,
81 requiresPrimaryEnvironment: boolean,
82): boolean {
83 if (!catalog.isReady || catalog.entries.size === 0) return false;
84 return (
85 !requiresPrimaryEnvironment ||
86 Array.from(catalog.entries.values()).some(
87 (entry) => entry.target._tag === "PrimaryConnectionTarget",
88 )
89 );
91​
92export function createAllEnvironmentProjectSnapshotsReadyAtom(input: {
93 readonly catalogValueAtom: Atom.Atom<EnvironmentCatalogState>;
94 readonly shellStateValueAtom: (environmentId: EnvironmentId) => Atom.Atom<EnvironmentShellState>;
95 readonly requiresPrimaryEnvironment: boolean;
96}) {
97 return Atom.make((get) => {
98 const catalog = get(input.catalogValueAtom);
99 if (!catalogIsComplete(catalog, input.requiresPrimaryEnvironment)) return false;
100 for (const environmentId of enabledEnvironmentIds(catalog)) {
101 const shell = get(input.shellStateValueAtom(environmentId));
102 if (shell.status !== "live" || Option.isNone(shell.snapshot)) return false;
103 }
104 return true;
105 }).pipe(Atom.withLabel("web-all-environment-project-snapshots-ready"));
107​
108export const allEnvironmentProjectSnapshotsReadyAtom =
109 createAllEnvironmentProjectSnapshotsReadyAtom({
110 catalogValueAtom: environmentCatalog.catalogValueAtom,
111 shellStateValueAtom: environmentShell.stateValueAtom,
112 requiresPrimaryEnvironment: !isHostedStaticApp(),
113 });
114​
115export const allReachableShellsLiveAtom = Atom.make((get) => {
116 const catalog = get(environmentCatalog.catalogValueAtom);
117 if (!catalogIsComplete(catalog, !isHostedStaticApp())) return false;
118 for (const environmentId of enabledEnvironmentIds(catalog)) {
119 const shell = get(environmentShell.stateValueAtom(environmentId));
120 if (shell.status === "live" && Option.isSome(shell.snapshot)) continue;
121 const state = AsyncResult.value(get(environmentCatalog.stateAtom(environmentId)));
122 if (Option.isNone(state)) return false;
123 const connection = state.value;
124 const settledOffline =
125 connection.phase === "offline" ||
126 connection.phase === "blocked" ||
127 (connection.phase === "backoff" && connection.attempt > 2);
128 if (!settledOffline) return false;
129 }
130 return true;
131}).pipe(Atom.withLabel("web-all-reachable-shells-live"));