Skip to content

Build your personal knowledge base with Trilium Notes

fix(collections/board): stop the resize loop error on clearing a filter

Elian Doran650c29f0c7fd
diff --git a/apps/client/src/widgets/collections/board/windowing.spec.tsx b/apps/client/src/widgets/collections/board/windowing.spec.tsx
index 49e2eb1caf..b666594e49 100644
--- a/apps/client/src/widgets/collections/board/windowing.spec.tsx
+++ b/apps/client/src/widgets/collections/board/windowing.spec.tsx
@@ -1,6 +1,6 @@
import { render } from "preact";
import { act } from "preact/test-utils";
-import { afterEach, beforeEach, describe, expect, it } from "vitest";
+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import {
askForCard, type ColumnWindow, computeWindow, estimateFor, forgetWindowHeights,
@@ -219,6 +219,7 @@ describe("useColumnWindow", () => {
});
afterEach(() => {
+ vi.unstubAllGlobals();
if (host) render(null, host);
host?.remove();
area?.remove();
@@ -278,6 +279,35 @@ describe("useColumnWindow", () => {
expect(last().windowChanged).toBe(true);
});
+ it("reads a resize on the next frame, not inside the observer's pass", async () => {
+ let reportResize = () => {};
+ vi.stubGlobal("ResizeObserver", class {
+ constructor(callback: () => void) {
+ reportResize = callback;
+ }
+ observe() {}
+ disconnect() {}
+ });
+ const { seen, last } = await probe(ids(2000));
+ const frames: FrameRequestCallback[] = [];
+ vi.stubGlobal("requestAnimationFrame", (frame: FrameRequestCallback) => frames.push(frame));
+ vi.stubGlobal("cancelAnimationFrame", () => {});
+
+ const before = last().bounds.until;
+ if (area) Object.defineProperty(area, "clientHeight", { value: 6000, configurable: true });
+ const renders = seen.length;
+ await act(async () => reportResize());
+
+ // Drawing inside the pass mounts cards whose first resize Chrome can no longer deliver.
+ expect(seen.length).toBe(renders);
+ expect(frames).toHaveLength(1);
+
+ await act(async () => {
+ for (const frame of frames.splice(0)) frame(0);
+ });
+ expect(last().bounds.until).toBeGreaterThan(before);
+ });
+
it("scrolls to a card the column is not drawing, placing it inside the area", async () => {
const { last } = await probe(ids(2000));
diff --git a/apps/client/src/widgets/collections/board/windowing.ts b/apps/client/src/widgets/collections/board/windowing.ts
index c305c98f2d..b6aa909661 100644
--- a/apps/client/src/widgets/collections/board/windowing.ts
+++ b/apps/client/src/widgets/collections/board/windowing.ts
@@ -229,12 +229,21 @@ export function useColumnWindow(
read();
area.addEventListener("scroll", read, { passive: true });
- const resize = new ResizeObserver(read);
+ // A resize is read on the next frame. Drawing inside the observer's pass mounts cards
+ // whose first resize the browser can no longer deliver, which it reports as an error.
+ let frame: number | undefined;
+ const resize = new ResizeObserver(() => {
+ frame ??= requestAnimationFrame(() => {
+ frame = undefined;
+ read();
+ });
+ });
resize.observe(area);
return () => {
area.removeEventListener("scroll", read);
resize.disconnect();
+ if (frame !== undefined) cancelAnimationFrame(frame);
};
}, [ areaRef, enabled ]);