react-virtualized icon indicating copy to clipboard operation
react-virtualized copied to clipboard

Infinite scroll in reverse order

Open kof opened this issue 10 years ago • 48 comments

Hi, first about my use case: I am building a chat app. Which means it is essentially important to:

  • be able to render a huge amount of rows
  • be able to request additional rows
  • be able to jump in the middle of a 1 year long conversation

This means that the list we are going to render is potentially just a fragment of a very long list, there are rows after what we have got and there are rows before.

We need to be able to scroll up and load previous rows as we go, as well as to scroll down and load next rows.

kof avatar May 04 '16 19:05 kof

So the missing part is right now the ability to scroll up (beginning of a vertical list) and request previous rows.

To go more technically:

  • when adding rows at the beginning of the list, scroller stays at the same place, it needs to go scrollTop + addedRowsHeight
  • InfiniteLoader logic needs to scan for unloaded ranges correctly considering scroll direction.

kof avatar May 04 '16 19:05 kof

This means that startIndex in InfiniteLoader can actually become a negative number.

kof avatar May 04 '16 20:05 kof

Hey @kof,

Reading this feature request, I wonder if you've considered doing something like...

  • Listen to onRowsRendered to detect when row 0 is displayed. (This would be your tip to load older rows.) Row 0 might be an actual chat log message or a placeholder "Load older messages" indicator.
  • When a set of older rows finishes loading, add them to the beginning of your List/Array- and give VirtualScroll the updated list count along with a scrollToIndex that points to the previous row 0 (eg. scrollToIndex should be the length of the newly-loaded rows).

I think that would work as you've described?

If not, I think it may be appropriate for you to create a fork of InfiniteLoader for your specific use case. I don't think it makes sense for the HOC to support negative indexes in the common case.

bvaughn avatar May 05 '16 05:05 bvaughn

I have created my IniniteLoader based on the original one. However I can't imagine this is an edge case. This is nothing else but a very long list where you jump in the middle of it and want to go back.

Imagine google search with 1000000 results and you can't load them on. Now you are on a page x and want to go back. With pagination you just click on the previous page number. Exactly the same is here.

kof avatar May 05 '16 06:05 kof

When a set of older rows finishes loading, add them to the beginning of your List/Array- and give VirtualScroll the updated list count along with a scrollToIndex that points to the previous row 0 (eg. scrollToIndex should be the length of the newly-loaded rows)

This was my first idea and resulted in junked scrolling, because:

  1. I start loading items before the first row is rendered, same logic we have right now, uses threshold param.
  2. Once loading is done, scroll position has evtl. changed, so now I need to set scrollTop = newRenderedRowsHeight + previousScrollTop

kof avatar May 05 '16 09:05 kof

Imagine google search with 1000000 results and you can't load them on. Now you are on a page x and want to go back. With pagination you just click on the previous page number. Exactly the same is here.

It doesn't seem like the same thing to me @kof. Google doesn't show you the middle results as page 0 (or 1).

Everything component and utility in react-virtualized is written to work with a list or array- things that don't make sense when we start talking about negative indexes. Despite what you say, yours is the first request I've ever received to support negative indexes.

bvaughn avatar May 05 '16 15:05 bvaughn

Google doesn't show you the middle results as page 0 (or 1).

Well imagine you got a link to the page 100. You don't load all the results before, right?

Everything component and utility in react-virtualized is written to work with a list or array- things that don't make sense when we start talking about negative indexes

Well maybe negative indexes isn't the right approach anyways. My interest is to unlock the use case. How is a different question.

kof avatar May 06 '16 17:05 kof

I mentioned a potential workaround using scrollToIndex. I understand your concern about scrollTop maybe having changed while the older chat records were loading but I still think that's the best approach. Maybe an improvement to my original suggestion would be...

  • Treat row 0 in your list as a place holder and render a "Loading more rows..." in it.
  • Listen to onScroll to detect when scrollTop is 0. When this happens, fire off a request for the next chunk of older rows.
  • Keep listening to scrollTop while your fetch is in progress.
  • When fetch completes, set scrollTop equal to the height of the newly-loaded rows plus the value of scrollTop from the most recent scrollTop call. (This should account for the case of the user scrolling back down to view newer messages.)

There are some limitations of how react-virtualized can be used. That's because I've written 99% of the code and I only have so much time.

Feel free to propose (and submit) a new HOC if you think there's an alternative to InfiniteLoader that might be able to better handle this.

bvaughn avatar May 06 '16 17:05 bvaughn

understand your concern about scrollTop maybe having changed

additionally threshold option would mean that loading is started before scrollTop is 0, once everything is loaded I would need to know how many rows will be rendered to scroll to the last known position.

kof avatar May 06 '16 18:05 kof

set scrollTop equal to the height of the newly-loaded rows

not exactly right, we might render less than we load, which means I need to calculate height of newly-rendered rows.

kof avatar May 06 '16 18:05 kof

additionally threshold option would mean that loading is started before scrollTop is 0, once everything is loaded I would need to know how many rows will be rendered to scroll to the last known position.

So implement your own threshold ? (Rather than checking for scrollOffset 0, check for 100- or whatever else you want to use for a threshold.) I don't think that's too much burden to put on application code.

not exactly right, we might render less than we load, which means I need to calculate height of newly-rendered rows.

That's not how RV components work. To render (and position) row N you need to have measured rows 0...N.

bvaughn avatar May 06 '16 19:05 bvaughn

