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/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 PullRequestRoutingPermissions,
14 type PullRequestRoutingPermission,
15 type StoredPullRequestRoutingPermission,
16} from "../connection/pullRequestRoutingPermissions.ts";
17import {
18 createAtomCommandScheduler,
19 createRuntimeCommand,
20 followStreamInEnvironment,
21} from "./runtime.ts";
22
23export interface EnvironmentCatalogState {
24 readonly isReady: boolean;
25 readonly entries: ReadonlyMap<EnvironmentIdType, ConnectionCatalogEntry>;
26}
27
28export function* enabledEnvironmentIds(
29 catalog: EnvironmentCatalogState,
30): Generator<EnvironmentIdType> {
31 for (const [environmentId, entry] of catalog.entries) {
32 if (entry.enabled && entry.unsupportedReason === undefined) {
33 yield environmentId;
34 }
35 }
36}
37
38const EMPTY_ENVIRONMENT_CATALOG_STATE: EnvironmentCatalogState = Object.freeze({
39 isReady: false,
40 entries: new Map(),
41});
42
43export function createEnvironmentCatalogAtoms<R, E>(
44 runtime: Atom.AtomRuntime<EnvironmentRegistry.EnvironmentRegistry | R, E>,
45) {
46 const commandScheduler = createAtomCommandScheduler();
47 const serial = { mode: "serial" as const, key: () => "environment-catalog" };
48 const catalogAtom = runtime.atom(
49 Stream.unwrap(
50 EnvironmentRegistry.EnvironmentRegistry.pipe(
51 Effect.map((registry) =>
52 SubscriptionRef.changes(registry.entries).pipe(
53 Stream.map((entries) => ({
54 isReady: true,
55 entries,
56 })),
57 ),
58 ),
59 ),
60 ),
61 { initialValue: EMPTY_ENVIRONMENT_CATALOG_STATE },
62 );
63
64 const catalogValueAtom = Atom.make((get) =>
65 Option.getOrElse(AsyncResult.value(get(catalogAtom)), () => EMPTY_ENVIRONMENT_CATALOG_STATE),
66 ).pipe(Atom.withLabel("environment-catalog-value"));
67
68 const pullRequestRoutingPermissionsAtom = runtime.atom(
69 Stream.unwrap(
70 PullRequestRoutingPermissions.pipe(Effect.map((permissions) => permissions.changes)),
71 ),
72 { initialValue: [] as ReadonlyArray<StoredPullRequestRoutingPermission> },
73 );
74 const pullRequestRoutingPermissionsValueAtom = Atom.make((get) =>
75 Option.getOrElse(AsyncResult.value(get(pullRequestRoutingPermissionsAtom)), () => []),
76 ).pipe(Atom.withLabel("environment-pull-request-routing-permissions"));
77 const setPullRequestRoutingPermission = createRuntimeCommand(runtime, {
78 label: "environment-catalog:pull-request-routing-permission",
79 scheduler: commandScheduler,
80 concurrency: serial,
81 execute: Effect.fn(function* (input: {
82 readonly environmentId: EnvironmentIdType;
83 readonly permission: PullRequestRoutingPermission;
84 }) {
85 const registry = yield* EnvironmentRegistry.EnvironmentRegistry;
86 const entry = (yield* SubscriptionRef.get(registry.entries)).get(input.environmentId);
87 if (entry === undefined)
88 return yield* new EnvironmentRegistry.EnvironmentNotRegisteredError({
89 environmentId: input.environmentId,
90 });
91 const permissions = yield* PullRequestRoutingPermissions;
92 yield* permissions.set(entry, input.permission);
93 }),
94 });
95
96 const networkStatusAtom = runtime.atom(
97 Stream.unwrap(
98 EnvironmentRegistry.EnvironmentRegistry.pipe(
99 Effect.map((registry) => SubscriptionRef.changes(registry.networkStatus)),
100 ),
101 ),
102 { initialValue: "unknown" as const },
103 );
104
105 const networkStatusValueAtom = Atom.make((get) =>
106 Option.getOrElse(AsyncResult.value(get(networkStatusAtom)), () => "unknown" as const),
107 ).pipe(Atom.withLabel("environment-network-status-value"));
108
109 const stateAtom = Atom.family((environmentId: EnvironmentIdType) =>
110 runtime.atom(
111 followStreamInEnvironment(
112 environmentId,
113 Stream.unwrap(
114 EnvironmentSupervisor.EnvironmentSupervisor.pipe(
115 Effect.map((supervisor) => SubscriptionRef.changes(supervisor.state)),
116 ),
117 ),
118 ),
119 { initialValue: AVAILABLE_CONNECTION_STATE },
120 ),
121 );
122
123 const register = createRuntimeCommand(runtime, {
124 label: "environment-catalog:register",
125 scheduler: commandScheduler,
126 concurrency: serial,
127 execute: (
128 target: Parameters<EnvironmentRegistry.EnvironmentRegistry["Service"]["register"]>[0],
129 ) =>
130 EnvironmentRegistry.EnvironmentRegistry.pipe(
131 Effect.flatMap((registry) => registry.register(target)),
132 ),
133 });
134 const remove = createRuntimeCommand(runtime, {
135 label: "environment-catalog:remove",
136 scheduler: commandScheduler,
137 concurrency: serial,
138 execute: (environmentId: EnvironmentIdType) =>
139 EnvironmentRegistry.EnvironmentRegistry.pipe(
140 Effect.flatMap((registry) => registry.remove(environmentId)),
141 ),
142 });
143 const removeRelayEnvironments = createRuntimeCommand(runtime, {
144 label: "environment-catalog:remove-relay-environments",
145 scheduler: commandScheduler,
146 concurrency: serial,
147 execute: (_input: void) =>
148 EnvironmentRegistry.EnvironmentRegistry.pipe(
149 Effect.flatMap((registry) => registry.removeRelayEnvironments()),
150 ),
151 });
152 const setEnabled = createRuntimeCommand(runtime, {
153 label: "environment-catalog:set-enabled",
154 scheduler: commandScheduler,
155 concurrency: serial,
156 execute: (input: { readonly environmentId: EnvironmentIdType; readonly enabled: boolean }) =>
157 EnvironmentRegistry.EnvironmentRegistry.pipe(
158 Effect.flatMap((registry) => registry.setEnabled(input.environmentId, input.enabled)),
159 ),
160 });
161 const removeRoute = createRuntimeCommand(runtime, {
162 label: "environment-catalog:remove-route",
163 scheduler: commandScheduler,
164 concurrency: serial,
165 execute: (input: { readonly environmentId: EnvironmentIdType; readonly routeId: string }) =>
166 EnvironmentRegistry.EnvironmentRegistry.pipe(
167 Effect.flatMap((registry) => registry.removeRoute(input.environmentId, input.routeId)),
168 ),
169 });
170 const reorderRoutes = createRuntimeCommand(runtime, {
171 label: "environment-catalog:reorder-routes",
172 scheduler: commandScheduler,
173 concurrency: serial,
174 execute: (input: {
175 readonly environmentId: EnvironmentIdType;
176 readonly routeIds: ReadonlyArray<string>;
177 }) =>
178 EnvironmentRegistry.EnvironmentRegistry.pipe(
179 Effect.flatMap((registry) => registry.reorderRoutes(input.environmentId, input.routeIds)),
180 ),
181 });
182 const retryNow = createRuntimeCommand(runtime, {
183 label: "environment-catalog:retry-now",
184 scheduler: commandScheduler,
185 concurrency: serial,
186 execute: (environmentId: EnvironmentIdType) =>
187 EnvironmentRegistry.EnvironmentRegistry.pipe(
188 Effect.flatMap((registry) => registry.retryNow(environmentId)),
189 ),
190 });
191
192 return {
193 catalogAtom,
194 catalogValueAtom,
195 pullRequestRoutingPermissionsValueAtom,
196 setPullRequestRoutingPermission,
197 networkStatusAtom,
198 networkStatusValueAtom,
199 stateAtom,
200 register,
201 remove,
202 removeRoute,
203 reorderRoutes,
204 removeRelayEnvironments,
205 retryNow,
206 setEnabled,
207 };
208}