Skip to content

Commit 4733654

Browse files
fix: limit the requested pages to a given threshold (#2801)
Co-authored-by: Tomi Virkki <virkki@vaadin.com>
1 parent 6d30553 commit 4733654

2 files changed

Lines changed: 48 additions & 1 deletion

File tree

‎packages/virtual-list/src/virtualizer-iron-list-adapter.js‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,9 @@ export class IronListAdapter {
129129
el.style.minHeight = '';
130130
}
131131

132-
this.updateElement(el, index);
132+
if (!this.__preventElementUpdates) {
133+
this.updateElement(el, index);
134+
}
133135

134136
if (el.offsetHeight === 0) {
135137
// If the elements have 0 height after update (for example due to lazy rendering),
@@ -150,6 +152,9 @@ export class IronListAdapter {
150152
return;
151153
}
152154

155+
// Prevent element update while the scroll position is being restored
156+
this.__preventElementUpdates = true;
157+
153158
// Record the scroll position before changing the size
154159
let fvi; // first visible index
155160
let fviOffsetBefore; // scroll offset of the first visible index
@@ -184,6 +189,11 @@ export class IronListAdapter {
184189
if (!this.elementsContainer.children.length) {
185190
requestAnimationFrame(() => this._resizeHandler());
186191
}
192+
193+
this.__preventElementUpdates = false;
194+
// Schedule and flush a resize handler
195+
this._resizeHandler();
196+
flush();
187197
}
188198

189199
get size() {

‎packages/virtual-list/test/virtualizer.test.js‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { expect } from '@esm-bundle/chai';
22
import { aTimeout, fixtureSync, nextFrame, oneEvent } from '@vaadin/testing-helpers';
3+
import sinon from 'sinon';
34
import { Virtualizer } from '../src/virtualizer.js';
45

56
describe('virtualizer', () => {
@@ -171,6 +172,42 @@ describe('virtualizer', () => {
171172
expect(item.getBoundingClientRect().top).to.equal(scrollTarget.getBoundingClientRect().top - 10);
172173
});
173174

175+
it('should not request a different set of items on size increase', () => {
176+
const updateElement = sinon.spy((el, index) => (el.textContent = `item-${index}`));
177+
init({ size: 100, updateElement });
178+
179+
// Scroll halfway down the list
180+
updateElement.resetHistory();
181+
virtualizer.scrollToIndex(50);
182+
const updatedIndexes = updateElement.getCalls().map((call) => call.args[1]);
183+
184+
// Increase the size so it shouldn't affect the current viewport items
185+
updateElement.resetHistory();
186+
virtualizer.size = 200;
187+
const postResizeUpdatedIndexes = updateElement.getCalls().map((call) => call.args[1]);
188+
189+
expect(postResizeUpdatedIndexes).to.eql(updatedIndexes);
190+
expect(postResizeUpdatedIndexes).not.to.include(0);
191+
});
192+
193+
it('should request a different set of items on size decrease', () => {
194+
const updateElement = sinon.spy((el, index) => (el.textContent = `item-${index}`));
195+
init({ size: 100, updateElement });
196+
197+
// Scroll halfway down the list
198+
updateElement.resetHistory();
199+
virtualizer.scrollToIndex(50);
200+
const updatedIndexes = updateElement.getCalls().map((call) => call.args[1]);
201+
202+
// Decrease the size so that we end up at the top of the list
203+
updateElement.resetHistory();
204+
virtualizer.size = 10;
205+
const postResizeUpdatedIndexes = updateElement.getCalls().map((call) => call.args[1]);
206+
207+
expect(postResizeUpdatedIndexes).not.to.include.members(updatedIndexes);
208+
expect(postResizeUpdatedIndexes).to.include(0);
209+
});
210+
174211
it('should have physical items once visible', async () => {
175212
init({ size: 0 });
176213
// Wait for possibly active resize observers to flush

0 commit comments

Comments
 (0)