Skip to content

Commit fdd8919

Browse files
EVWorthclaude
andauthored
fix(grid): stop hijacking the wheel, and stop rebuilding observers (#542)
Closes #407, #417. ## The wheel (#407) Past 5000 rows the grid installed a non-passive wheel listener that called preventDefault and moved scrollTop itself: const SCROLL_BOOST = Math.min(totalRows / 100, 100); el.scrollTop += e.deltaY * SCROLL_BOOST; The multiplier came from the row count, which is not a property of the input device. On a 10K-row result one notch of a mouse wheel travelled 10,000px, about 312 rows, so the grid was unusable for finding anything. A trackpad sends small pixel deltas many times a second and got the same treatment from the other direction, feeling stuck. deltaMode, which is what actually says whether a delta is in pixels, lines or pages, was never read. Deleted. Native scrolling and the virtualizer already handle this between them, and preventDefault on a non-passive listener was costing a main-thread round trip per event to reimplement it worse. ## The observer (#417) TruncatedCell's ResizeObserver effect listed `formatted` in its dependencies, so every value change tore the observer down and constructed another. A grid polling SELECT NOW(), or a column being typed into, does that for every visible cell on every render. Nothing leaked — the cleanup did disconnect them — so the issue's framing overstates it. The cost is churn: hundreds of constructions a second all doing the work of one, showing up as CPU burnt on an idle grid. Split in two. The observer is created once per mounted cell and watches for the box changing size. A second effect re-measures when the text changes, which is a single layout read and what the original effect was really for. `columnName` and `dataType` were dependencies too, and neither bears on whether text overflows its box. ## Tests - one browser test asserting no non-passive wheel listener is installed on a 5001-row result; React's own root registers a passive one, which by definition cannot preventDefault, so the assertion targets the kind that can - three on observer churn: one observer across 25 value changes, one across column-metadata changes, and still disconnected on unmount The wheel test and the first two churn tests fail against the previous implementation. The unmount test passes either way — it guards behaviour the split could plausibly have broken, and did not. Co-authored-by: Claude Opus 5 <[email protected]>
1 parent 4518ee6 commit fdd8919

4 files changed

Lines changed: 99 additions & 16 deletions

File tree

‎src/components/grid/ResultsGrid.tsx‎

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -556,18 +556,6 @@ export function ResultsGrid() {
556556

557557
const scrollContainerRef = useRef<HTMLDivElement | null>(null);
558558

559-
useEffect(() => {
560-
const el = scrollContainerRef.current;
561-
if (!el || totalRows <= 5000) return;
562-
const SCROLL_BOOST = Math.min(totalRows / 100, 100);
563-
const onWheel = (e: WheelEvent) => {
564-
e.preventDefault();
565-
el.scrollTop += e.deltaY * SCROLL_BOOST;
566-
};
567-
el.addEventListener("wheel", onWheel, { passive: false });
568-
return () => el.removeEventListener("wheel", onWheel);
569-
}, [totalRows]);
570-
571559
const rowVirtualizer = useVirtualizer({
572560
count: totalRows,
573561
getScrollElement: () => scrollContainerRef.current,

‎src/components/grid/TruncatedCell.tsx‎

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,23 +47,41 @@ export function TruncatedCell({
4747
const [isOverflowing, setIsOverflowing] = useState(false);
4848
const textRef = useRef<HTMLDivElement | null>(null);
4949

50+
// One observer for as long as the cell is mounted.
51+
//
52+
// This used to list `formatted` among its dependencies, so every value
53+
// change tore the observer down and built a new one. A grid polling
54+
// `SELECT NOW()`, or a column being typed into, does that for every visible
55+
// cell on every render — hundreds of ResizeObserver constructions a second,
56+
// all doing the work of one. Nothing leaked, since the cleanup did
57+
// disconnect them, but the churn is pure waste and it showed up as CPU
58+
// burnt on an idle grid (#417).
59+
//
60+
// `columnName` and `dataType` were dependencies too, and neither has any
61+
// bearing on whether text overflows its box.
5062
useEffect(() => {
5163
const el = textRef.current;
5264
if (!el) return;
5365
let scheduled = false;
54-
const check = () => setIsOverflowing(el.scrollWidth > el.clientWidth);
5566
const ro = new ResizeObserver(() => {
5667
if (scheduled) return;
5768
scheduled = true;
5869
requestAnimationFrame(() => {
5970
scheduled = false;
60-
check();
71+
setIsOverflowing(el.scrollWidth > el.clientWidth);
6172
});
6273
});
6374
ro.observe(el);
64-
check();
6575
return () => ro.disconnect();
66-
}, [formatted, columnName, dataType]);
76+
}, []);
77+
78+
// Text can change without the box resizing, and that changes whether it
79+
// overflows — so the measurement still has to re-run per value. One layout
80+
// read, which is what the old effect was really for.
81+
useEffect(() => {
82+
const el = textRef.current;
83+
if (el) setIsOverflowing(el.scrollWidth > el.clientWidth);
84+
}, [formatted]);
6785

6886
const showIcon = showForTextType || isOverflowing;
6987
const openViewer = () => {

‎src/components/grid/__tests__/ResultsGrid.browser.test.tsx‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1211,3 +1211,33 @@ describe("Copy as INSERT", () => {
12111211
writeSpy.mockRestore();
12121212
});
12131213
});
1214+
1215+
// ─── Scrolling ────────────────────────────────────────────────
1216+
describe("scrolling a virtualized result", () => {
1217+
it("does not intercept the wheel", () => {
1218+
// Past 5000 rows the grid installed a non-passive wheel listener that
1219+
// called preventDefault and moved scrollTop itself, multiplied by
1220+
// min(rows/100, 100). On a 10K-row result one notch of a mouse wheel
1221+
// travelled 10,000px — about 312 rows — while a trackpad's small
1222+
// pixel deltas were left feeling stuck. Native scrolling and the
1223+
// virtualizer handle this between them (#407).
1224+
const addSpy = vi.spyOn(HTMLElement.prototype, "addEventListener");
1225+
1226+
resultState.results = [
1227+
makeResult({
1228+
rows: Array.from({ length: 5001 }, (_, i) => [i, `row ${i}`]),
1229+
}),
1230+
];
1231+
render(<ResultsGrid />);
1232+
1233+
// React's own root attaches a passive wheel listener, which by definition
1234+
// cannot preventDefault. What must not exist is a non-passive one.
1235+
const hijackers = addSpy.mock.calls.filter(([type, , options]) =>
1236+
type === "wheel"
1237+
&& !(typeof options === "object" && options !== null && options.passive === true)
1238+
);
1239+
expect(hijackers).toEqual([]);
1240+
1241+
addSpy.mockRestore();
1242+
});
1243+
});

‎src/components/grid/__tests__/TruncatedCell.test.tsx‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,14 @@ import { TruncatedCell } from "../TruncatedCell";
44

55
class FakeResizeObserver {
66
static instances: FakeResizeObserver[] = [];
7+
/** Total ever constructed. `instances` only holds the live ones, since
8+
* disconnect removes from it — so it cannot show churn on its own. */
9+
static created = 0;
710
callback: ResizeObserverCallback;
811
observed: Element[] = [];
912
constructor(callback: ResizeObserverCallback) {
1013
this.callback = callback;
14+
FakeResizeObserver.created++;
1115
FakeResizeObserver.instances.push(this);
1216
}
1317
observe(el: Element) {
@@ -34,6 +38,7 @@ beforeAll(() => {
3438

3539
beforeEach(() => {
3640
FakeResizeObserver.instances = [];
41+
FakeResizeObserver.created = 0;
3742
});
3843

3944
afterEach(() => {
@@ -263,3 +268,45 @@ describe("TruncatedCell", () => {
263268
expect(onViewFull).not.toHaveBeenCalled();
264269
});
265270
});
271+
272+
describe("observer churn", () => {
273+
it("keeps one observer across value changes", () => {
274+
// The effect listed `formatted` as a dependency, so every new value tore
275+
// the observer down and built another. A grid polling SELECT NOW(), or a
276+
// column being typed into, does that for every visible cell on every
277+
// render (#417).
278+
const { rerender } = render(
279+
<TruncatedCell value="12:00:00" columnName="now" onViewFull={() => {}} />,
280+
);
281+
expect(FakeResizeObserver.created).toBe(1);
282+
283+
for (let i = 1; i <= 25; i++) {
284+
rerender(
285+
<TruncatedCell value={`12:00:${String(i).padStart(2, "0")}`} columnName="now" onViewFull={() => {}} />,
286+
);
287+
}
288+
289+
expect(FakeResizeObserver.created).toBe(1);
290+
expect(FakeResizeObserver.instances).toHaveLength(1);
291+
});
292+
293+
it("keeps one observer when only the column metadata changes", () => {
294+
// Neither of these has any bearing on whether text overflows its box,
295+
// and both were dependencies.
296+
const { rerender } = render(
297+
<TruncatedCell value="x" columnName="a" dataType="varchar" onViewFull={() => {}} />,
298+
);
299+
rerender(<TruncatedCell value="x" columnName="b" dataType="text" onViewFull={() => {}} />);
300+
301+
expect(FakeResizeObserver.created).toBe(1);
302+
});
303+
304+
it("still disconnects when the cell unmounts", () => {
305+
const { unmount } = render(
306+
<TruncatedCell value="x" columnName="a" onViewFull={() => {}} />,
307+
);
308+
expect(FakeResizeObserver.instances).toHaveLength(1);
309+
unmount();
310+
expect(FakeResizeObserver.instances).toHaveLength(0);
311+
});
312+
});

0 commit comments

Comments
 (0)