Skip to content

Commit 5d701b3

Browse files
committed
fix(web): scrolling a large diff no longer stalls while collapsed files load
The progressive loader swaps each placeholder for the full parsed file as its patch arrives, even when the file is collapsed. Pierre highlights any item that has content, so every collapsed file that loaded mid-scroll was syntax-highlighted in a worker and its whole result, about 4 MB for a 300-line file, was deserialized on the main thread during the scroll. A collapsed file now hands the viewer a header-only stub and only gets its content once expanded.
1 parent 7812230 commit 5d701b3

3 files changed

Lines changed: 110 additions & 5 deletions

File tree

‎apps/web/src/components/DiffPanel.tsx‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import { useTheme } from "../hooks/useTheme";
3535
import {
3636
buildFileDiffContentVersion,
3737
buildFileDiffIdentityKey,
38+
getCollapsedFileDiffStub,
3839
getDiffCollapseIconClassName,
3940
getDiffLineStat,
4041
getRenderablePatch,
@@ -478,15 +479,16 @@ export default function DiffPanel({
478479
renderableFileEntries
479480
.filter(({ fileDiff }) => !lazySource || readyFilePaths.has(resolveFileDiffPath(fileDiff)))
480481
.map(({ fileDiff, fileKey, fileVersion }) => {
482+
const pending = fileDiff.cacheKey?.endsWith(":pending") === true;
483+
// Header-only placeholders use the viewer's collapsed geometry until their patch arrives.
484+
const collapsed = collapsedDiffFileKeys.has(fileKey) || pending;
481485
return {
482-
fileDiff,
486+
fileDiff:
487+
lazySource && collapsed && !pending ? getCollapsedFileDiffStub(fileDiff) : fileDiff,
483488
filePath: resolveFileDiffPath(fileDiff),
484489
fileKey,
485490
fileVersion,
486-
// Header-only placeholders use the viewer's collapsed geometry until their patch arrives.
487-
collapsed:
488-
collapsedDiffFileKeys.has(fileKey) ||
489-
fileDiff.cacheKey?.endsWith(":pending") === true,
491+
collapsed,
490492
};
491493
}),
492494
[collapsedDiffFileKeys, renderableFileEntries, lazySource, readyFilePaths],

‎apps/web/src/lib/diffRendering.test.ts‎

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
buildFileDiffIdentityKey,
77
buildFileDiffRenderKey,
88
buildPatchCacheKey,
9+
getCollapsedFileDiffStub,
910
getDiffLineStat,
1011
getRenderablePatch,
1112
resolveFileDiffPath,
@@ -426,3 +427,81 @@ describe("a file whose name a patch header cannot carry plainly", () => {
426427
expect(resolveFileDiffPreviousPath(file)).toBe("old\tname.ts");
427428
});
428429
});
430+
431+
describe("getCollapsedFileDiffStub", () => {
432+
const renamedPatch = [
433+
"diff --git a/old-name.ts b/new-name.ts",
434+
"similarity index 50%",
435+
"rename from old-name.ts",
436+
"rename to new-name.ts",
437+
"--- a/old-name.ts",
438+
"+++ b/new-name.ts",
439+
"@@ -1,2 +1,2 @@",
440+
" const shared = 1;",
441+
"-const before = 2;",
442+
"+const after = 3;",
443+
"diff --git a/other.ts b/other.ts",
444+
"--- a/other.ts",
445+
"+++ b/other.ts",
446+
"@@ -1 +1 @@",
447+
"-old",
448+
"+new",
449+
].join("\n");
450+
451+
const parseFiles = () => {
452+
const parsed = getRenderablePatch(renamedPatch, "collapsed-stub");
453+
if (parsed?.kind !== "files") throw new Error("expected parsed files");
454+
const [renamed, other] = parsed.files;
455+
if (!renamed || !other) throw new Error("expected two files");
456+
return { renamed, other };
457+
};
458+
459+
it("drops the content and keeps the header fields", () => {
460+
const { renamed } = parseFiles();
461+
expect(renamed.hunks.length).toBeGreaterThan(0);
462+
463+
const stub = getCollapsedFileDiffStub(renamed);
464+
465+
expect(stub.hunks).toEqual([]);
466+
expect(stub.additionLines).toEqual([]);
467+
expect(stub.deletionLines).toEqual([]);
468+
expect(stub.splitLineCount).toBe(0);
469+
expect(stub.unifiedLineCount).toBe(0);
470+
expect(stub.name).toBe(renamed.name);
471+
expect(stub.prevName).toBe(renamed.prevName);
472+
expect(stub.prevName).toBe("old-name.ts");
473+
expect(stub.type).toBe(renamed.type);
474+
expect(stub.lang).toBe(renamed.lang);
475+
expect(stub.newObjectId).toBe(renamed.newObjectId);
476+
expect(stub.prevObjectId).toBe(renamed.prevObjectId);
477+
});
478+
479+
it("marks the stub collapsed rather than pending", () => {
480+
const { renamed } = parseFiles();
481+
482+
const stub = getCollapsedFileDiffStub(renamed);
483+
484+
expect(stub.cacheKey).toBe(`${renamed.cacheKey}:collapsed`);
485+
expect(stub.cacheKey?.endsWith(":pending")).toBe(false);
486+
});
487+
488+
it("returns the same stub for the same input and a different one for another", () => {
489+
const { renamed, other } = parseFiles();
490+
491+
const first = getCollapsedFileDiffStub(renamed);
492+
const second = getCollapsedFileDiffStub(renamed);
493+
const otherStub = getCollapsedFileDiffStub(other);
494+
495+
expect(second).toBe(first);
496+
expect(otherStub).not.toBe(first);
497+
expect(otherStub.name).toBe("other.ts");
498+
});
499+
500+
it("keeps the file's identity key", () => {
501+
const { renamed } = parseFiles();
502+
503+
const stub = getCollapsedFileDiffStub(renamed);
504+
505+
expect(buildFileDiffIdentityKey(stub)).toBe(buildFileDiffIdentityKey(renamed));
506+
});
507+
});

‎apps/web/src/lib/diffRendering.ts‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,30 @@ export function resolveFileDiffPreviousPath(fileDiff: FileDiffMetadata): string
222222
return fileDiffPath(fileDiff.prevName ?? fileDiff.name ?? "");
223223
}
224224

225+
const collapsedFileDiffStubs = new WeakMap<FileDiffMetadata, FileDiffMetadata>();
226+
227+
/**
228+
* Header-only stand-in for a collapsed file, so the viewer gets no content and never highlights
229+
* lines it does not display. The `:collapsed` suffix keeps it apart from a `:pending` placeholder,
230+
* which disables the expand control.
231+
*/
232+
export function getCollapsedFileDiffStub(fileDiff: FileDiffMetadata): FileDiffMetadata {
233+
const cached = collapsedFileDiffStubs.get(fileDiff);
234+
if (cached) return cached;
235+
const stub: FileDiffMetadata = {
236+
...fileDiff,
237+
hunks: [],
238+
additionLines: [],
239+
deletionLines: [],
240+
splitLineCount: 0,
241+
unifiedLineCount: 0,
242+
isPartial: true,
243+
cacheKey: `${fileDiff.cacheKey ?? resolveFileDiffPath(fileDiff)}:collapsed`,
244+
};
245+
collapsedFileDiffStubs.set(fileDiff, stub);
246+
return stub;
247+
}
248+
225249
/**
226250
* Stable across re-renders of the same file, distinct for every block in a
227251
* patch. A type change (regular file to symlink) arrives as a deletion and an

0 commit comments

Comments
 (0)