Open History to see the last commit
1import { useEffect } from "react";
2
3import { getCachedNativeReviewDiffData } from "./nativeReviewDiffAdapter";
4import type { ReviewSectionItem } from "./reviewModel";
5import {
6 getCachedReviewParsedDiff,
7 getReviewParsedDiffSourceCharacterCount,
8 MAX_CACHED_REVIEW_DIFFS,
9 MAX_CACHED_REVIEW_SOURCE_CHARACTERS,
10} from "./reviewState";
11
12interface IdleDeadlineLike {
13 readonly didTimeout: boolean;
14 timeRemaining(): number;
15}
16
17type IdleCallback = (deadline: IdleDeadlineLike) => void;
18
19function scheduleIdle(callback: IdleCallback): number {
20 if (typeof globalThis.requestIdleCallback === "function") {
21 return globalThis.requestIdleCallback(callback, { timeout: 2_000 });
22 }
23
24 return setTimeout(
25 () => callback({ didTimeout: true, timeRemaining: () => 0 }),
26 100,
27 ) as unknown as number;
28}
29
30function cancelIdle(handle: number): void {
31 if (typeof globalThis.cancelIdleCallback === "function") {
32 globalThis.cancelIdleCallback(handle);
33 return;
34 }
35 clearTimeout(handle);
36}
37
38function prewarmReviewDiffSection(input: {
39 readonly threadKey: string;
40 readonly section: ReviewSectionItem;
41}): void {
42 const { section, threadKey } = input;
43 if (section.diff === null || section.diff.length > MAX_CACHED_REVIEW_SOURCE_CHARACTERS) {
44 return;
45 }
46
47 const parsedDiff = getCachedReviewParsedDiff({
48 threadKey,
49 sectionId: section.id,
50 diff: section.diff,
51 });
52 getCachedNativeReviewDiffData({ parsedDiff, comments: [] });
53}
54
55function getReviewDiffPrewarmSections(input: {
56 readonly threadKey: string;
57 readonly sections: ReadonlyArray<ReviewSectionItem>;
58 readonly selectedSectionId: string | null;
59}): ReadonlyArray<ReviewSectionItem> {
60 const { threadKey, sections, selectedSectionId } = input;
61 const selectedIndex = sections.findIndex((section) => section.id === selectedSectionId);
62 const selectedSection = sections[selectedIndex];
63 if (!selectedSection) {
64 return [];
65 }
66
67 let sourceCharacterCount = getReviewParsedDiffSourceCharacterCount({
68 threadKey,
69 sectionId: selectedSection.id,
70 diff: selectedSection.diff,
71 });
72 if (sourceCharacterCount > MAX_CACHED_REVIEW_SOURCE_CHARACTERS) {
73 return [];
74 }
75
76 const pendingSections: ReviewSectionItem[] = [];
77 for (let distance = 1; distance < sections.length; distance += 1) {
78 for (const index of [selectedIndex - distance, selectedIndex + distance]) {
79 if (pendingSections.length >= MAX_CACHED_REVIEW_DIFFS - 1) {
80 return pendingSections;
81 }
82 const section = sections[index];
83 if (!section || section.diff === null) {
84 continue;
85 }
86 const sectionCharacterCount = getReviewParsedDiffSourceCharacterCount({
87 threadKey,
88 sectionId: section.id,
89 diff: section.diff,
90 });
91 if (sourceCharacterCount + sectionCharacterCount > MAX_CACHED_REVIEW_SOURCE_CHARACTERS) {
92 continue;
93 }
94 pendingSections.push(section);
95 sourceCharacterCount += sectionCharacterCount;
96 }
97 }
98 return pendingSections;
99}
100
101export function useReviewDiffPrewarming(input: {
102 readonly threadKey: string | null;
103 readonly sections: ReadonlyArray<ReviewSectionItem>;
104 readonly selectedSectionId: string | null;
105}): void {
106 const { sections, selectedSectionId, threadKey } = input;
107
108 // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- prewarms pending review diff sections in idle callbacks and cancels the idle handle on cleanup
109 useEffect(() => {
110 if (!threadKey) {
111 return;
112 }
113
114 const pendingSections = getReviewDiffPrewarmSections({
115 threadKey,
116 sections,
117 selectedSectionId,
118 });
119 if (pendingSections.length === 0) {
120 return;
121 }
122
123 let cancelled = false;
124 let idleHandle: number | null = null;
125 let nextSectionIndex = 0;
126
127 const scheduleNext = () => {
128 idleHandle = scheduleIdle(() => {
129 if (cancelled) {
130 return;
131 }
132
133 const section = pendingSections[nextSectionIndex];
134 if (!section) {
135 return;
136 }
137 nextSectionIndex += 1;
138 prewarmReviewDiffSection({ threadKey, section });
139
140 if (nextSectionIndex < pendingSections.length) {
141 scheduleNext();
142 }
143 });
144 };
145
146 scheduleNext();
147 return () => {
148 cancelled = true;
149 if (idleHandle !== null) {
150 cancelIdle(idleHandle);
151 }
152 };
153 }, [sections, selectedSectionId, threadKey]);
154}