Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/calm-scroll-rect-rows.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@tanstack/virtual-core': patch
---

Fix the ResizeObserver loop error when the scroll element shrinks in a framework that removes rows without clearing their refs, such as Solid: rows that a render detaches inside the scroll element's ResizeObserver delivery are now unobserved before the browser delivers again.
7 changes: 7 additions & 0 deletions packages/virtual-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1155,12 +1155,19 @@ export class Virtualizer<
this.observer.observe(cached)
})

let observing = false
this.unsubs.push(
this.options.observeElementRect(this, (rect) => {
this.scrollRect = rect
this.maybeNotify()
// A render inside a delivery can detach rows without a ref-null
// callback (Solid never makes one). Drop them as the row observer
// does. The first call is the initial read, not a delivery, and
// rows that are not attached yet stay observed through it.
if (observing) this.measureElement(null)
}),
)
observing = true

this.unsubs.push(
this.options.observeElementOffset(this, (offset, isScrolling) => {
Expand Down
73 changes: 69 additions & 4 deletions packages/virtual-core/tests/measurement.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1007,8 +1007,9 @@ const flushMicrotasks = () => new Promise((resolve) => setTimeout(resolve))

// A framework that renders in a microtask after `onChange`, as Vue does, and
// unmounts a row as React and Vue do: the ref gets null first, the element
// leaves the document after.
function setupMicrotaskRender() {
// leaves the document after. With `syncWithoutNullRef` it renders inside
// `onChange` and never gives the ref null, as Solid does.
function setupMicrotaskRender({ syncWithoutNullRef = false } = {}) {
const observers: Array<{
callback: ResizeObserverCallback
targets: Set<any>
Expand Down Expand Up @@ -1053,7 +1054,7 @@ function setupMicrotaskRender() {
const wanted = virtualizer.getVirtualItems().map((item) => item.index)
for (const index of [...mounted]) {
if (wanted.includes(index)) continue
virtualizer.measureElement(null)
if (!syncWithoutNullRef) virtualizer.measureElement(null)
node(index).isConnected = false
mounted.delete(index)
}
Expand All @@ -1075,7 +1076,7 @@ function setupMicrotaskRender() {
cb(0, false)
return () => {}
},
onChange: () => queueMicrotask(render),
onChange: () => (syncWithoutNullRef ? render() : queueMicrotask(render)),
})
virtualizer._willUpdate()
render()
Expand Down Expand Up @@ -1108,6 +1109,70 @@ test('stops observing rows that a microtask render detaches in a scroll element
expect(observed()).toContain(node(3))
})

test('stops observing rows that a sync render detaches in a scroll element delivery without clearing their refs', () => {
const {
virtualizer,
el,
node,
mounted,
observers,
observed,
detachedObserved,
} = setupMicrotaskRender({ syncWithoutNullRef: true })
expect([...mounted]).toEqual([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

el.clientHeight = 200
observers.find(({ targets }) => targets.has(el))!.callback([], {} as any)

expect([...mounted]).toEqual([0, 1, 2, 3])
expect(detachedObserved()).toEqual([])
expect([...virtualizer.elementsCache.keys()]).toEqual([0, 1, 2, 3])
expect(observed()).toContain(node(3))
})

test('keeps rows that are not attached yet when the scroll element is first read', () => {
const observe = vi.fn()
const mockWindow = {
requestAnimationFrame: vi.fn(),
cancelAnimationFrame: vi.fn(),
performance: { now: () => Date.now() },
ResizeObserver: vi.fn(function () {
return { observe, unobserve: vi.fn(), disconnect: vi.fn() }
}),
}
const el = {
scrollTop: 0,
scrollLeft: 0,
scrollWidth: 400,
scrollHeight: 5000,
clientWidth: 400,
clientHeight: 500,
ownerDocument: { defaultView: mockWindow },
} as unknown as HTMLDivElement
const virtualizer = new Virtualizer({
count: 100,
estimateSize: () => 50,
initialRect: { width: 400, height: 500 },
getScrollElement: () => el,
scrollToFn: vi.fn(),
observeElementRect,
observeElementOffset: (_instance, cb) => {
cb(0, false)
return () => {}
},
})
const row = {
getAttribute: (name: string) => (name === 'data-index' ? '0' : null),
isConnected: false,
} as unknown as HTMLElement

virtualizer.measureElement(row)
virtualizer._willUpdate()

expect(virtualizer.elementsCache.get(0)).toBe(row)
expect(observe).toHaveBeenCalledWith(row, { box: 'border-box' })
})

test('stops observing rows that a microtask render detaches in a row delivery', async () => {
const { virtualizer, unobserve, node, sizes, deliver } = setupResizeBatch(
(instance, sync) => {
Expand Down
Loading