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

useReviewDiffPrewarming.ts

153 lines4.2 KB

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;
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;
29​
30function cancelIdle(handle: number): void {
31 if (typeof globalThis.cancelIdleCallback === "function") {
32 globalThis.cancelIdleCallback(handle);
33 return;
34 }
35 clearTimeout(handle);
37​
38export function 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: [] });
54​
55export function 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;
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 useEffect(() => {
109 if (!threadKey) {
110 return;
111 }
112​
113 const pendingSections = getReviewDiffPrewarmSections({
114 threadKey,
115 sections,
116 selectedSectionId,
117 });
118 if (pendingSections.length === 0) {
119 return;
120 }
121​
122 let cancelled = false;
123 let idleHandle: number | null = null;
124 let nextSectionIndex = 0;
125​
126 const scheduleNext = () => {
127 idleHandle = scheduleIdle(() => {
128 if (cancelled) {
129 return;
130 }
131​
132 const section = pendingSections[nextSectionIndex];
133 if (!section) {
134 return;
135 }
136 nextSectionIndex += 1;
137 prewarmReviewDiffSection({ threadKey, section });
138​
139 if (nextSectionIndex < pendingSections.length) {
140 scheduleNext();
141 }
142 });
143 };
144​
145 scheduleNext();
146 return () => {
147 cancelled = true;
148 if (idleHandle !== null) {
149 cancelIdle(idleHandle);
150 }
151 };
152 }, [sections, selectedSectionId, threadKey]);