TanStack / virtual

🤖 Headless UI for Virtualizing Large Element Lists in JS/TS, React, Solid, Vue and Svelte
https://tanstack.com/virtual
MIT License
5.57k stars 306 forks source link

Infinite loop in Vue adapter when estimated size doesn't match dynamic size #715

Open markjaniczak opened 7 months ago

markjaniczak commented 7 months ago

Describe the bug

I'm using @tanstack/vue-virtual to render a dynamic list of elements and on specific browsers at specific resolution scaling virtualizer will end up in an infinite loop. I've found these previous issues which seem somewhat related:

https://github.com/TanStack/virtual/issues/619#issuecomment-1969516091 I put my measure function inside of a nextTick as to prevent that same issue.

https://github.com/TanStack/virtual/issues/159#issuecomment-873995201 I found the same issue on my windows machine at 125% scaling; what should be integer pixel sizes are read as decimal pixel sizes when calling getBoundingClientRect inside of my measure function.

In my actual app my list item elements will typically be one of two sizes as they conditionally render a line of text if it's available in the item. My codesandbox example is not exactly the same scenario as I've not been able to reproduce sub pixel sizes exactly but I've attached a couple of videos to help show what's happening.

Your minimal, reproducible example

https://codesandbox.io/p/devbox/runtime-river-jw2kt4

Steps to reproduce

  1. Load the page
  2. Observe that the page is frozen in an infinite loop

Expected behavior

As a user I was expecting the virtualizer to behave the same way on all screens regardless of pixel density.

How often does this bug happen?

Every time

Screenshots or Videos

https://github.com/TanStack/virtual/assets/13617683/aadf7fd5-a1a8-4fb9-86f5-9c9924914277

Platform

Windows 11 Pro 23H2 Edge 123.0.2420.97

tanstack-virtual version

3.4.0

TypeScript version

5.2.2

Additional context

This issue was first noticed on a chromebook screen that had resolution scaling set to 125%. When it was changed to 100% the problem no longer appeared. On the same machine, I could move the window to a connected monitor that was set to 1920x1080 at 100% scaling and couldn't reproduce the issue either.

Terms & Code of Conduct

piecyk commented 7 months ago

hmm @markjaniczak do you use some custom measureElement method, default one already use Math.round for exactly this reason https://github.com/TanStack/virtual/blob/main/packages/virtual-core/src/index.ts#L197-L216

markjaniczak commented 7 months ago

I don't, no. Although I did try supplying my own measureElement and used different methods of rounding and even just returned the decimal value from it.

It doesn't happen when I move measureElement out of the nextTick function so I think it's something to do with the order of how Vue renders after a state change. But I can't do that because then I face the same scrolling issue mentioned in https://github.com/TanStack/virtual/issues/619#issuecomment-1969516091

markjaniczak commented 7 months ago

I've added some logging around here https://github.com/TanStack/virtual/blob/eae81c166c7ecdec477efb2f5d8ab6d897e096ae/packages/virtual-core/src/index.ts#L395-L409 and noticing that the calculated range bounces back and forth: image

markjaniczak commented 7 months ago

I've done a little more digging and found that the issue actually occurs when the estimated sizes don't match the measured sizes and you've wrapped the measureElement function inside a nextTick. But using nextTick seems necessary in order to prevent the scroll issue mentioned in the other issue.

Perhaps the vue adapter also needs to defer state updates with nextTick? https://github.com/TanStack/virtual/blob/eae81c166c7ecdec477efb2f5d8ab6d897e096ae/packages/vue-virtual/src/index.ts#L48-L70