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

useReviewDiffPrewarming.ts

158 lines4.4 KB

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