Review follow-up. The viewport-scaled lookahead was unbounded, so a tall window prefetched several feed pages on every mount / filter change before any scroll — wasted work on the app's heaviest query. Clamp it to MAX_LOOKAHEAD_ROWS, and move the policy into a pure readAhead.ts (readAheadLookahead / shouldPrefetchNext) with unit tests covering the floor, the scaling, the cap boundary, and the fetch trigger — the core of the feed-scroll fix now has coverage instead of only manual E2E.
45 lines
1.7 KiB
TypeScript
45 lines
1.7 KiB
TypeScript
import { describe, expect, it } from "vitest";
|
|
import {
|
|
MAX_LOOKAHEAD_ROWS,
|
|
PREFETCH_ROWS,
|
|
PREFETCH_VIEWPORTS,
|
|
readAheadLookahead,
|
|
shouldPrefetchNext,
|
|
} from "./readAhead";
|
|
|
|
describe("readAheadLookahead", () => {
|
|
it("floors at PREFETCH_ROWS when nothing (or little) is rendered", () => {
|
|
expect(readAheadLookahead(0)).toBe(PREFETCH_ROWS);
|
|
expect(readAheadLookahead(1)).toBe(PREFETCH_ROWS); // 1*2=2 < floor 4
|
|
});
|
|
|
|
it("scales with the rendered row count for a normal viewport", () => {
|
|
expect(readAheadLookahead(10)).toBe(10 * PREFETCH_VIEWPORTS); // 20
|
|
expect(readAheadLookahead(15)).toBe(30);
|
|
});
|
|
|
|
it("caps at MAX_LOOKAHEAD_ROWS so a tall window doesn't prefetch unboundedly", () => {
|
|
// 30 rendered rows would be 60 uncapped — the cap is what bounds per-mount feed prefetch.
|
|
expect(readAheadLookahead(30)).toBe(MAX_LOOKAHEAD_ROWS);
|
|
expect(readAheadLookahead(1000)).toBe(MAX_LOOKAHEAD_ROWS);
|
|
});
|
|
});
|
|
|
|
describe("shouldPrefetchNext", () => {
|
|
it("fetches once the last rendered row enters the read-ahead window", () => {
|
|
// 100 rows loaded, viewport renders 10 → lookahead 20. Fetch when lastIndex >= 100-1-20 = 79.
|
|
expect(shouldPrefetchNext(78, 100, 10)).toBe(false);
|
|
expect(shouldPrefetchNext(79, 100, 10)).toBe(true);
|
|
expect(shouldPrefetchNext(99, 100, 10)).toBe(true);
|
|
});
|
|
|
|
it("fetches eagerly while fewer rows are loaded than the lookahead (early pages)", () => {
|
|
// Only 15 rows loaded, lookahead 20 → the whole list is inside the window.
|
|
expect(shouldPrefetchNext(8, 15, 10)).toBe(true);
|
|
});
|
|
|
|
it("does not fetch near the top of a long loaded list", () => {
|
|
expect(shouldPrefetchNext(12, 500, 10)).toBe(false);
|
|
});
|
|
});
|