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

serverConfigProjection.ts

97 lines3.3 KB

1import type { ServerConfig, ServerConfigStreamEvent } from "@t3tools/contracts";
2import * as Option from "effect/Option";
3​
4export interface ServerConfigProjection {
5 readonly config: ServerConfig;
6 readonly latestEvent: ServerConfigStreamEvent;
7 readonly source: "cache" | "live";
8}
9​
10/**
11 * Published themes and usage-limit sources are current machine state, so a
12 * cache could restore a set the machine no longer reports.
13 */
14export function withoutEnvironmentThemes(config: ServerConfig): ServerConfig {
15 if (config.environmentThemes === undefined && config.usageLimitSources === undefined) {
16 return config;
17 }
18 const { environmentThemes: _themes, usageLimitSources: _sources, ...rest } = config;
19 return rest;
21​
22export function applyServerConfigProjection(
23 current: Option.Option<ServerConfigProjection>,
24 event: ServerConfigStreamEvent,
25): Option.Option<ServerConfigProjection> {
26 switch (event.type) {
27 case "snapshot": {
28 // Wire snapshots never contain published themes. Keep the previous set
29 // until a capable server sends its authoritative theme event. A legacy
30 // server cannot send a later removal, so a downgrade must clear the set.
31 const capabilities = event.config.environment.capabilities;
32 const carriedThemes =
33 capabilities.environmentThemes === true && Option.isSome(current)
34 ? current.value.config.environmentThemes
35 : undefined;
36 const carriedSources =
37 capabilities.usageLimitSources === true && Option.isSome(current)
38 ? current.value.config.usageLimitSources
39 : undefined;
40 return Option.some({
41 config: {
42 ...event.config,
43 ...(carriedThemes === undefined ? {} : { environmentThemes: carriedThemes }),
44 ...(carriedSources === undefined ? {} : { usageLimitSources: carriedSources }),
45 },
46 latestEvent: event,
47 source: "live" as const,
48 });
49 }
50 case "keybindingsUpdated":
51 return Option.map(current, (projection) => ({
52 config: {
53 ...projection.config,
54 keybindings: event.payload.keybindings,
55 issues: event.payload.issues,
56 },
57 latestEvent: event,
58 source: "live",
59 }));
60 case "providerStatuses":
61 return Option.map(current, (projection) => ({
62 config: {
63 ...projection.config,
64 providers: event.payload.providers,
65 },
66 latestEvent: event,
67 source: "live",
68 }));
69 case "settingsUpdated":
70 return Option.map(current, (projection) => ({
71 config: {
72 ...projection.config,
73 settings: event.payload.settings,
74 },
75 latestEvent: event,
76 source: "live",
77 }));
78 case "environmentThemesUpdated":
79 return Option.map(current, (projection) => ({
80 config: {
81 ...projection.config,
82 environmentThemes: event.payload.themes.length > 0 ? event.payload.themes : undefined,
83 },
84 latestEvent: event,
85 source: "live",
86 }));
87 case "usageLimitSourcesUpdated":
88 return Option.map(current, (projection) => ({
89 config: {
90 ...projection.config,
91 usageLimitSources: event.payload.sources.length > 0 ? event.payload.sources : undefined,
92 },
93 latestEvent: event,
94 source: "live",
95 }));
96 }