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

threadShell.ts

213 lines7.7 KB

1import type {
2 EnvironmentId,
3 OrchestrationShellSnapshot,
4 OrchestrationThreadShell,
5 ProjectId,
6 ScopedProjectRef,
7 ScopedThreadRef,
8 ThreadId,
9} from "@t3tools/contracts";
10import { Atom } from "effect/unstable/reactivity";
11​
12import type { EnvironmentThreadShell } from "./models.ts";
13import { scopeThreadShell } from "./models.ts";
14import { type EnvironmentCatalogState, enabledEnvironmentIds } from "./connections.ts";
15import {
16 arrayElementsEqual,
17 parseProjectRefCollectionKey,
18 parseThreadKey,
19 projectRefCollectionKey,
20 threadKey,
21 threadRefsEqual,
22} from "./entities.ts";
23​
24const EMPTY_THREADS: ReadonlyArray<OrchestrationThreadShell> = Object.freeze([]);
25const EMPTY_SCOPED_THREAD_REFS: ReadonlyArray<ScopedThreadRef> = Object.freeze([]);
26const EMPTY_THREAD_INDEX: ReadonlyMap<ThreadId, OrchestrationThreadShell> = new Map();
27const EMPTY_THREAD_REFS_BY_PROJECT: ReadonlyMap<
28 ProjectId,
29 ReadonlyArray<ScopedThreadRef>
30> = new Map();
31​
32export function createEnvironmentThreadShellAtoms(input: {
33 readonly catalogValueAtom: Atom.Atom<EnvironmentCatalogState>;
34 readonly snapshotAtom: (
35 environmentId: EnvironmentId,
36 ) => Atom.Atom<OrchestrationShellSnapshot | null>;
37}) {
38 // Point reads and aggregate lists share values without keeping an atom alive
39 // for every listed thread. Replaced source objects can be collected.
40 const scopedThreads = new WeakMap<
41 OrchestrationThreadShell,
42 Map<EnvironmentId, EnvironmentThreadShell>
43 >();
44 const scopedThread = (environmentId: EnvironmentId, thread: OrchestrationThreadShell) => {
45 let byEnvironment = scopedThreads.get(thread);
46 if (byEnvironment === undefined) {
47 byEnvironment = new Map();
48 scopedThreads.set(thread, byEnvironment);
49 }
50 let value = byEnvironment.get(environmentId);
51 if (value === undefined) {
52 value = scopeThreadShell(environmentId, thread);
53 byEnvironment.set(environmentId, value);
54 }
55 return value;
56 };
57​
58 const environmentThreadsAtom = Atom.family((environmentId: EnvironmentId) =>
59 Atom.make(
60 (get): ReadonlyArray<OrchestrationThreadShell> =>
61 get(input.snapshotAtom(environmentId))?.threads ?? EMPTY_THREADS,
62 ).pipe(Atom.withLabel(`environment-threads:${environmentId}`)),
63 );
64​
65 const environmentThreadIndexAtom = Atom.family((environmentId: EnvironmentId) =>
66 Atom.make((get): ReadonlyMap<ThreadId, OrchestrationThreadShell> => {
67 const threads = get(environmentThreadsAtom(environmentId));
68 if (threads.length === 0) {
69 return EMPTY_THREAD_INDEX;
70 }
71 return new Map(threads.map((thread) => [thread.id, thread] as const));
72 }).pipe(Atom.withLabel(`environment-thread-index:${environmentId}`)),
73 );
74​
75 const environmentThreadRefsAtom = Atom.family((environmentId: EnvironmentId) => {
76 let previous: ReadonlyArray<ScopedThreadRef> = [];
77 return Atom.make((get) => {
78 const next = get(environmentThreadsAtom(environmentId)).map((thread) => ({
79 environmentId,
80 threadId: thread.id,
81 }));
82 if (threadRefsEqual(previous, next)) {
83 return previous;
84 }
85 previous = next;
86 return next;
87 }).pipe(Atom.withLabel(`environment-thread-refs:${environmentId}`));
88 });
89​
90 const environmentThreadRefsByProjectAtom = Atom.family((environmentId: EnvironmentId) => {
91 let previous: ReadonlyMap<
92 ProjectId,
93 ReadonlyArray<ScopedThreadRef>
94 > = EMPTY_THREAD_REFS_BY_PROJECT;
95 return Atom.make((get) => {
96 const grouped = new Map<ProjectId, ScopedThreadRef[]>();
97 for (const thread of get(environmentThreadsAtom(environmentId))) {
98 const refs = grouped.get(thread.projectId);
99 const ref = { environmentId, threadId: thread.id };
100 if (refs === undefined) {
101 grouped.set(thread.projectId, [ref]);
102 } else {
103 refs.push(ref);
104 }
105 }
106 if (grouped.size === 0) {
107 previous = EMPTY_THREAD_REFS_BY_PROJECT;
108 return previous;
109 }
110 const next = new Map<ProjectId, ReadonlyArray<ScopedThreadRef>>();
111 for (const [projectId, refs] of grouped) {
112 const previousRefs = previous.get(projectId);
113 next.set(
114 projectId,
115 previousRefs !== undefined && threadRefsEqual(previousRefs, refs) ? previousRefs : refs,
116 );
117 }
118 const previousProjectIds = [...previous.keys()];
119 if (
120 next.size === previous.size &&
121 [...next].every(
122 ([projectId, refs], index) =>
123 previousProjectIds[index] === projectId && previous.get(projectId) === refs,
124 )
125 ) {
126 return previous;
127 }
128 previous = next;
129 return previous;
130 }).pipe(Atom.withLabel(`environment-thread-refs-by-project:${environmentId}`));
131 });
132​
133 const threadShellAtomFamily = Atom.family((key: string) => {
134 const ref = parseThreadKey(key);
135 return Atom.make((get) => {
136 const source = get(environmentThreadIndexAtom(ref.environmentId)).get(ref.threadId) ?? null;
137 return source === null ? null : scopedThread(ref.environmentId, source);
138 }).pipe(Atom.withLabel(`environment-thread-shell:${key}`));
139 });
140​
141 const threadShellsForProjectRefsAtomFamily = Atom.family((key: string) => {
142 const projectRefs = parseProjectRefCollectionKey(key);
143 let previous: ReadonlyArray<EnvironmentThreadShell> = [];
144 return Atom.make((get) => {
145 const next: EnvironmentThreadShell[] = [];
146 const seen = new Set<string>();
147 for (const projectRef of projectRefs) {
148 const refs =
149 get(environmentThreadRefsByProjectAtom(projectRef.environmentId)).get(
150 projectRef.projectId,
151 ) ?? EMPTY_SCOPED_THREAD_REFS;
152 if (refs.length === 0) continue;
153 const threads = get(environmentThreadIndexAtom(projectRef.environmentId));
154 for (const ref of refs) {
155 const key = threadKey(ref);
156 if (seen.has(key)) {
157 continue;
158 }
159 seen.add(key);
160 const thread = threads.get(ref.threadId);
161 if (thread !== undefined) {
162 next.push(scopedThread(ref.environmentId, thread));
163 }
164 }
165 }
166 if (arrayElementsEqual(previous, next)) {
167 return previous;
168 }
169 previous = next;
170 return previous;
171 }).pipe(Atom.withLabel(`environment-thread-shells-for-projects:${key}`));
172 });
173​
174 let previousThreadRefs: ReadonlyArray<ScopedThreadRef> = [];
175 const threadRefsAtom = Atom.make((get) => {
176 const refs: ScopedThreadRef[] = [];
177 for (const environmentId of enabledEnvironmentIds(get(input.catalogValueAtom))) {
178 refs.push(...get(environmentThreadRefsAtom(environmentId)));
179 }
180 if (threadRefsEqual(previousThreadRefs, refs)) {
181 return previousThreadRefs;
182 }
183 previousThreadRefs = refs;
184 return refs;
185 }).pipe(Atom.withLabel("environment-thread-refs"));
186​
187 let previousThreadShells: ReadonlyArray<EnvironmentThreadShell> = [];
188 const threadShellsAtom = Atom.make((get) => {
189 const next: EnvironmentThreadShell[] = [];
190 for (const environmentId of enabledEnvironmentIds(get(input.catalogValueAtom))) {
191 for (const thread of get(environmentThreadsAtom(environmentId))) {
192 next.push(scopedThread(environmentId, thread));
193 }
194 }
195 if (arrayElementsEqual(previousThreadShells, next)) {
196 return previousThreadShells;
197 }
198 previousThreadShells = next;
199 return previousThreadShells;
200 }).pipe(Atom.withLabel("environment-thread-shell-list"));
201​
202 return {
203 environmentThreadsAtom,
204 environmentThreadIndexAtom,
205 environmentThreadRefsAtom,
206 environmentThreadRefsByProjectAtom,
207 threadRefsAtom,
208 threadShellsAtom,
209 threadShellsForProjectRefsAtom: (refs: ReadonlyArray<ScopedProjectRef>) =>
210 threadShellsForProjectRefsAtomFamily(projectRefCollectionKey(refs)),
211 threadShellAtom: (ref: ScopedThreadRef) => threadShellAtomFamily(threadKey(ref)),
212 };