Skip to content

Commit 12a40b1

Browse files
authored
fix: don't update element for the same index more than once (#2910)
1 parent 9c03f4a commit 12a40b1

6 files changed

Lines changed: 34 additions & 16 deletions

File tree

‎packages/combo-box/src/vaadin-combo-box-scroller.js‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ export class ComboBoxScroller extends PolymerElement {
8181
* calling `scrollIntoView` does not have any effect.
8282
*/
8383
opened: {
84-
type: Boolean
84+
type: Boolean,
85+
observer: '__openedChanged'
8586
},
8687

8788
/**
@@ -134,6 +135,12 @@ export class ComboBoxScroller extends PolymerElement {
134135
this.__boundOnItemClick = this.__onItemClick.bind(this);
135136
}
136137

138+
__openedChanged(opened) {
139+
if (this.__virtualizer && opened) {
140+
this.__virtualizer.update();
141+
}
142+
}
143+
137144
/** @protected */
138145
ready() {
139146
super.ready();
@@ -226,6 +233,7 @@ export class ComboBoxScroller extends PolymerElement {
226233
this.__virtualizer.flush();
227234
// Ensure the total count of items is properly announced.
228235
this.setAttribute('aria-setsize', items.length);
236+
this.__virtualizer.update();
229237
}
230238
}
231239

@@ -239,6 +247,7 @@ export class ComboBoxScroller extends PolymerElement {
239247
/** @private */
240248
__focusedIndexChanged(index) {
241249
if (this.__virtualizer && index >= 0) {
250+
this.__virtualizer.update();
242251
this.scrollIntoView(index);
243252
}
244253
}

‎packages/combo-box/test/lazy-loading.test.js‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -734,11 +734,13 @@ describe('lazy loading', () => {
734734
expect(selectedRenderedItemElements[0].item).to.eql({ id: 0, value: 'value 0', label: 'label 0' });
735735
});
736736

737-
it('should select value matching selectedItem when items are loaded', () => {
737+
it('should select value matching selectedItem when items are loaded', async () => {
738738
comboBox.opened = true;
739739
comboBox.selectedItem = { id: 0, value: 'value 0', label: 'label 0' };
740740
expect(comboBox.value).to.equal('value 0');
741741
flushComboBox(comboBox);
742+
// Wait for the timeout in __loadingChanged to finish
743+
await aTimeout(0);
742744
const selectedRenderedItemElements = getAllItems(comboBox).filter((itemEl) => itemEl.selected);
743745
// doesn't work when run on SauceLabs, work locally
744746
// expect(selectedRenderedItemElements).to.have.lengthOf(1);

‎packages/component-base/src/virtualizer-iron-list-adapter.js‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,19 +118,20 @@ export class IronListAdapter {
118118
update(startIndex = 0, endIndex = this.size - 1) {
119119
this.__getVisibleElements().forEach((el) => {
120120
if (el.__virtualIndex >= startIndex && el.__virtualIndex <= endIndex) {
121-
this.__updateElement(el, el.__virtualIndex);
121+
this.__updateElement(el, el.__virtualIndex, true);
122122
}
123123
});
124124
}
125125

126-
__updateElement(el, index) {
126+
__updateElement(el, index, forceSameIndexUpdates) {
127127
// Clean up temporary min height
128128
if (el.style.minHeight) {
129129
el.style.minHeight = '';
130130
}
131131

132-
if (!this.__preventElementUpdates) {
132+
if (!this.__preventElementUpdates && (el.__lastUpdatedIndex !== index || forceSameIndexUpdates)) {
133133
this.updateElement(el, index);
134+
el.__lastUpdatedIndex = index;
134135
}
135136

136137
if (el.offsetHeight === 0) {

‎packages/component-base/test/virtualizer.test.js‎

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -172,22 +172,19 @@ describe('virtualizer', () => {
172172
expect(item.getBoundingClientRect().top).to.equal(scrollTarget.getBoundingClientRect().top - 10);
173173
});
174174

175-
it('should not request a different set of items on size increase', () => {
175+
it('should not request item updates on size increase', () => {
176176
const updateElement = sinon.spy((el, index) => (el.textContent = `item-${index}`));
177177
init({ size: 100, updateElement });
178178

179179
// Scroll halfway down the list
180180
updateElement.resetHistory();
181181
virtualizer.scrollToIndex(50);
182-
const updatedIndexes = updateElement.getCalls().map((call) => call.args[1]);
183182

184183
// Increase the size so it shouldn't affect the current viewport items
185184
updateElement.resetHistory();
186185
virtualizer.size = 200;
187-
const postResizeUpdatedIndexes = updateElement.getCalls().map((call) => call.args[1]);
188186

189-
expect(postResizeUpdatedIndexes).to.eql(updatedIndexes);
190-
expect(postResizeUpdatedIndexes).not.to.include(0);
187+
expect(updateElement.called).to.be.false;
191188
});
192189

193190
it('should request a different set of items on size decrease', () => {
@@ -208,6 +205,17 @@ describe('virtualizer', () => {
208205
expect(postResizeUpdatedIndexes).to.include(0);
209206
});
210207

208+
it('should request an index only once', async () => {
209+
const updateElement = sinon.spy((el, index) => (el.textContent = `item-${index}`));
210+
init({ size: 100, updateElement });
211+
212+
// Wait for a possible resize observer flush
213+
await aTimeout(100);
214+
215+
const firstIndexUpdatesCount = updateElement.getCalls().filter((call) => call.args[1] === 0).length;
216+
expect(firstIndexUpdatesCount).to.equal(1);
217+
});
218+
211219
it('should have physical items once visible', async () => {
212220
init({ size: 0 });
213221
// Wait for possibly active resize observers to flush

‎packages/grid/src/vaadin-grid.js‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -884,7 +884,6 @@ class Grid extends ElementMixin(
884884

885885
this._a11yUpdateRowLevel(row, model.level);
886886
this._a11yUpdateRowSelected(row, model.selected);
887-
this._a11yUpdateRowExpanded(row, model.expanded);
888887
this._a11yUpdateRowDetailsOpened(row, model.detailsOpened);
889888

890889
row.toggleAttribute('expanded', model.expanded);
@@ -902,6 +901,8 @@ class Grid extends ElementMixin(
902901
});
903902

904903
this._updateDetailsCellHeight(row);
904+
905+
this._a11yUpdateRowExpanded(row, model.expanded);
905906
}
906907

907908
/** @private */

‎packages/virtual-list/src/vaadin-virtual-list.js‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -153,11 +153,8 @@ class VirtualList extends ElementMixin(ThemableMixin(PolymerElement)) {
153153
const hasRenderedItems = this.childElementCount > 0;
154154

155155
if ((renderer || hasRenderedItems) && virtualizer) {
156-
if (items.length === virtualizer.size) {
157-
virtualizer.update();
158-
} else {
159-
virtualizer.size = items.length;
160-
}
156+
virtualizer.size = items.length;
157+
virtualizer.update();
161158
}
162159
}
163160

0 commit comments

Comments
 (0)