Skip to content

Commit 0551832

Browse files
authored
fix: cancel request debouncer if all data has been received (#7840)
1 parent e312f1f commit 0551832

File tree

3 files changed

+30
-12
lines changed

3 files changed

+30
-12
lines changed

vaadin-grid-flow-parent/vaadin-grid-flow-integration-tests/src/test/java/com/vaadin/flow/component/grid/it/GridTooltipIT.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ public void columnTooltipOverridesGridTooltip() {
9292
scrollToElement(grid);
9393
// set grid tooltip
9494
clickElementWithJs("set-grid-tooltip-button");
95+
flushScrolling(grid);
9596
// check column has grid's tooltip
9697
showTooltip(grid.getCell(1, 1));
9798
Assert.assertEquals("Grid's tooltip! Jill", getActiveTooltipText());

vaadin-grid-flow-parent/vaadin-grid-flow-integration-tests/test/grid-connector.test.ts

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,15 @@ describe('grid connector', () => {
4242
expect(getBodyCellText(grid, 0, 0)).to.equal('foo');
4343
});
4444

45+
it('should cancel debounced requests if all data has already been received', async () => {
46+
setRootItems(grid.$connector, [{ key: '0', name: 'foo' }]);
47+
await nextFrame();
48+
grid.$connector.reset();
49+
setRootItems(grid.$connector, [{ key: '0', name: 'bar' }]);
50+
await aTimeout(GRID_CONNECTOR_ROOT_REQUEST_DELAY);
51+
expect(grid.$server.setViewportRange).to.be.not.called;
52+
});
53+
4554
describe('empty grid', () => {
4655
it('should not have loading state when refreshing grid', async () => {
4756
setRootItems(grid.$connector, []);
@@ -73,7 +82,7 @@ describe('grid connector', () => {
7382

7483
beforeEach(async () => {
7584
// Use a smaller page size for testing
76-
grid.pageSize = 25;
85+
grid.pageSize = 10;
7786
grid.$connector.reset();
7887

7988
// Add all root items
@@ -84,23 +93,23 @@ describe('grid connector', () => {
8493
describe('last requested range is in viewport', () => {
8594
beforeEach(async () => {
8695
// Request a range of items at the top
87-
clear(grid.$connector, 0, 50);
96+
clear(grid.$connector, 0, 30);
8897
await aTimeout(GRID_CONNECTOR_ROOT_REQUEST_DELAY);
89-
expect(grid.$server.setViewportRange).to.be.calledOnceWith(0, 50);
90-
setRootItems(grid.$connector, items, 0, 50);
98+
expect(grid.$server.setViewportRange).to.be.calledOnceWith(0, 30);
99+
setRootItems(grid.$connector, items, 0, 30);
91100
grid.$server.setViewportRange.resetHistory();
92101
});
93102

94103
it('should request new items after incomplete confirm', async () => {
95104
// Clear the items again
96-
clear(grid.$connector, 0, 100);
105+
clear(grid.$connector, 0, 30);
97106

98107
// Add the first page items back before the request timeout (partial/incomplete preload)
99108
setRootItems(grid.$connector, items, 0, grid.pageSize);
100109
await aTimeout(GRID_CONNECTOR_ROOT_REQUEST_DELAY);
101110

102111
// Grid should have requested for the missing items
103-
expect(grid.$server.setViewportRange).to.be.calledOnceWith(0, 50);
112+
expect(grid.$server.setViewportRange).to.be.calledOnceWith(0, 30);
104113
});
105114

106115
it('should not request for new items after complete confirm', async () => {
@@ -122,8 +131,8 @@ describe('grid connector', () => {
122131
clear(grid.$connector, 50, 50);
123132
grid.scrollToIndex(50);
124133
await aTimeout(GRID_CONNECTOR_ROOT_REQUEST_DELAY);
125-
expect(grid.$server.setViewportRange).to.have.been.calledOnceWith(25, 75);
126-
setRootItems(grid.$connector, items, 25, 75);
134+
expect(grid.$server.setViewportRange).to.have.been.calledOnceWith(30, 50);
135+
setRootItems(grid.$connector, items, 30, 50);
127136
grid.$server.setViewportRange.resetHistory();
128137
});
129138

@@ -133,15 +142,15 @@ describe('grid connector', () => {
133142
// - Clear last requested range partially
134143
// - Preload first two pages so that grid doesn't need to request a new range yet
135144
grid.scrollToIndex(0);
136-
clear(grid.$connector, 50, grid.pageSize);
137-
setRootItems(grid.$connector, items, 0, 50);
145+
clear(grid.$connector, 40, grid.pageSize);
146+
setRootItems(grid.$connector, items, 0, 30);
138147
await aTimeout(GRID_CONNECTOR_ROOT_REQUEST_DELAY);
139148
expect(grid.$server.setViewportRange).to.not.have.been.called;
140149

141150
// Scroll down again, should reload the range because part of it was cleared
142151
grid.scrollToIndex(50);
143152
await aTimeout(GRID_CONNECTOR_ROOT_REQUEST_DELAY);
144-
expect(grid.$server.setViewportRange).to.have.been.calledOnceWith(25, 75);
153+
expect(grid.$server.setViewportRange).to.have.been.calledOnceWith(30, 50);
145154
});
146155

147156
it('should not request for items if data outside of the last range was cleared', async () => {
@@ -150,7 +159,7 @@ describe('grid connector', () => {
150159
// - Clear data outside the requested range
151160
// - Preload first two pages so that grid doesn't need to request a new range yet
152161
grid.scrollToIndex(0);
153-
clear(grid.$connector, 75, grid.pageSize);
162+
clear(grid.$connector, 70, grid.pageSize);
154163
grid.$connector.confirm(-1);
155164
await aTimeout(GRID_CONNECTOR_ROOT_REQUEST_DELAY);
156165
expect(grid.$server.setViewportRange).to.not.have.been.called;

vaadin-grid-flow-parent/vaadin-grid-flow/src/main/resources/META-INF/resources/frontend/gridConnector.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -846,6 +846,14 @@ window.Vaadin.Flow.gridConnector.initLazy = (grid) => {
846846
}
847847
});
848848

849+
// If all pending requests have already been resolved (which can happen
850+
// for example if the server sent preloaded data while the grid had
851+
// already made its own requests), cancel the request debouncer to
852+
// prevent further unnecessary calls.
853+
if (Object.keys(pendingRequests).length === 0) {
854+
rootRequestDebouncer?.cancel();
855+
}
856+
849857
// Sanitize last requested range for the root level
850858
sanitizeLastRequestedRange();
851859
// Clear current update state

0 commit comments

Comments
 (0)