Skip to content

Commit

Permalink
fix: prevent unnecessary page fetches when scrolling to index
Browse files Browse the repository at this point in the history
  • Loading branch information
sissbruecker committed Dec 20, 2021
1 parent bec0993 commit 5a7d5b7
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/vaadin-grid-scroller.html
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,13 @@
this._scrollingToIndex = true;
index = Math.min(Math.max(index, 0), this._effectiveSize - 1);
this.$.table.scrollTop = index / this._effectiveSize * (this.$.table.scrollHeight - this.$.table.offsetHeight);

// We need to run the iron-list scroll handler here in order to recalculate the scaling from effective size to
// virtual size after changing the scroll position. However we don't want to trigger updates to the items / rows
// in this step, which could result in data provider requests for the previous viewport
this._preventItemUpdates = true;
this._scrollHandler();
this._preventItemUpdates = false;

if (this._accessIronListAPI(() => this._maxScrollTop) && this._virtualCount < this._effectiveSize) {
this._adjustVirtualIndexOffset(1000000);
Expand Down Expand Up @@ -259,6 +265,10 @@
* @protected
*/
_assignModels(itemSet) {
// Skip here if internal flag for preventing item updates is set
if (this._preventItemUpdates) {
return;
}
this._iterateItems((pidx, vidx) => {
const el = this._physicalItems[pidx];
this._toggleAttribute('hidden', vidx >= this._effectiveSize, el);
Expand Down
18 changes: 18 additions & 0 deletions test/scroll-to-index.html
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,24 @@
};
});

it('should not request to load page for previous viewport when clearing items from cache', () => {
grid.dataProvider = sinon.spy(infiniteDataProvider);
// Clear initial data provider request from spy
grid.dataProvider.reset();
// Remove item from current viewport from cache
// This simulates Flow component behavior, which clears pages for the
// previous viewport after loading pages for the new viewport
delete grid._cache.items[0];

// Scroll to new viewport
grid.scrollToIndex(1000);

// Verify data provider was not called with page for previous viewport (page 0)
grid.dataProvider.args.forEach(args => {
expect(args[0].page).not.to.eql(0);
});
});

});

describe('Added item', () => {
Expand Down

0 comments on commit 5a7d5b7

Please # to comment.