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

server.ts

120 lines5.1 KB

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