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

server.ts

125 lines5.2 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 { mergeWithDefaultKeybindings } 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 usageLimitSources: true,
33 usageLimitsCommand: true,
34});
35export const environmentServerConfigsAtom = createEnvironmentServerConfigsAtom({
36 catalogValueAtom: environmentCatalog.catalogValueAtom,
37 serverConfigValueAtom: serverEnvironment.configValueAtom,
38});
39​
40interface PrimaryServerState {
41 readonly config: ServerConfig | null;
42 readonly latestEvent: ServerConfigStreamEvent | null;
43 readonly welcome: ServerLifecycleWelcomePayload | null;
45​
46const EMPTY_AVAILABLE_EDITORS: ReadonlyArray<EditorId> = [];
47export const EMPTY_SERVER_PROVIDERS: ReadonlyArray<ServerProvider> = [];
48const EMPTY_PRIMARY_SERVER_STATE: PrimaryServerState = {
49 config: null,
50 latestEvent: null,
51 welcome: null,
52};
53​
54const primaryServerStateAtom = Atom.make((get): PrimaryServerState => {
55 const environmentId = get(primaryEnvironmentIdAtom);
56 if (environmentId === null) {
57 return EMPTY_PRIMARY_SERVER_STATE;
58 }
59​
60 const target = { environmentId, input: {} };
61 const configProjection = Option.getOrNull(
62 AsyncResult.value(get(serverEnvironment.configProjection(target))),
63 );
64 const welcome = Option.getOrNull(AsyncResult.value(get(serverEnvironment.welcome(target))));
65​
66 return {
67 config: get(serverEnvironment.configValueAtom(environmentId)),
68 latestEvent: configProjection?.latestEvent ?? null,
69 welcome,
70 };
71}).pipe(Atom.withLabel("web-primary-server-state"));
72​
73export const primaryServerConfigAtom = Atom.make(
74 (get): ServerConfig | null => get(primaryServerStateAtom).config,
75).pipe(Atom.withLabel("web-primary-server-config"));
76​
77export const primaryServerConfigEventAtom = Atom.make(
78 (get): ServerConfigStreamEvent | null => get(primaryServerStateAtom).latestEvent,
79).pipe(Atom.withLabel("web-primary-server-config-event"));
80​
81export const primaryServerWelcomeAtom = Atom.make(
82 (get): ServerLifecycleWelcomePayload | null => get(primaryServerStateAtom).welcome,
83).pipe(Atom.withLabel("web-primary-server-welcome"));
84​
85export const primaryServerLegacyThreadMigrationAtom = Atom.make(
86 (get): ServerLifecycleLegacyThreadMigrationPayload | null => {
87 const environmentId = get(primaryEnvironmentIdAtom);
88 if (environmentId === null) {
89 return null;
90 }
91 return Option.getOrNull(
92 AsyncResult.value(get(serverEnvironment.legacyThreadMigration({ environmentId, input: {} }))),
93 );
94 },
95).pipe(Atom.withLabel("web-primary-server-legacy-thread-migration"));
96​
97export const primaryServerSettingsAtom = Atom.make(
98 (get): ServerSettings => get(primaryServerConfigAtom)?.settings ?? DEFAULT_SERVER_SETTINGS,
99).pipe(Atom.withLabel("web-primary-server-settings"));
100​
101export const primaryServerProvidersAtom = Atom.make(
102 (get): ReadonlyArray<ServerProvider> =>
103 get(primaryServerConfigAtom)?.providers ?? EMPTY_SERVER_PROVIDERS,
104).pipe(Atom.withLabel("web-primary-server-providers"));
105​
106export const primaryServerKeybindingsAtom = Atom.make((get): ServerConfig["keybindings"] =>
107 mergeWithDefaultKeybindings(get(primaryServerConfigAtom)?.keybindings ?? []),
108).pipe(Atom.withLabel("web-primary-server-keybindings"));
109​
110export const primaryServerAvailableEditorsAtom = Atom.make(
111 (get): ReadonlyArray<EditorId> =>
112 get(primaryServerConfigAtom)?.availableEditors ?? EMPTY_AVAILABLE_EDITORS,
113).pipe(Atom.withLabel("web-primary-server-available-editors"));
114​
115const EMPTY_ENVIRONMENT_THEMES: ReadonlyArray<EnvironmentTheme> = [];
116​
117/**
118 * Palettes published by the primary environment's machine. Only the primary
119 * environment: a client follows the machine it is anchored to, not every
120 * environment it happens to be connected to.
121 */
122export const primaryServerEnvironmentThemesAtom = Atom.make(
123 (get): ReadonlyArray<EnvironmentTheme> =>
124 get(primaryServerConfigAtom)?.environmentThemes ?? EMPTY_ENVIRONMENT_THEMES,
125).pipe(Atom.withLabel("web-primary-server-environment-themes"));