Open History to see the last commit
1import type { EnvironmentId as EnvironmentIdType } from "@t3tools/contracts";
2import * as Effect from "effect/Effect";
3import * as Option from "effect/Option";
4import * as Stream from "effect/Stream";
5import * as SubscriptionRef from "effect/SubscriptionRef";
6import { AsyncResult, Atom } from "effect/unstable/reactivity";
7
8import * as EnvironmentRegistry from "../connection/registry.ts";
9import type { ConnectionCatalogEntry } from "../connection/catalog.ts";
10import { AVAILABLE_CONNECTION_STATE } from "../connection/model.ts";
11import * as EnvironmentSupervisor from "../connection/supervisor.ts";
12import {
13 createAtomCommandScheduler,
14 createRuntimeCommand,
15 followStreamInEnvironment,
16} from "./runtime.ts";
17
18export interface EnvironmentCatalogState {
19 readonly isReady: boolean;
20 readonly entries: ReadonlyMap<EnvironmentIdType, ConnectionCatalogEntry>;
21}
22
23export const EMPTY_ENVIRONMENT_CATALOG_STATE: EnvironmentCatalogState = Object.freeze({
24 isReady: false,
25 entries: new Map(),
26});
27
28export function createEnvironmentCatalogAtoms<R, E>(
29 runtime: Atom.AtomRuntime<EnvironmentRegistry.EnvironmentRegistry | R, E>,
30) {
31 const commandScheduler = createAtomCommandScheduler();
32 const serial = { mode: "serial" as const, key: () => "environment-catalog" };
33 const catalogAtom = runtime.atom(
34 Stream.unwrap(
35 EnvironmentRegistry.EnvironmentRegistry.pipe(
36 Effect.map((registry) =>
37 SubscriptionRef.changes(registry.entries).pipe(
38 Stream.map((entries) => ({
39 isReady: true,
40 entries,
41 })),
42 ),
43 ),
44 ),
45 ),
46 { initialValue: EMPTY_ENVIRONMENT_CATALOG_STATE },
47 );
48
49 const catalogValueAtom = Atom.make((get) =>
50 Option.getOrElse(AsyncResult.value(get(catalogAtom)), () => EMPTY_ENVIRONMENT_CATALOG_STATE),
51 ).pipe(Atom.withLabel("environment-catalog-value"));
52
53 const networkStatusAtom = runtime.atom(
54 Stream.unwrap(
55 EnvironmentRegistry.EnvironmentRegistry.pipe(
56 Effect.map((registry) => SubscriptionRef.changes(registry.networkStatus)),
57 ),
58 ),
59 { initialValue: "unknown" as const },
60 );
61
62 const networkStatusValueAtom = Atom.make((get) =>
63 Option.getOrElse(AsyncResult.value(get(networkStatusAtom)), () => "unknown" as const),
64 ).pipe(Atom.withLabel("environment-network-status-value"));
65
66 const stateAtom = Atom.family((environmentId: EnvironmentIdType) =>
67 runtime.atom(
68 followStreamInEnvironment(
69 environmentId,
70 Stream.unwrap(
71 EnvironmentSupervisor.EnvironmentSupervisor.pipe(
72 Effect.map((supervisor) => SubscriptionRef.changes(supervisor.state)),
73 ),
74 ),
75 ),
76 { initialValue: AVAILABLE_CONNECTION_STATE },
77 ),
78 );
79
80 const register = createRuntimeCommand(runtime, {
81 label: "environment-catalog:register",
82 scheduler: commandScheduler,
83 concurrency: serial,
84 execute: (
85 target: Parameters<EnvironmentRegistry.EnvironmentRegistry["Service"]["register"]>[0],
86 ) =>
87 EnvironmentRegistry.EnvironmentRegistry.pipe(
88 Effect.flatMap((registry) => registry.register(target)),
89 ),
90 });
91 const remove = createRuntimeCommand(runtime, {
92 label: "environment-catalog:remove",
93 scheduler: commandScheduler,
94 concurrency: serial,
95 execute: (environmentId: EnvironmentIdType) =>
96 EnvironmentRegistry.EnvironmentRegistry.pipe(
97 Effect.flatMap((registry) => registry.remove(environmentId)),
98 ),
99 });
100 const removeRelayEnvironments = createRuntimeCommand(runtime, {
101 label: "environment-catalog:remove-relay-environments",
102 scheduler: commandScheduler,
103 concurrency: serial,
104 execute: (_input: void) =>
105 EnvironmentRegistry.EnvironmentRegistry.pipe(
106 Effect.flatMap((registry) => registry.removeRelayEnvironments()),
107 ),
108 });
109 const retryNow = createRuntimeCommand(runtime, {
110 label: "environment-catalog:retry-now",
111 scheduler: commandScheduler,
112 concurrency: serial,
113 execute: (environmentId: EnvironmentIdType) =>
114 EnvironmentRegistry.EnvironmentRegistry.pipe(
115 Effect.flatMap((registry) => registry.retryNow(environmentId)),
116 ),
117 });
118
119 return {
120 catalogAtom,
121 catalogValueAtom,
122 networkStatusAtom,
123 networkStatusValueAtom,
124 stateAtom,
125 register,
126 remove,
127 removeRelayEnvironments,
128 retryNow,
129 };
130}