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

shell.ts

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