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

shellSnapshotState.ts

140 lines3.5 KB

1import type {
2 EnvironmentId,
3 OrchestrationShellSnapshot,
4 OrchestrationShellStreamEvent,
5} from "@t3tools/contracts";
6import { Atom, type AtomRegistry } from "effect/unstable/reactivity";
7​
8import { applyShellStreamEvent } from "./shellSnapshotReducer.ts";
9​
10export interface ShellSnapshotState {
11 readonly data: OrchestrationShellSnapshot | null;
12 readonly error: string | null;
13 readonly isPending: boolean;
15​
16export interface ShellSnapshotTarget {
17 readonly environmentId: EnvironmentId | null;
19​
20export const EMPTY_SHELL_SNAPSHOT_STATE = Object.freeze<ShellSnapshotState>({
21 data: null,
22 error: null,
23 isPending: false,
24});
25​
26const INITIAL_SHELL_SNAPSHOT_STATE = Object.freeze<ShellSnapshotState>({
27 data: null,
28 error: null,
29 isPending: true,
30});
31​
32const knownShellSnapshotKeys = new Set<string>();
33​
34export const shellSnapshotStateAtom = Atom.family((key: string) => {
35 knownShellSnapshotKeys.add(key);
36 return Atom.make(INITIAL_SHELL_SNAPSHOT_STATE).pipe(
37 Atom.keepAlive,
38 Atom.withLabel(`shell-snapshot:${key}`),
39 );
40});
41​
42export const EMPTY_SHELL_SNAPSHOT_ATOM = Atom.make(EMPTY_SHELL_SNAPSHOT_STATE).pipe(
43 Atom.keepAlive,
44 Atom.withLabel("shell-snapshot:null"),
45);
46​
47export function getShellSnapshotTargetKey(target: ShellSnapshotTarget): string | null {
48 return target.environmentId;
50​
51export interface ShellSnapshotManagerConfig {
52 readonly getRegistry: () => AtomRegistry.AtomRegistry;
54​
55export function createShellSnapshotManager(config: ShellSnapshotManagerConfig) {
56 function getSnapshot(target: ShellSnapshotTarget): ShellSnapshotState {
57 const targetKey = getShellSnapshotTargetKey(target);
58 if (targetKey === null) {
59 return EMPTY_SHELL_SNAPSHOT_STATE;
60 }
61​
62 return config.getRegistry().get(shellSnapshotStateAtom(targetKey));
63 }
64​
65 function setState(targetKey: string, nextState: ShellSnapshotState): void {
66 config.getRegistry().set(shellSnapshotStateAtom(targetKey), nextState);
67 }
68​
69 function markPending(target: ShellSnapshotTarget): void {
70 const targetKey = getShellSnapshotTargetKey(target);
71 if (targetKey === null) {
72 return;
73 }
74​
75 const current = config.getRegistry().get(shellSnapshotStateAtom(targetKey));
76 setState(targetKey, {
77 data: current.data,
78 error: null,
79 isPending: true,
80 });
81 }
82​
83 function syncSnapshot(target: ShellSnapshotTarget, snapshot: OrchestrationShellSnapshot): void {
84 const targetKey = getShellSnapshotTargetKey(target);
85 if (targetKey === null) {
86 return;
87 }
88​
89 setState(targetKey, {
90 data: snapshot,
91 error: null,
92 isPending: false,
93 });
94 }
95​
96 function applyEvent(target: ShellSnapshotTarget, event: OrchestrationShellStreamEvent): void {
97 const targetKey = getShellSnapshotTargetKey(target);
98 if (targetKey === null) {
99 return;
100 }
101​
102 const current = config.getRegistry().get(shellSnapshotStateAtom(targetKey));
103 if (current.data === null) {
104 return;
105 }
106​
107 setState(targetKey, {
108 data: applyShellStreamEvent(current.data, event),
109 error: null,
110 isPending: false,
111 });
112 }
113​
114 function invalidate(target?: ShellSnapshotTarget): void {
115 if (target) {
116 const targetKey = getShellSnapshotTargetKey(target);
117 if (targetKey !== null) {
118 setState(targetKey, EMPTY_SHELL_SNAPSHOT_STATE);
119 }
120 return;
121 }
122​
123 for (const key of knownShellSnapshotKeys) {
124 setState(key, EMPTY_SHELL_SNAPSHOT_STATE);
125 }
126 }
127​
128 function reset(): void {
129 invalidate();
130 }
131​
132 return {
133 markPending,
134 syncSnapshot,
135 applyEvent,
136 getSnapshot,
137 invalidate,
138 reset,
139 };