Open History to see the last commit
1import {
2 DEFAULT_SERVER_SETTINGS,
3 type EditorId,
4 type ServerConfig,
5 type ServerConfigStreamEvent,
6 type ServerLifecycleWelcomePayload,
7 type ServerProvider,
8 type ServerSettings,
9} from "@v12/contracts";
10import { createServerEnvironmentAtoms } from "@v12/client-runtime/state/server";
11import { createEnvironmentServerConfigsAtom } from "@v12/client-runtime/state/shell";
12import { DEFAULT_RESOLVED_KEYBINDINGS } from "@v12/shared/keybindings";
13import * as Option from "effect/Option";
14import { AsyncResult, Atom } from "effect/unstable/reactivity";
15
16import { environmentCatalog } from "../connection/catalog";
17import { connectionAtomRuntime } from "../connection/runtime";
18import { primaryEnvironmentIdAtom } from "./primaryEnvironment";
19import { environmentSession } from "./session";
20
21export const serverEnvironment = createServerEnvironmentAtoms(connectionAtomRuntime, {
22 initialConfigValueAtom: environmentSession.initialConfigValueAtom,
23});
24export const environmentServerConfigsAtom = createEnvironmentServerConfigsAtom({
25 catalogValueAtom: environmentCatalog.catalogValueAtom,
26 serverConfigValueAtom: serverEnvironment.configValueAtom,
27});
28
29interface PrimaryServerState {
30 readonly config: ServerConfig | null;
31 readonly latestEvent: ServerConfigStreamEvent | null;
32 readonly welcome: ServerLifecycleWelcomePayload | null;
33}
34
35const EMPTY_AVAILABLE_EDITORS: ReadonlyArray<EditorId> = [];
36const EMPTY_SERVER_PROVIDERS: ReadonlyArray<ServerProvider> = [];
37const EMPTY_PRIMARY_SERVER_STATE: PrimaryServerState = {
38 config: null,
39 latestEvent: null,
40 welcome: null,
41};
42
43export const primaryServerStateAtom = Atom.make((get): PrimaryServerState => {
44 const environmentId = get(primaryEnvironmentIdAtom);
45 if (environmentId === null) {
46 return EMPTY_PRIMARY_SERVER_STATE;
47 }
48
49 const target = { environmentId, input: {} };
50 const configProjection = Option.getOrNull(
51 AsyncResult.value(get(serverEnvironment.configProjection(target))),
52 );
53 const welcome = Option.getOrNull(AsyncResult.value(get(serverEnvironment.welcome(target))));
54
55 return {
56 config: get(serverEnvironment.configValueAtom(environmentId)),
57 latestEvent: configProjection?.latestEvent ?? null,
58 welcome,
59 };
60}).pipe(Atom.withLabel("web-primary-server-state"));
61
62export const primaryServerConfigAtom = Atom.make(
63 (get): ServerConfig | null => get(primaryServerStateAtom).config,
64).pipe(Atom.withLabel("web-primary-server-config"));
65
66export const primaryServerConfigEventAtom = Atom.make(
67 (get): ServerConfigStreamEvent | null => get(primaryServerStateAtom).latestEvent,
68).pipe(Atom.withLabel("web-primary-server-config-event"));
69
70export const primaryServerWelcomeAtom = Atom.make(
71 (get): ServerLifecycleWelcomePayload | null => get(primaryServerStateAtom).welcome,
72).pipe(Atom.withLabel("web-primary-server-welcome"));
73
74export const primaryServerSettingsAtom = Atom.make(
75 (get): ServerSettings => get(primaryServerConfigAtom)?.settings ?? DEFAULT_SERVER_SETTINGS,
76).pipe(Atom.withLabel("web-primary-server-settings"));
77
78export const primaryServerProvidersAtom = Atom.make(
79 (get): ReadonlyArray<ServerProvider> =>
80 get(primaryServerConfigAtom)?.providers ?? EMPTY_SERVER_PROVIDERS,
81).pipe(Atom.withLabel("web-primary-server-providers"));
82
83export const primaryServerKeybindingsAtom = Atom.make(
84 (get): ServerConfig["keybindings"] =>
85 get(primaryServerConfigAtom)?.keybindings ?? DEFAULT_RESOLVED_KEYBINDINGS,
86).pipe(Atom.withLabel("web-primary-server-keybindings"));
87
88export const primaryServerAvailableEditorsAtom = Atom.make(
89 (get): ReadonlyArray<EditorId> =>
90 get(primaryServerConfigAtom)?.availableEditors ?? EMPTY_AVAILABLE_EDITORS,
91).pipe(Atom.withLabel("web-primary-server-available-editors"));
92
93export const primaryServerKeybindingsConfigPathAtom = Atom.make(
94 (get): string | null => get(primaryServerConfigAtom)?.keybindingsConfigPath ?? null,
95).pipe(Atom.withLabel("web-primary-server-keybindings-config-path"));
96
97export const primaryServerObservabilityAtom = Atom.make(
98 (get): ServerConfig["observability"] | null =>
99 get(primaryServerConfigAtom)?.observability ?? null,
100).pipe(Atom.withLabel("web-primary-server-observability"));