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

shellCacheEncoding.ts

81 lines3.0 KB

1import { OrchestrationV2ThreadShellJson } from "@t3tools/contracts";
2import * as Effect from "effect/Effect";
3import * as Schema from "effect/Schema";
4​
5import { yieldToHostTimer } from "./hostYield.ts";
6import { StoredOrchestrationShellSnapshot } from "./orchestrationCache.ts";
7​
8const ROWS_PER_CHUNK = 32;
9​
10const encodeThreadChunk = Schema.encodeEffect(Schema.Array(OrchestrationV2ThreadShellJson));
11const EncodedRows = Schema.Array(Schema.Unknown);
12const encodeEnvelope = Schema.encodeEffect(
13 Schema.fromJsonString(
14 StoredOrchestrationShellSnapshot.mapFields((fields) => ({
15 ...fields,
16 snapshot: fields.snapshot.mapFields((snapshotFields) => ({
17 ...snapshotFields,
18 threads: EncodedRows,
19 archivedThreads: EncodedRows,
20 })),
21 })),
22 ),
23);
24​
25/**
26 * Shell rows are immutable and the shell reducer keeps unchanged rows by
27 * reference, so each encoder remembers the canonical encoding of rows it has
28 * already encoded successfully and only runs the row codec for new rows.
29 *
30 * `yieldToHost` defaults to a zero-delay timer.
31 */
32export function makeStoredShellSnapshotEncoder(
33 options: {
34 readonly rowsPerChunk?: number;
35 readonly yieldToHost?: Effect.Effect<void>;
36 } = {},
37) {
38 const yieldToHost = options.yieldToHost ?? yieldToHostTimer;
39 const rowsPerChunk = options.rowsPerChunk ?? ROWS_PER_CHUNK;
40 const encodedRows = new WeakMap<OrchestrationV2ThreadShellJson, unknown>();
41​
42 return Effect.fnUntraced(function* (stored: typeof StoredOrchestrationShellSnapshot.Type) {
43 let hasWorked = false;
44 const yieldBetweenChunks = Effect.suspend(() => {
45 if (hasWorked) return yieldToHost;
46 hasWorked = true;
47 return Effect.void;
48 });
49 const encodeMisses =
50 /* Cache encoding runs for shell update batches; avoid tracing each batch. */
51 Effect.fnUntraced(function* (misses: ReadonlyArray<OrchestrationV2ThreadShellJson>) {
52 yield* yieldBetweenChunks;
53 const chunk = yield* encodeThreadChunk(misses);
54 chunk.forEach((row, index) => encodedRows.set(misses[index]!, row));
55 });
56 const encodeRows =
57 /* Cache encoding runs for shell update batches; avoid tracing each batch. */
58 Effect.fnUntraced(function* (rows: ReadonlyArray<OrchestrationV2ThreadShellJson>) {
59 let misses: Array<OrchestrationV2ThreadShellJson> = [];
60 for (const row of rows) {
61 if (encodedRows.has(row)) continue;
62 misses.push(row);
63 if (misses.length >= rowsPerChunk) {
64 yield* encodeMisses(misses);
65 misses = [];
66 }
67 }
68 if (misses.length > 0) yield* encodeMisses(misses);
69 return rows.map((row) => encodedRows.get(row));
70 });
71​
72 const { snapshot } = stored;
73 const threads = yield* encodeRows(snapshot.threads);
74 const archivedThreads = yield* encodeRows(snapshot.archivedThreads);
75 if (hasWorked) yield* yieldToHost;
76 return yield* encodeEnvelope({
77 ...stored,
78 snapshot: { ...snapshot, threads, archivedThreads },
79 });
80 });