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

shell.ts

48 lines1.9 KB

1import {
2 AVAILABLE_CONNECTION_STATE,
3 connectionProjectionPhase,
4} from "@t3tools/client-runtime/connection";
5import {
6 createEnvironmentShellAtoms,
7 createEnvironmentShellSummaryAtom,
8 createEnvironmentSnapshotAtom,
9 createShellEnvironmentAtoms,
10} from "@t3tools/client-runtime/state/shell";
11import * as Option from "effect/Option";
12import { AsyncResult, Atom } from "effect/unstable/reactivity";
13​
14import { environmentCatalog } from "../connection/catalog";
15import { connectionAtomRuntime } from "../connection/runtime";
16​
17export const shellEnvironment = createShellEnvironmentAtoms(connectionAtomRuntime);
18export const environmentShell = createEnvironmentShellAtoms(connectionAtomRuntime);
19export const environmentSnapshotAtom = createEnvironmentSnapshotAtom(environmentShell.stateAtom);
20export const environmentShellSummaryAtom = createEnvironmentShellSummaryAtom({
21 catalogValueAtom: environmentCatalog.catalogValueAtom,
22 shellStateValueAtom: environmentShell.stateValueAtom,
23});
24​
25export const allEnvironmentShellsBootstrappedAtom = Atom.make((get) => {
26 const catalog = AsyncResult.value(get(environmentCatalog.catalogAtom));
27 if (Option.isNone(catalog)) {
28 return false;
29 }
30 for (const environmentId of catalog.value.entries.keys()) {
31 if (Option.isSome(get(environmentShell.stateValueAtom(environmentId)).snapshot)) {
32 continue;
33 }
34 const connection = Option.getOrElse(
35 AsyncResult.value(get(environmentCatalog.stateAtom(environmentId))),
36 () => AVAILABLE_CONNECTION_STATE,
37 );
38 if (connectionProjectionPhase(connection) !== "disconnected") {
39 return false;
40 }
41 // A retrying environment is only transiently disconnected; give it its
42 // first retries before letting the landing settle without its snapshot.
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"));