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

session.ts

95 lines3.3 KB

1import type { EnvironmentId, ServerConfig } from "@v12/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 { EnvironmentRegistry } from "../connection/registry.ts";
9import type { PreparedConnection } from "../connection/model.ts";
10import { EnvironmentSupervisor } from "../connection/supervisor.ts";
11import { safeErrorLogAttributes } from "../errors/safeLog.ts";
12import { followStreamInEnvironment } from "./runtime.ts";
13​
14export function initialConfigOption<E>(
15 initialConfig: Effect.Effect<ServerConfig, E>,
16): Effect.Effect<Option.Option<ServerConfig>> {
17 return initialConfig.pipe(
18 Effect.map(Option.some),
19 Effect.catch((error) =>
20 Effect.logWarning("Could not load the initial environment configuration.").pipe(
21 Effect.annotateLogs({ ...safeErrorLogAttributes(error) }),
22 Effect.as(Option.none<ServerConfig>()),
23 ),
24 ),
25 );
27​
28export function createEnvironmentSessionAtoms<R, E>(
29 runtime: Atom.AtomRuntime<EnvironmentRegistry | R, E>,
30) {
31 const initialConfigAtom = Atom.family((environmentId: EnvironmentId) =>
32 runtime.atom(
33 followStreamInEnvironment(
34 environmentId,
35 Stream.unwrap(
36 EnvironmentSupervisor.pipe(
37 Effect.map((supervisor) =>
38 SubscriptionRef.changes(supervisor.session).pipe(
39 Stream.mapEffect(
40 Option.match({
41 onNone: () => Effect.succeed(Option.none<ServerConfig>()),
42 onSome: (session) => initialConfigOption(session.initialConfig),
43 }),
44 ),
45 ),
46 ),
47 ),
48 ),
49 ),
50 { initialValue: Option.none() },
51 ),
52 );
53​
54 // This is only the bootstrap config captured when a transport session is
55 // established. Consumers that need current provider/settings state must use
56 // createServerEnvironmentAtoms(...).configValueAtom instead.
57 const initialConfigValueAtom = Atom.family((environmentId: EnvironmentId) =>
58 Atom.make((get): ServerConfig | null =>
59 Option.getOrNull(
60 Option.getOrElse(AsyncResult.value(get(initialConfigAtom(environmentId))), () =>
61 Option.none(),
62 ),
63 ),
64 ).pipe(Atom.withLabel(`environment-config-value:${environmentId}`)),
65 );
66​
67 const preparedConnectionAtom = Atom.family((environmentId: EnvironmentId) =>
68 runtime.atom(
69 followStreamInEnvironment(
70 environmentId,
71 Stream.unwrap(
72 EnvironmentSupervisor.pipe(
73 Effect.map((supervisor) => SubscriptionRef.changes(supervisor.prepared)),
74 ),
75 ),
76 ),
77 { initialValue: Option.none<PreparedConnection>() },
78 ),
79 );
80​
81 const preparedConnectionValueAtom = Atom.family((environmentId: EnvironmentId) =>
82 Atom.make((get) =>
83 Option.getOrElse(AsyncResult.value(get(preparedConnectionAtom(environmentId))), () =>
84 Option.none<PreparedConnection>(),
85 ),
86 ).pipe(Atom.withLabel(`environment-prepared-connection:${environmentId}`)),
87 );
88​
89 return {
90 initialConfigAtom,
91 initialConfigValueAtom,
92 preparedConnectionAtom,
93 preparedConnectionValueAtom,
94 };