Skip to content

Commit 865be9b

Browse files
authored
refactor!: split getItem into smaller methods, set loading state in updateRow (#9698)
1 parent 2527322 commit 865be9b

File tree

4 files changed

+72
-65
lines changed

4 files changed

+72
-65
lines changed

packages/grid-pro/src/vaadin-grid-pro-inline-editing-mixin.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -543,10 +543,10 @@ export const InlineEditingMixin = (superClass) =>
543543

544544
/**
545545
* @param {!HTMLElement} row
546-
* @param {GridItem} item
547546
* @private
548547
*/
549-
__updateRow(row, item = row._item) {
548+
__updateRow(row) {
549+
const item = this.__getRowItem(row);
550550
if (this.__edited) {
551551
const { cell, model } = this.__edited;
552552
if (cell.parentNode === row && model.item !== item) {

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

Lines changed: 19 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import { microTask, timeOut } from '@vaadin/component-base/src/async.js';
77
import { DataProviderController } from '@vaadin/component-base/src/data-provider-controller/data-provider-controller.js';
88
import { Debouncer } from '@vaadin/component-base/src/debounce.js';
99
import { get } from '@vaadin/component-base/src/path-utils.js';
10-
import { getBodyRowCells, updateCellsPart, updateState } from './vaadin-grid-helpers.js';
1110

1211
/**
1312
* @polymerMixin
@@ -182,46 +181,20 @@ export const DataProviderMixin = (superClass) =>
182181
this.requestContentUpdate();
183182
}
184183

185-
/**
186-
* @param {number} index
187-
* @param {HTMLElement} row
188-
* @protected
189-
*/
190-
_getItem(index, row) {
191-
row.index = index;
192-
193-
const { item } = this._dataProviderController.getFlatIndexContext(index);
194-
if (item) {
195-
this.__updateLoading(row, false);
196-
this.__updateRow(row, item);
197-
if (this._isExpanded(item)) {
198-
this._dataProviderController.ensureFlatIndexHierarchy(index);
199-
}
200-
} else {
201-
this.__updateLoading(row, true);
202-
this._dataProviderController.ensureFlatIndexLoaded(index);
203-
}
184+
/** @private */
185+
__getRowItem(row) {
186+
const { item } = this._dataProviderController.getFlatIndexContext(row.index);
187+
return item;
204188
}
205189

206-
/**
207-
* @param {!HTMLElement} row
208-
* @param {boolean} loading
209-
* @private
210-
*/
211-
__updateLoading(row, loading) {
212-
const cells = getBodyRowCells(row);
213-
214-
// Row state attribute
215-
updateState(row, 'loading', loading);
216-
217-
// Cells part attribute
218-
updateCellsPart(cells, 'loading-row-cell', loading);
190+
/** @private */
191+
__ensureRowItem(row) {
192+
this._dataProviderController.ensureFlatIndexLoaded(row.index);
193+
}
219194

220-
if (loading) {
221-
// Run style generators for the loading row to have custom names cleared
222-
this._generateCellClassNames(row);
223-
this._generateCellPartNames(row);
224-
}
195+
/** @private */
196+
__ensureRowHierarchy(row) {
197+
this._dataProviderController.ensureFlatIndexHierarchy(row.index);
225198
}
226199

227200
/**
@@ -302,7 +275,7 @@ export const DataProviderMixin = (superClass) =>
302275
if (this._flatSize !== this._dataProviderController.flatSize) {
303276
// Schedule an update of all rendered rows by _debouncerApplyCachedData,
304277
// to ensure that all pages associated with the rendered rows are loaded.
305-
this._shouldUpdateAllRenderedRowsAfterPageLoad = true;
278+
this._shouldLoadAllRenderedRowsAfterPageLoad = true;
306279

307280
// TODO: Updating the flat size property can still result in a synchonous virtualizer update
308281
// if the size change requires the virtualizer to increase the amount of physical elements
@@ -312,9 +285,7 @@ export const DataProviderMixin = (superClass) =>
312285
}
313286

314287
// After updating the cache, check if some of the expanded items should have sub-caches loaded
315-
this._getRenderedRows().forEach((row) => {
316-
this._dataProviderController.ensureFlatIndexHierarchy(row.index);
317-
});
288+
this._getRenderedRows().forEach((row) => this.__ensureRowHierarchy(row));
318289

319290
this._hasData = true;
320291
}
@@ -325,13 +296,14 @@ export const DataProviderMixin = (superClass) =>
325296
this._debouncerApplyCachedData = Debouncer.debounce(this._debouncerApplyCachedData, timeOut.after(0), () => {
326297
this._setLoading(false);
327298

328-
const shouldUpdateAllRenderedRowsAfterPageLoad = this._shouldUpdateAllRenderedRowsAfterPageLoad;
329-
this._shouldUpdateAllRenderedRowsAfterPageLoad = false;
299+
const shouldLoadAllRenderedRowsAfterPageLoad = this._shouldLoadAllRenderedRowsAfterPageLoad;
300+
this._shouldLoadAllRenderedRowsAfterPageLoad = false;
330301

331302
this._getRenderedRows().forEach((row) => {
332-
const { item } = this._dataProviderController.getFlatIndexContext(row.index);
333-
if (item || shouldUpdateAllRenderedRowsAfterPageLoad) {
334-
this._getItem(row.index, row);
303+
this.__updateRow(row);
304+
305+
if (shouldLoadAllRenderedRowsAfterPageLoad) {
306+
this.__ensureRowItem(row);
335307
}
336308
});
337309

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

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import {
2828
iterateRowCells,
2929
updateBooleanRowStates,
3030
updateCellsPart,
31+
updateState,
3132
} from './vaadin-grid-helpers.js';
3233
import { KeyboardNavigationMixin } from './vaadin-grid-keyboard-navigation-mixin.js';
3334
import { RowDetailsMixin } from './vaadin-grid-row-details-mixin.js';
@@ -639,10 +640,15 @@ export const GridMixin = (superClass) =>
639640
return;
640641
}
641642

643+
row.index = index;
644+
642645
this._updateRowOrderParts(row, index);
643646

644647
this._a11yUpdateRowRowindex(row, index);
645-
this._getItem(index, row);
648+
649+
this.__ensureRowItem(row);
650+
this.__ensureRowHierarchy(row);
651+
this.__updateRow(row);
646652
}
647653

648654
/** @private */
@@ -741,10 +747,38 @@ export const GridMixin = (superClass) =>
741747

742748
/**
743749
* @param {!HTMLElement} row
744-
* @param {GridItem} item
750+
* @param {boolean} loading
751+
* @private
752+
*/
753+
__updateRowLoading(row, loading) {
754+
const cells = getBodyRowCells(row);
755+
756+
// Row state attribute
757+
updateState(row, 'loading', loading);
758+
759+
// Cells part attribute
760+
updateCellsPart(cells, 'loading-row-cell', loading);
761+
762+
if (loading) {
763+
// Run style generators for the loading row to have custom names cleared
764+
this._generateCellClassNames(row);
765+
this._generateCellPartNames(row);
766+
}
767+
}
768+
769+
/**
770+
* @param {!HTMLElement} row
745771
* @private
746772
*/
747-
__updateRow(row, item = row._item) {
773+
__updateRow(row) {
774+
const item = this.__getRowItem(row);
775+
if (item) {
776+
this.__updateRowLoading(row, false);
777+
} else {
778+
this.__updateRowLoading(row, true);
779+
return;
780+
}
781+
748782
row._item = item;
749783
const model = this.__getRowModel(row);
750784

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

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -606,13 +606,7 @@ describe('data provider', () => {
606606
});
607607

608608
describe('rendering', () => {
609-
function getFirstRowUpdateCount() {
610-
const callsForFirstIndex = grid.__updateRow.getCalls().filter((call) => {
611-
const item = call.args[1];
612-
return item.value === '0';
613-
});
614-
return callsForFirstIndex.length;
615-
}
609+
let firstRowRendererSpy;
616610

617611
beforeEach(async () => {
618612
grid.itemIdPath = 'value';
@@ -624,40 +618,47 @@ describe('data provider', () => {
624618
};
625619
});
626620

627-
cb(pageItems, 3);
621+
cb(pageItems, pageItems.length);
622+
};
623+
grid.querySelector('vaadin-grid-column').renderer = (_root, _grid, { item }) => {
624+
if (item.value === '0') {
625+
firstRowRendererSpy?.();
626+
}
628627
};
629-
sinon.spy(grid, '__updateRow');
628+
630629
await nextFrame();
630+
631+
firstRowRendererSpy = sinon.spy();
631632
});
632633

633634
it('should limit row updates', async () => {
634635
grid.expandedItems = [{ value: '0' }, { value: '1' }, { value: '1-0' }, { value: '2' }];
635636
await nextFrame();
636637
// There are currently two __updateRow calls for a row. The extra one (a direct update request)
637638
// is coming from _expandedItemsChanged.
638-
expect(getFirstRowUpdateCount()).to.equal(2);
639+
expect(firstRowRendererSpy).to.have.callCount(4);
639640
});
640641

641642
it('should limit row updates on a small size', async () => {
642643
grid.size = 3;
643644
grid.expandedItems = [{ value: '0' }, { value: '1' }, { value: '1-0' }, { value: '2' }];
644645
await nextFrame();
645-
expect(getFirstRowUpdateCount()).to.equal(2);
646+
expect(firstRowRendererSpy).to.have.callCount(4);
646647
});
647648

648649
it('should limit row updates on a small page size', async () => {
649650
grid.pageSize = 1;
650651
grid.expandedItems = [{ value: '0' }, { value: '1' }, { value: '1-0' }, { value: '2' }];
651652
await nextFrame();
652653
// Changing page size causes yet an additional update request
653-
expect(getFirstRowUpdateCount()).to.equal(3);
654+
expect(firstRowRendererSpy).to.have.callCount(6);
654655
});
655656

656657
it('should limit row updates on a small page size, reverse property update order', async () => {
657658
grid.expandedItems = [{ value: '0' }, { value: '1' }, { value: '1-0' }, { value: '2' }];
658659
grid.pageSize = 1;
659660
await nextFrame();
660-
expect(getFirstRowUpdateCount()).to.equal(3);
661+
expect(firstRowRendererSpy).to.have.callCount(6);
661662
});
662663
});
663664
});

0 commit comments

Comments
 (0)