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

shell.ts

86 lines3.6 KB

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