Skip to content

Commit 3224d4e

Browse files
authored
refactor: avoid unnecessary render cycles in the data provider callback (#5647)
1 parent 63cd432 commit 3224d4e

2 files changed

Lines changed: 83 additions & 13 deletions

File tree

‎packages/grid/src/vaadin-grid-data-provider-mixin.js‎

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -397,26 +397,33 @@ export const DataProviderMixin = (superClass) =>
397397
cache.size = items.length;
398398
}
399399

400-
const currentItems = Array.from(this.$.items.children).map((row) => row._item);
401-
402400
// Populate the cache with new items
403401
items.forEach((item, itemsIndex) => {
404402
const itemIndex = page * this.pageSize + itemsIndex;
405403
cache.items[itemIndex] = item;
406-
if (this._isExpanded(item) && currentItems.indexOf(item) > -1) {
407-
// Force synchronous data request for expanded item sub-cache
408-
cache.ensureSubCacheForScaledIndex(itemIndex);
404+
});
405+
406+
// With the new items added, update the cache size and the grid's effective size
407+
this._cache.updateSize();
408+
this._effectiveSize = this._cache.effectiveSize;
409+
410+
// After updating the cache, check if some of the expanded items should have sub-caches loaded
411+
this._getVisibleRows().forEach((row) => {
412+
const { cache, scaledIndex } = this._cache.getCacheAndIndex(row.index);
413+
const item = cache.items[scaledIndex];
414+
if (item && this._isExpanded(item)) {
415+
cache.ensureSubCacheForScaledIndex(scaledIndex);
409416
}
410417
});
411418

412419
this._hasData = true;
413420

421+
// Remove the pending request
414422
delete cache.pendingRequests[page];
415423

424+
// Schedule a debouncer to update the visible rows
416425
this._debouncerApplyCachedData = Debouncer.debounce(this._debouncerApplyCachedData, timeOut.after(0), () => {
417426
this._setLoading(false);
418-
this._cache.updateSize();
419-
this._effectiveSize = this._cache.effectiveSize;
420427

421428
iterateChildren(this.$.items, (row) => {
422429
if (!row.hidden) {
@@ -430,10 +437,12 @@ export const DataProviderMixin = (superClass) =>
430437
this.__scrollToPendingIndexes();
431438
});
432439

440+
// If the grid is not loading anything, flush the debouncer immediately
433441
if (!this._cache.isLoading()) {
434442
this._debouncerApplyCachedData.flush();
435443
}
436444

445+
// Notify that new data has been received
437446
this.__itemsReceived();
438447
});
439448
}

‎packages/grid/test/data-provider.test.js‎

Lines changed: 67 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import { expect } from '@esm-bundle/chai';
22
import { aTimeout, fixtureSync, nextFrame } from '@vaadin/testing-helpers';
33
import sinon from 'sinon';
4-
import '../vaadin-grid.js';
4+
import '../all-imports.js';
55
import { html, PolymerElement } from '@polymer/polymer/polymer-element.js';
66
import { css, registerStyles } from '@vaadin/vaadin-themable-mixin/vaadin-themable-mixin.js';
77
import {
88
flushGrid,
9+
getBodyCellContent,
910
getCellContent,
1011
getContainerCell,
1112
getFirstCell,
@@ -204,6 +205,10 @@ describe('data provider', () => {
204205
expect(page).to.be.above(-1);
205206
}
206207
});
208+
209+
it('should set itemHasChildren path by default', () => {
210+
expect(grid.itemHasChildrenPath).to.equal('children');
211+
});
207212
});
208213

209214
describe('tree', () => {
@@ -247,16 +252,16 @@ describe('data provider', () => {
247252
}
248253

249254
beforeEach(() => {
255+
const treeColumn = document.createElement('vaadin-grid-tree-column');
256+
treeColumn.path = 'value';
257+
grid.itemHasChildrenPath = 'value';
258+
grid.prepend(treeColumn);
250259
grid.pageSize = 5;
251260
grid.dataProvider = sinon.spy(finiteDataProvider);
252261
generateItemIds = false;
253262
});
254263

255264
describe('first level', () => {
256-
it('should set itemHasChildren path by default', () => {
257-
expect(grid.itemHasChildrenPath).to.equal('children');
258-
});
259-
260265
it('should have collapsed items by default', () => {
261266
for (let i = 0; i < grid._effectiveSize; i++) {
262267
expect(isIndexExpanded(grid, i)).to.be.false;
@@ -455,7 +460,7 @@ describe('data provider', () => {
455460
finiteDataProvider(params, callback);
456461
// Only the root-level items (10) should be rendered at this point even though the
457462
// data request for the first expanded item resolved
458-
expect(grid._effectiveSize).to.equal(10);
463+
expect(getBodyCellContent(grid, 1, 0).textContent).to.equal('foo1');
459464
done();
460465
}, 0);
461466
}
@@ -557,6 +562,62 @@ describe('data provider', () => {
557562
expect(grid._cache.getItemForIndex(18)).to.deep.equal({ level: 0, value: 'foo8' });
558563
});
559564
});
565+
566+
describe('rendering', () => {
567+
function getFirstRowUpdateCount() {
568+
const callsForFirstIndex = grid._updateItem.getCalls().filter((call) => {
569+
const item = call.args[1];
570+
return item.value === '0';
571+
});
572+
return callsForFirstIndex.length;
573+
}
574+
575+
beforeEach(async () => {
576+
grid.itemIdPath = 'value';
577+
grid.dataProvider = ({ parentItem, page, pageSize }, cb) => {
578+
const pageItems = [...Array(3)].map((_, i) => {
579+
const indexInLevel = page * pageSize + i;
580+
return {
581+
value: `${parentItem ? `${parentItem.value}-` : ''}${indexInLevel}`,
582+
};
583+
});
584+
585+
cb(pageItems, 3);
586+
};
587+
sinon.spy(grid, '_updateItem');
588+
await nextFrame();
589+
});
590+
591+
it('should limit row updates', async () => {
592+
grid.expandedItems = [{ value: '0' }, { value: '1' }, { value: '1-0' }, { value: '2' }];
593+
await nextFrame();
594+
// There are currently two _updateItem calls for a row. The extra one (a direct update request)
595+
// is coming from _expandedItemsChanged.
596+
expect(getFirstRowUpdateCount()).to.equal(2);
597+
});
598+
599+
it('should limit row updates on a small size', async () => {
600+
grid.size = 3;
601+
grid.expandedItems = [{ value: '0' }, { value: '1' }, { value: '1-0' }, { value: '2' }];
602+
await nextFrame();
603+
expect(getFirstRowUpdateCount()).to.equal(2);
604+
});
605+
606+
it('should limit row updates on a small page size', async () => {
607+
grid.pageSize = 1;
608+
grid.expandedItems = [{ value: '0' }, { value: '1' }, { value: '1-0' }, { value: '2' }];
609+
await nextFrame();
610+
// Changing page size causes yet an additional update request
611+
expect(getFirstRowUpdateCount()).to.equal(3);
612+
});
613+
614+
it('should limit row updates on a small page size, reverse property update order', async () => {
615+
grid.expandedItems = [{ value: '0' }, { value: '1' }, { value: '1-0' }, { value: '2' }];
616+
grid.pageSize = 1;
617+
await nextFrame();
618+
expect(getFirstRowUpdateCount()).to.equal(3);
619+
});
620+
});
560621
});
561622
});
562623

0 commit comments

Comments
 (0)