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