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

server.ts

121 lines5.2 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 usageLimitSources: 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 primaryServerSettingsAtom = Atom.make(
84 (get): ServerSettings => get(primaryServerConfigAtom)?.settings ?? DEFAULT_SERVER_SETTINGS,
85).pipe(Atom.withLabel("web-primary-server-settings"));
86​
87export const primaryServerProvidersAtom = Atom.make(
88 (get): ReadonlyArray<ServerProvider> =>
89 get(primaryServerConfigAtom)?.providers ?? EMPTY_SERVER_PROVIDERS,
90).pipe(Atom.withLabel("web-primary-server-providers"));
91​
92export const primaryServerKeybindingsAtom = Atom.make(
93 (get): ServerConfig["keybindings"] =>
94 get(primaryServerConfigAtom)?.keybindings ?? DEFAULT_RESOLVED_KEYBINDINGS,
95).pipe(Atom.withLabel("web-primary-server-keybindings"));
96​
97export const primaryServerAvailableEditorsAtom = Atom.make(
98 (get): ReadonlyArray<EditorId> =>
99 get(primaryServerConfigAtom)?.availableEditors ?? EMPTY_AVAILABLE_EDITORS,
100).pipe(Atom.withLabel("web-primary-server-available-editors"));
101​
102export const primaryServerKeybindingsConfigPathAtom = Atom.make(
103 (get): string | null => get(primaryServerConfigAtom)?.keybindingsConfigPath ?? null,
104).pipe(Atom.withLabel("web-primary-server-keybindings-config-path"));
105​
106const EMPTY_ENVIRONMENT_THEMES: ReadonlyArray<EnvironmentTheme> = [];
107​
108/**
109 * Palettes published by the primary environment's machine. Only the primary
110 * environment: a client follows the machine it is anchored to, not every
111 * environment it happens to be connected to.
112 */
113export const primaryServerEnvironmentThemesAtom = Atom.make(
114 (get): ReadonlyArray<EnvironmentTheme> =>
115 get(primaryServerConfigAtom)?.environmentThemes ?? EMPTY_ENVIRONMENT_THEMES,
116).pipe(Atom.withLabel("web-primary-server-environment-themes"));
117​
118export const primaryServerObservabilityAtom = Atom.make(
119 (get): ServerConfig["observability"] | null =>
120 get(primaryServerConfigAtom)?.observability ?? null,
121).pipe(Atom.withLabel("web-primary-server-observability"));