From 69d5822cf9cd1a871d3de9ae886021949f0f3f0d Mon Sep 17 00:00:00 2001 From: Taksh Date: Thu, 13 Aug 2026 21:27:39 +0530 Subject: [PATCH] fix(desktop): keep the timeline settle write out of the resize pass `useVirtualizedViewportResize` called `settleAtBottom` synchronously from its ResizeObserver callback. That path runs `pinToBottom` -> `scrollToIndex`, which shifts Virtua's rendered range, re-measures rows and can toggle the scrollbar -- resizing the observed element at a deeper depth of the pass that is delivering to us. The browser detects the re-entrancy and logs "ResizeObserver loop completed with undelivered notifications" continuously on a live channel. Defer the settle to the next animation frame, mirroring the rAF defer the sibling observer in `useVirtualizedBottomSettle` already uses, and skip deliveries whose geometry is unchanged -- the callback took no `entries`, so a resize that round-tripped to the same layout still scrolled. Lift the callback into `createVirtualizedViewportResizeHandler` so the frame handling is testable without a DOM. Refs #5717 Signed-off-by: Taksh --- .../ui/useVirtualizedViewportResize.test.mjs | 146 +++++++++++++++++- .../ui/useVirtualizedViewportResize.ts | 99 ++++++++++-- 2 files changed, 234 insertions(+), 11 deletions(-) diff --git a/desktop/src/features/messages/ui/useVirtualizedViewportResize.test.mjs b/desktop/src/features/messages/ui/useVirtualizedViewportResize.test.mjs index e284460c172..a27dc270cd3 100644 --- a/desktop/src/features/messages/ui/useVirtualizedViewportResize.test.mjs +++ b/desktop/src/features/messages/ui/useVirtualizedViewportResize.test.mjs @@ -1,7 +1,11 @@ import assert from "node:assert/strict"; import test from "node:test"; -import { shouldSettleVirtualizedViewportResize } from "./useVirtualizedViewportResize.ts"; +import { + createVirtualizedViewportResizeHandler, + readObservedViewportSize, + shouldSettleVirtualizedViewportResize, +} from "./useVirtualizedViewportResize.ts"; test("viewport resize follows the virtualizer's explicit bottom state", () => { assert.equal( @@ -13,3 +17,143 @@ test("viewport resize follows the virtualizer's explicit bottom state", () => { false, ); }); + +test("a resize that does not change geometry does not settle", () => { + const size = { width: 800, height: 600 }; + assert.equal( + shouldSettleVirtualizedViewportResize({ + virtualizerAtBottom: true, + previousSize: size, + nextSize: { ...size }, + }), + false, + ); +}); + +test("a real geometry change still settles", () => { + const previousSize = { width: 800, height: 600 }; + assert.equal( + shouldSettleVirtualizedViewportResize({ + virtualizerAtBottom: true, + previousSize, + nextSize: { width: 800, height: 540 }, + }), + true, + ); + assert.equal( + shouldSettleVirtualizedViewportResize({ + virtualizerAtBottom: true, + previousSize, + nextSize: { width: 783, height: 600 }, + }), + true, + ); +}); + +test("the first delivery settles because there is no baseline to compare", () => { + assert.equal( + shouldSettleVirtualizedViewportResize({ + virtualizerAtBottom: true, + previousSize: null, + nextSize: { width: 800, height: 600 }, + }), + true, + ); +}); + +test("border-box sizing is preferred over contentRect", () => { + assert.deepEqual( + readObservedViewportSize({ + borderBoxSize: [{ inlineSize: 800, blockSize: 600 }], + contentRect: { width: 1, height: 2 }, + }), + { width: 800, height: 600 }, + ); + assert.deepEqual( + readObservedViewportSize({ contentRect: { width: 640, height: 480 } }), + { width: 640, height: 480 }, + ); + assert.equal(readObservedViewportSize({}), null); +}); + +// ── Observer wiring ────────────────────────────────────────────────────────── + +function harness({ atBottom = true } = {}) { + const frames = []; + const cancelled = []; + let settles = 0; + const handler = createVirtualizedViewportResizeHandler({ + virtualizerAtBottomRef: { current: atBottom }, + settleAtBottom: () => { + settles += 1; + }, + requestFrame: (cb) => { + frames.push(cb); + return frames.length; + }, + cancelFrame: (handle) => cancelled.push(handle), + }); + return { + handler, + cancelled, + settles: () => settles, + pendingFrames: () => frames.length, + runFrames: () => { + const queued = frames.splice(0); + for (const cb of queued) cb(); + }, + deliver: (width, height) => + handler.handleEntries([ + { borderBoxSize: [{ inlineSize: width, blockSize: height }] }, + ]), + }; +} + +test("the settle write is deferred out of the delivery pass", () => { + const h = harness(); + h.deliver(800, 600); + // Nothing may run synchronously — a scroll write here resizes the observed + // element inside the pass that is delivering to us. + assert.equal(h.settles(), 0); + assert.equal(h.pendingFrames(), 1); + h.runFrames(); + assert.equal(h.settles(), 1); +}); + +test("a re-entrant delivery does not queue a second frame", () => { + const h = harness(); + h.deliver(800, 600); + h.deliver(800, 540); + assert.equal(h.pendingFrames(), 1); + h.runFrames(); + assert.equal(h.settles(), 1); +}); + +test("a resize back to the same geometry settles nothing", () => { + const h = harness(); + h.deliver(800, 600); + h.runFrames(); + assert.equal(h.settles(), 1); + + h.deliver(800, 600); + assert.equal(h.pendingFrames(), 0); + h.runFrames(); + assert.equal(h.settles(), 1, "identical geometry must not re-settle"); +}); + +test("geometry is tracked even while the virtualizer is not at bottom", () => { + const h = harness({ atBottom: false }); + h.deliver(800, 600); + h.runFrames(); + assert.equal(h.settles(), 0); +}); + +test("cancel releases a pending frame", () => { + const h = harness(); + h.deliver(800, 600); + h.handler.cancel(); + assert.deepEqual(h.cancelled, [1]); + // A second cancel is a no-op rather than a double free. + h.handler.cancel(); + assert.deepEqual(h.cancelled, [1]); +}); diff --git a/desktop/src/features/messages/ui/useVirtualizedViewportResize.ts b/desktop/src/features/messages/ui/useVirtualizedViewportResize.ts index d3273a09b62..292bbeb9001 100644 --- a/desktop/src/features/messages/ui/useVirtualizedViewportResize.ts +++ b/desktop/src/features/messages/ui/useVirtualizedViewportResize.ts @@ -1,11 +1,89 @@ import * as React from "react"; +export type VirtualizedViewportSize = { width: number; height: number }; + export function shouldSettleVirtualizedViewportResize({ virtualizerAtBottom, + previousSize, + nextSize, }: { virtualizerAtBottom: boolean; + /** Last size this observer acted on, or null before the first delivery. */ + previousSize?: VirtualizedViewportSize | null; + nextSize?: VirtualizedViewportSize | null; }): boolean { - return virtualizerAtBottom; + if (!virtualizerAtBottom) return false; + // No baseline yet (or no readable box): the first delivery still settles, as + // it always has. + if (!previousSize || !nextSize) return true; + // A resize that round-trips to the same geometry has nothing to settle, and + // settling anyway is what keeps re-entering the observation pass. + return ( + previousSize.width !== nextSize.width || + previousSize.height !== nextSize.height + ); +} + +export function readObservedViewportSize( + entry: ResizeObserverEntry, +): VirtualizedViewportSize | null { + const borderBox = entry.borderBoxSize?.[0]; + if (borderBox) { + return { width: borderBox.inlineSize, height: borderBox.blockSize }; + } + const rect = entry.contentRect; + if (!rect) return null; + return { width: rect.width, height: rect.height }; +} + +/** + * The ResizeObserver callback, lifted out of the hook so it can be driven + * without a DOM. + * + * `settleAtBottom` scrolls, which re-measures rows and can toggle the + * scrollbar — resizing the observed element at a deeper depth of the pass that + * is delivering to us. Running the write on the next frame keeps it out of that + * pass, so the browser stops reporting "ResizeObserver loop completed with + * undelivered notifications". This mirrors the rAF defer the sibling observer in + * `useVirtualizedBottomSettle` already uses. + */ +export function createVirtualizedViewportResizeHandler({ + virtualizerAtBottomRef, + settleAtBottom, + requestFrame, + cancelFrame, +}: { + virtualizerAtBottomRef: { current: boolean }; + settleAtBottom: () => void; + requestFrame: (cb: () => void) => number; + cancelFrame: (handle: number) => void; +}) { + let previousSize: VirtualizedViewportSize | null = null; + let frame: number | null = null; + + return { + handleEntries(entries: readonly ResizeObserverEntry[]) { + const entry = entries.at(-1); + const nextSize = entry ? readObservedViewportSize(entry) : null; + const settle = shouldSettleVirtualizedViewportResize({ + virtualizerAtBottom: virtualizerAtBottomRef.current, + previousSize, + nextSize, + }); + if (nextSize) previousSize = nextSize; + if (!settle || frame !== null) return; + frame = requestFrame(() => { + frame = null; + settleAtBottom(); + }); + }, + cancel() { + if (frame !== null) { + cancelFrame(frame); + frame = null; + } + }, + }; } /** Re-settles a bottom-pinned virtualized timeline after viewport reflow. */ @@ -24,16 +102,17 @@ export function useVirtualizedViewportResize( return; } - const observer = new ResizeObserver(() => { - if ( - shouldSettleVirtualizedViewportResize({ - virtualizerAtBottom: virtualizerAtBottomRef.current, - }) - ) { - settleAtBottom(); - } + const handler = createVirtualizedViewportResizeHandler({ + virtualizerAtBottomRef, + settleAtBottom, + requestFrame: requestAnimationFrame, + cancelFrame: cancelAnimationFrame, }); + const observer = new ResizeObserver(handler.handleEntries); observer.observe(container); - return () => observer.disconnect(); + return () => { + observer.disconnect(); + handler.cancel(); + }; }, [scrollContainerRef, settleAtBottom, virtualizerAtBottomRef]); }