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

server.ts

144 lines6.2 KB

1import {
2 DEFAULT_SERVER_SETTINGS,
3 type EditorId,
4 type EnvironmentId,
5 type EnvironmentTheme,
6 type ServerConfig,
7 type ServerConfigStreamEvent,
8 type ServerLifecycleLegacyThreadMigrationPayload,
9 type ServerLifecycleWelcomePayload,
10 type ServerProvider,
11 type ServerSettings,
12} from "@t3tools/contracts";
13import { createServerEnvironmentAtoms } from "@t3tools/client-runtime/state/server";
14import { createOutdatedServerUpdateCommand } from "@t3tools/client-runtime/state/outdatedServerUpdate";
15import { createEnvironmentServerConfigsAtom } from "@t3tools/client-runtime/state/shell";
16import { mergeWithDefaultKeybindings } from "@t3tools/shared/keybindings";
17import * as Option from "effect/Option";
18import { AsyncResult, Atom } from "effect/reactivity";
19​
20import { environmentCatalog } from "../connection/catalog";
21import { connectionAtomRuntime } from "../connection/runtime";
22import { environmentAtomList } from "./environmentAtomList";
23import { primaryEnvironmentIdAtom } from "./primaryEnvironment";
24import { environmentSession } from "./session";
25import { reuseProvidersIgnoringCheckedAt } from "./serverProviders.logic";
26​
27// Only the primary environment's themes are rendered, but which environment is primary changes
28// at runtime and the subscription payload is fixed when it is established -- gating on "primary
29// right now" would leave a newly promoted environment with no themes until it reconnected.
30export const serverEnvironment = createServerEnvironmentAtoms(connectionAtomRuntime, {
31 initialConfigValueAtom: environmentSession.initialConfigValueAtom,
32 environmentThemes: true,
33 usageLimitSources: true,
34 usageLimitsCommand: true,
35});
36export const environmentUpdateStatesAtom = environmentAtomList(
37 "web-environment-update-states",
38 serverEnvironment.updateStateAtom,
39);
40export const updateOutdatedServer = createOutdatedServerUpdateCommand(connectionAtomRuntime);
41export const environmentServerConfigsAtom = createEnvironmentServerConfigsAtom({
42 catalogValueAtom: environmentCatalog.catalogValueAtom,
43 serverConfigValueAtom: serverEnvironment.configValueAtom,
44});
45​
46interface PrimaryServerState {
47 readonly config: ServerConfig | null;
48 readonly latestEvent: ServerConfigStreamEvent | null;
49 readonly welcome: ServerLifecycleWelcomePayload | null;
51​
52const EMPTY_AVAILABLE_EDITORS: ReadonlyArray<EditorId> = [];
53export const EMPTY_SERVER_PROVIDERS: ReadonlyArray<ServerProvider> = [];
54const EMPTY_PRIMARY_SERVER_STATE: PrimaryServerState = {
55 config: null,
56 latestEvent: null,
57 welcome: null,
58};
59​
60const primaryServerStateAtom = Atom.make((get): PrimaryServerState => {
61 const environmentId = get(primaryEnvironmentIdAtom);
62 if (environmentId === null) {
63 return EMPTY_PRIMARY_SERVER_STATE;
64 }
65​
66 const target = { environmentId, input: {} };
67 const configProjection = Option.getOrNull(
68 AsyncResult.value(get(serverEnvironment.configProjection(target))),
69 );
70 const welcome = Option.getOrNull(AsyncResult.value(get(serverEnvironment.welcome(target))));
71​
72 return {
73 config: get(serverEnvironment.configValueAtom(environmentId)),
74 latestEvent: configProjection?.latestEvent ?? null,
75 welcome,
76 };
77}).pipe(Atom.withLabel("web-primary-server-state"));
78​
79export const primaryServerConfigAtom = Atom.make(
80 (get): ServerConfig | null => get(primaryServerStateAtom).config,
81).pipe(Atom.withLabel("web-primary-server-config"));
82​
83export const primaryServerConfigEventAtom = Atom.make(
84 (get): ServerConfigStreamEvent | null => get(primaryServerStateAtom).latestEvent,
85).pipe(Atom.withLabel("web-primary-server-config-event"));
86​
87/** The primary server's first config this session. */
88export const primaryServerSessionConfigAtom = Atom.make((get): ServerConfig | null => {
89 const environmentId = get(primaryEnvironmentIdAtom);
90 return environmentId === null
91 ? null
92 : get(environmentSession.initialConfigValueAtom(environmentId));
93}).pipe(Atom.withLabel("web-primary-server-session-config"));
94​
95export const primaryServerWelcomeAtom = Atom.make(
96 (get): ServerLifecycleWelcomePayload | null => get(primaryServerStateAtom).welcome,
97).pipe(Atom.withLabel("web-primary-server-welcome"));
98​
99export const primaryServerLegacyThreadMigrationAtom = Atom.make(
100 (get): ServerLifecycleLegacyThreadMigrationPayload | null => {
101 const environmentId = get(primaryEnvironmentIdAtom);
102 if (environmentId === null) {
103 return null;
104 }
105 return Option.getOrNull(
106 AsyncResult.value(get(serverEnvironment.legacyThreadMigration({ environmentId, input: {} }))),
107 );
108 },
109).pipe(Atom.withLabel("web-primary-server-legacy-thread-migration"));
110​
111export const primaryServerSettingsAtom = Atom.make(
112 (get): ServerSettings => get(primaryServerConfigAtom)?.settings ?? DEFAULT_SERVER_SETTINGS,
113).pipe(Atom.withLabel("web-primary-server-settings"));
114​
115export const primaryServerProvidersAtom = Atom.make(
116 (get): ReadonlyArray<ServerProvider> =>
117 get(primaryServerConfigAtom)?.providers ?? EMPTY_SERVER_PROVIDERS,
118).pipe(Atom.withLabel("web-primary-server-providers"));
119​
120export const primaryServerKeybindingsAtom = Atom.make((get): ServerConfig["keybindings"] =>
121 mergeWithDefaultKeybindings(get(primaryServerConfigAtom)?.keybindings ?? []),
122).pipe(Atom.withLabel("web-primary-server-keybindings"));
123​
124export const primaryServerAvailableEditorsAtom = Atom.make(
125 (get): ReadonlyArray<EditorId> =>
126 get(primaryServerConfigAtom)?.availableEditors ?? EMPTY_AVAILABLE_EDITORS,
127).pipe(Atom.withLabel("web-primary-server-available-editors"));
128​
129const EMPTY_ENVIRONMENT_THEMES: ReadonlyArray<EnvironmentTheme> = [];
130​
131export const primaryServerEnvironmentThemesAtom = Atom.make(
132 (get): ReadonlyArray<EnvironmentTheme> =>
133 get(primaryServerConfigAtom)?.environmentThemes ?? EMPTY_ENVIRONMENT_THEMES,
134).pipe(Atom.withLabel("web-primary-server-environment-themes"));
135​
136/** Providers for views that do not show `checkedAt`; see {@link reuseProvidersIgnoringCheckedAt}. */
137export const providersIgnoringCheckedAtAtom = Atom.family((environmentId: EnvironmentId) =>
138 Atom.make((get): ReadonlyArray<ServerProvider> =>
139 reuseProvidersIgnoringCheckedAt(
140 Option.getOrNull(get.self<ReadonlyArray<ServerProvider>>()),
141 get(serverEnvironment.providersValueAtom(environmentId)) ?? EMPTY_SERVER_PROVIDERS,
142 ),
143 ).pipe(Atom.withLabel(`web-providers-ignoring-checked-at:${environmentId}`)),
144);