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

server.ts

133 lines5.6 KB

1import {
2 DEFAULT_SERVER_SETTINGS,
3 type EditorId,
4 type EnvironmentTheme,
5 type ServerConfig,
6 type ServerConfigStreamEvent,
7 type ServerLifecycleLegacyThreadMigrationPayload,
8 type ServerLifecycleWelcomePayload,
9 type ServerProvider,
10 type ServerSettings,
11} from "@t3tools/contracts";
12import { createServerEnvironmentAtoms } from "@t3tools/client-runtime/state/server";
13import { createEnvironmentServerConfigsAtom } from "@t3tools/client-runtime/state/shell";
14import { DEFAULT_RESOLVED_KEYBINDINGS } from "@t3tools/shared/keybindings";
15import * as Option from "effect/Option";
16import { AsyncResult, Atom } from "effect/unstable/reactivity";
17​
18import { environmentCatalog } from "../connection/catalog";
19import { connectionAtomRuntime } from "../connection/runtime";
20import { primaryEnvironmentIdAtom } from "./primaryEnvironment";
21import { environmentSession } from "./session";
22​
23// Opted in for every environment, not just the primary one. Only the primary
24// environment's themes are rendered, but which environment is primary changes
25// at runtime and the subscription payload is fixed when it is established --
26// gating on "primary right now" would leave a newly promoted environment with
27// no themes until it reconnected. The set is capped server-side and stripped
28// before caching, so following all of them costs a few KB per environment.
29export const serverEnvironment = createServerEnvironmentAtoms(connectionAtomRuntime, {
30 initialConfigValueAtom: environmentSession.initialConfigValueAtom,
31 environmentThemes: true,
32});
33export const environmentServerConfigsAtom = createEnvironmentServerConfigsAtom({
34 catalogValueAtom: environmentCatalog.catalogValueAtom,
35 serverConfigValueAtom: serverEnvironment.configValueAtom,
36});
37​
38interface PrimaryServerState {
39 readonly config: ServerConfig | null;
40 readonly latestEvent: ServerConfigStreamEvent | null;
41 readonly welcome: ServerLifecycleWelcomePayload | null;
43​
44const EMPTY_AVAILABLE_EDITORS: ReadonlyArray<EditorId> = [];
45export const EMPTY_SERVER_PROVIDERS: ReadonlyArray<ServerProvider> = [];
46const EMPTY_PRIMARY_SERVER_STATE: PrimaryServerState = {
47 config: null,
48 latestEvent: null,
49 welcome: null,
50};
51​
52export const primaryServerStateAtom = Atom.make((get): PrimaryServerState => {
53 const environmentId = get(primaryEnvironmentIdAtom);
54 if (environmentId === null) {
55 return EMPTY_PRIMARY_SERVER_STATE;
56 }
57​
58 const target = { environmentId, input: {} };
59 const configProjection = Option.getOrNull(
60 AsyncResult.value(get(serverEnvironment.configProjection(target))),
61 );
62 const welcome = Option.getOrNull(AsyncResult.value(get(serverEnvironment.welcome(target))));
63​
64 return {
65 config: get(serverEnvironment.configValueAtom(environmentId)),
66 latestEvent: configProjection?.latestEvent ?? null,
67 welcome,
68 };
69}).pipe(Atom.withLabel("web-primary-server-state"));
70​
71export const primaryServerConfigAtom = Atom.make(
72 (get): ServerConfig | null => get(primaryServerStateAtom).config,
73).pipe(Atom.withLabel("web-primary-server-config"));
74​
75export const primaryServerConfigEventAtom = Atom.make(
76 (get): ServerConfigStreamEvent | null => get(primaryServerStateAtom).latestEvent,
77).pipe(Atom.withLabel("web-primary-server-config-event"));
78​
79export const primaryServerWelcomeAtom = Atom.make(
80 (get): ServerLifecycleWelcomePayload | null => get(primaryServerStateAtom).welcome,
81).pipe(Atom.withLabel("web-primary-server-welcome"));
82​
83export const primaryServerLegacyThreadMigrationAtom = Atom.make(
84 (get): ServerLifecycleLegacyThreadMigrationPayload | null => {
85 const environmentId = get(primaryEnvironmentIdAtom);
86 if (environmentId === null) {
87 return null;
88 }
89 return Option.getOrNull(
90 AsyncResult.value(get(serverEnvironment.legacyThreadMigration({ environmentId, input: {} }))),
91 );
92 },
93).pipe(Atom.withLabel("web-primary-server-legacy-thread-migration"));
94​
95export const primaryServerSettingsAtom = Atom.make(
96 (get): ServerSettings => get(primaryServerConfigAtom)?.settings ?? DEFAULT_SERVER_SETTINGS,
97).pipe(Atom.withLabel("web-primary-server-settings"));
98​
99export const primaryServerProvidersAtom = Atom.make(
100 (get): ReadonlyArray<ServerProvider> =>
101 get(primaryServerConfigAtom)?.providers ?? EMPTY_SERVER_PROVIDERS,
102).pipe(Atom.withLabel("web-primary-server-providers"));
103​
104export const primaryServerKeybindingsAtom = Atom.make(
105 (get): ServerConfig["keybindings"] =>
106 get(primaryServerConfigAtom)?.keybindings ?? DEFAULT_RESOLVED_KEYBINDINGS,
107).pipe(Atom.withLabel("web-primary-server-keybindings"));
108​
109export const primaryServerAvailableEditorsAtom = Atom.make(
110 (get): ReadonlyArray<EditorId> =>
111 get(primaryServerConfigAtom)?.availableEditors ?? EMPTY_AVAILABLE_EDITORS,
112).pipe(Atom.withLabel("web-primary-server-available-editors"));
113​
114export const primaryServerKeybindingsConfigPathAtom = Atom.make(
115 (get): string | null => get(primaryServerConfigAtom)?.keybindingsConfigPath ?? null,
116).pipe(Atom.withLabel("web-primary-server-keybindings-config-path"));
117​
118const EMPTY_ENVIRONMENT_THEMES: ReadonlyArray<EnvironmentTheme> = [];
119​
120/**
121 * Palettes published by the primary environment's machine. Only the primary
122 * environment: a client follows the machine it is anchored to, not every
123 * environment it happens to be connected to.
124 */
125export const primaryServerEnvironmentThemesAtom = Atom.make(
126 (get): ReadonlyArray<EnvironmentTheme> =>
127 get(primaryServerConfigAtom)?.environmentThemes ?? EMPTY_ENVIRONMENT_THEMES,
128).pipe(Atom.withLabel("web-primary-server-environment-themes"));
129​
130export const primaryServerObservabilityAtom = Atom.make(
131 (get): ServerConfig["observability"] | null =>
132 get(primaryServerConfigAtom)?.observability ?? null,
133).pipe(Atom.withLabel("web-primary-server-observability"));