Open History to see the last commit
1import {
2 OrchestrationV2ShellSnapshot,
3 type ThreadId,
4 ThreadPullRequestLink,
5} from "@t3tools/contracts";
6import * as Clock from "effect/Clock";
7import * as Effect from "effect/Effect";
8import * as Predicate from "effect/Predicate";
9import * as Schema from "effect/Schema";
10
11import { StoredOrchestrationShellSnapshot } from "../platform/orchestrationCache.ts";
12
13export interface DeferredShellSnapshot extends OrchestrationV2ShellSnapshot {
14 readonly loadPullRequests?: Effect.Effect<
15 ReadonlyMap<ThreadId, ReadonlyArray<ThreadPullRequestLink>>
16 >;
17}
18
19// A zero sleep is a microtask yield, which on React Native runs before the next frame.
20// A 1ms sleep takes a host timer, so the rows can render before the deferred decode.
21const yieldToHost = Effect.sleep("1 millis");
22
23const decodeThreadLinks = Schema.decodeUnknownEffect(
24 Schema.UndefinedOr(Schema.toCodecJson(Schema.Array(ThreadPullRequestLink))),
25);
26const LINKS_PER_BATCH = 400;
27const DECODE_SLICE_NS = 4_000_000n;
28
29/**
30 * Removes each parsed thread row's pull request links from `snapshot` in place and returns
31 * them in row order, for `deferPullRequests` once the rows are decoded.
32 */
33function detachPullRequests(snapshot: unknown): ReadonlyArray<unknown> {
34 if (!Predicate.isObject(snapshot)) return [];
35 const rows: unknown = snapshot.threads;
36 if (!Array.isArray(rows)) return [];
37 return rows.map((row: unknown) => {
38 if (!Predicate.isObject(row)) return undefined;
39 const links = row.pullRequests;
40 delete row.pullRequests;
41 return links;
42 });
43}
44
45function deferPullRequests<S extends OrchestrationV2ShellSnapshot>(
46 snapshot: S,
47 rawLinks: ReadonlyArray<unknown>,
48): S & Required<Pick<DeferredShellSnapshot, "loadPullRequests">> {
49 const threadIds = snapshot.threads.map((thread) => thread.id);
50 // Schema decoding runs when called, so each thread's decode is built after a yield.
51 const loadPullRequests = Effect.gen(function* () {
52 const linksByThreadId = new Map<ThreadId, ReadonlyArray<ThreadPullRequestLink>>();
53 let batchSize = LINKS_PER_BATCH;
54 let deadline = 0n;
55 for (const [index, threadId] of threadIds.entries()) {
56 const raw = rawLinks[index];
57 if (raw === undefined) continue;
58 const size = Array.isArray(raw) ? raw.length : 1;
59 if (batchSize + size > LINKS_PER_BATCH || (yield* Clock.monotonicTimeNanos) >= deadline) {
60 yield* yieldToHost;
61 batchSize = 0;
62 deadline = (yield* Clock.monotonicTimeNanos) + DECODE_SLICE_NS;
63 }
64 batchSize += size;
65 const links = yield* decodeThreadLinks(raw).pipe(
66 Effect.catch((cause) =>
67 Effect.logWarning("Discarding unreadable shell pull request links.", {
68 threadId,
69 cause: String(cause),
70 }).pipe(Effect.as(undefined)),
71 ),
72 );
73 if (links !== undefined) linksByThreadId.set(threadId, links);
74 }
75 return linksByThreadId as ReadonlyMap<ThreadId, ReadonlyArray<ThreadPullRequestLink>>;
76 });
77 return { ...snapshot, loadPullRequests };
78}
79
80const decodeShellSnapshotJson = Schema.decodeUnknownEffect(
81 Schema.toCodecJson(OrchestrationV2ShellSnapshot),
82);
83
84export const decodeShellSnapshotDeferringPullRequests = Effect.fnUntraced(function* (
85 json: unknown,
86) {
87 const rawLinks = detachPullRequests(json);
88 const snapshot = yield* decodeShellSnapshotJson(json);
89 return rawLinks.some((links) => links !== undefined)
90 ? deferPullRequests(snapshot, rawLinks)
91 : snapshot;
92});
93
94const decodeJson = Schema.decodeUnknownEffect(Schema.fromJsonString(Schema.Unknown));
95const decodeStoredShell = Schema.decodeUnknownEffect(StoredOrchestrationShellSnapshot);
96
97export const decodeStoredShellSnapshot = Effect.fnUntraced(function* (raw: string) {
98 const parsed = yield* decodeJson(raw);
99 const rawLinks = Predicate.isObject(parsed) ? detachPullRequests(parsed.snapshot) : [];
100 const stored = yield* decodeStoredShell(parsed);
101 if (!rawLinks.some((links) => links !== undefined)) return stored;
102 return { ...stored, snapshot: deferPullRequests(stored.snapshot, rawLinks) };
103});