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

Debounce InifiniteLoader's loadMoreRows

Open cosminnicula opened this issue 10 years ago • 3 comments

While playing around with the InifiniteLoader, I noticed the following: while scrolling down into the list that's powering the InfiniteLoader example, loadMoreRows() can be called a large number of times with the ranges (startIndex and stopIndex) that are related to the scroll position. To see exactly what I mean, I inserted a console.log(startIndex + '-' + stopIndex + ' -> make a server request') inside the InfiniteLoader.example -> _loadMoreRows() and here's the results in the console:

0-21 -> make a server request
22-22 -> make a server request
23-23 -> make a server request
24-24 -> make a server request
25-25 -> make a server request
26-26 -> make a server request
27-27 -> make a server request
...
65-67 -> make a server request

It's obvious that if it was to make a server request for each and every single one of these ranges, then the performance wouldn't be that good. A simple fix for this problem would be to debounce the server requests while merging the ranges, like below:

0-21 -> make a server request
22-44 -> make a server request
45-67 -> make a server request

Since the signature of the loadMoreRows forces me to return a Promise, I can't do in the constructor something like this:

constructor (props) {
...
// _ is http://underscorejs.org
this._loadMoreRows = _.debounce(this._loadMoreRows.bind(this), 200);
...
}

So, given the previous restriction, here's the implementation that I managed to find:

...
let requestedRanges = [];
...

constructor (props) {
  ...
  this._debouncedLoadMoreRows = _.debounce(this._debouncedLoadMoreRows.bind(this), 200)
}

_debouncedAjaxRequest () {
  // do a server request with each set of merged ranges and resolve all the Promises inside that range
}

_loadMoreRows ({ startIndex, stopIndex }) {
  ...
  const p  = new Promise(resolve => promiseResolver = resolve)
  requestedRanges.push({startIndex, stopIndex, promise: p, promiseResolver});
  this._debouncedLoadMoreRows();

  return p
}

I'm just wondering if there's a better way to do this?

cosminnicula avatar Apr 25 '16 07:04 cosminnicula

Hey @cosminnicula,

Thanks for taking the time to create this issue and explain your thinking. :)

I understand the current implementation of InfiniteLoader is a bit limited. I felt it might be best to leave control over debouncing or batching requests to the end user rather than try to guess at how you might want to do it.

It's actually pretty complicated to ensure that requests are batched in the way you've described when you take into account all of the edge cases. I'm a little hesitant to take it on. I'll give it some thought.

bvaughn avatar Apr 26 '16 04:04 bvaughn

Check out the new 6.3.0 release. InfiniteLoader now has a new property, minimumBatchSize. This defaults to 10. It will let you batch rows the way you're asking. :)

bvaughn avatar Apr 26 '16 05:04 bvaughn

Thanks @bvaughn :thumbsup:

cosminnicula avatar Apr 27 '16 05:04 cosminnicula