I don't think there's any action for me to take on this issue at the moment so I'm going to close it. We can continue discussion though- I just like to keep the issues list pruned.

bvaughn avatar May 09 '16 03:05 bvaughn

@kof I'm needing to do the same kind of thing. Chat...which should really be bottom to top scrolling. I'm curious if you ever found a workable solution...I managed to get variable height rows working by pre-rendering in componentWillReceiveProps (I couldn't get CellMeasurer to work with redux), but I'm stuck on the bottom-to-top layout.

It works fine if I just download all messages in a conversation up front. I'm not sure at what point this will start to be a performance concern. It would be ideal if I could structure my array newest-to-oldest and just have the RV component render bottom to top. No idea what that would take. Anyway, I was just curious if you had suggestions.

jamesbillinger avatar Jul 07 '16 13:07 jamesbillinger

I have one, but I still didn't come to make it ready for open source.

kof avatar Jul 07 '16 14:07 kof

Also my requirement is to load infinite amount of messages and also being able to start in the middle of that stream. I have implemented a bunch of components to do that.

kof avatar Jul 07 '16 14:07 kof

No worries...and thanks for the response. I think for now I may just load the last 100 (or so) messages, and then offer a button to load all (slowly) if need be.

jamesbillinger avatar Jul 07 '16 16:07 jamesbillinger

Another example of the same bi-directional scrolling need: calendars. react-infinite-calendar solves this by having far-away start- and end-dates. Not that big of an issue for a calendar, but still a hack. Could a new HOC address this? I'd be happy to give it a try. Or are there too many insurmountable issues?

smhg avatar Jul 08 '16 09:07 smhg

The biggest issue I can see- other than the added complexity from additional forking behavior- would be with dynamically measured content (eg a chat list using CellMeasurer).

react-virtualized has been optimized to defer measuring cells until needed for better performance. If you have a list of 1,000,000,000 rows, but only the first 15 are visible, it only measures the first 15 and uses an estimated height for the rest. If you scroll down to rows 50...64, it will measure 15...64 but then continue using the estimated height for 65+.

The reason it measures rows 15...64 if a user jumps from 0...14 to 50...64 is a bit complicated to explain. Basically once react-virtualized measures a certain cell- its size and position is cached (unless certain properties change which tell it to clear the cache) to avoid having to recalculate it later. (This is another performance tweak.) If we were to continue using estimated row heights for rows 15...64, then as a user scrolled up from row 50- we would have to run through every row after the current row and slightly adjust their position to match their newly-calculated position. (This gets very expensive if a user jumps to the end of a list and then scrolls to the beginning.)

Now, some caveats:

  • We could skip this update if the estimated and actual row height were the same. (Meaning fixed-height rows could avoid this performance cost.)
  • We could try alternately just throwing away pre-computed/cached rows after an updated one (eg if you jumped from 0...14 to 50...64 and then 30...44- we could throw away cached positions for 50...64 and just re-calculate them later if needed).

I have thought a little about doing this. I may even open an issue for it now while I'm thinking about it.

Anyway, the reason I mention this long-winded explanation is- displaying items in reverse order makes this issue much more prevalent. Every time you add a new item, it pushes everything else down and requires us to update offsets. With the current implementation- this would be very expensive if a user were scrolled to the end of a long chat list.

Edit: I've created issue #309 for this.

bvaughn avatar Jul 08 '16 13:07 bvaughn

I believe I understand the issue. Absolutely positioning a row requires knowledge of the distance from the top - the height of all of the rows above it. And resizing a row at the top requires repositioning every row to the last one displayed. Doing this while also scrolling up results in janky scrolling at best.

It seemed like this might be as simple as absolutely positioning from bottom rather than top with index 0 at the bottom. However, browsers are designed to maintain scroll position based on distance from top rather than bottom, so I'm not sure how that would work. The start/stop indexes would stay the same. but what would happen to the div when content was added to the "top" of the div - absolutely positioned from the bottom?

I'm sure that wouldn't work either. Even Google Hangouts web client is janky when scrolling up. This must just be something that just doesn't work well in web.

The one thing I noticed about Hangouts though...they use relative positioning rather than absolute. I wonder if this allows them to avoid measuring each row?

Thanks again for the conversation on this. I'm not sure if this is the best place for it or if a solution is possible, but the attention is certainly appreciated.

jamesbillinger avatar Jul 11 '16 12:07 jamesbillinger

Doing this while also scrolling up results in janky scrolling at best.

Yes. I can estimate the position based on the number of rows before it and their estimated size, but if the actual size differs, it can cause things to be "janky" (rows might pop in or out of visibility unexpectedly as we replace the estimates with real measurements).

It seemed like this might be as simple as absolutely positioning from bottom rather than top...

I think this would have the same UX behavior while scrolling. (If real measurements differ from estimated ones, things would look broken. The more they differ- the more they would look broken.

The one thing I noticed about Hangouts though...they use relative positioning rather than absolute. I wonder if this allows them to avoid measuring each row?

This is a possibility I haven't considered much. Maybe there's something there. I'm not sure. I think it might still be a little janky because I think we would have to essentially assume all rows (or columns) are the same size and then dynamically shift them around a bit faster (or slower) as a user scrolls based on their actual size. This might make it look like the scrolling speed is inconsistent if you have a bunch of short rows followed by a couple of really tall rows.

bvaughn avatar Jul 12 '16 01:07 bvaughn