Skip to content

Commit 5a8b633

Browse files
authored
fix: properly clear pending requests on grid item collapse (#5491)
1 parent 9dbc3ff commit 5a8b633

3 files changed

Lines changed: 56 additions & 13 deletions

File tree

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

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,17 @@ import {
77
getBodyCellText,
88
getBodyCellContent,
99
GRID_CONNECTOR_PARENT_REQUEST_DELAY,
10-
getHeaderCellContent
10+
getHeaderCellContent,
11+
collapseItems
1112
} from './shared.js';
1213
import sinon from 'sinon';
1314
import type { FlowGrid } from './shared.js';
1415
import type { GridColumn } from '@vaadin/grid';
1516

17+
function isGridInLoadingState(grid: FlowGrid) {
18+
return (grid as any)._cache.isLoading();
19+
}
20+
1621
describe('grid connector - tree', () => {
1722
let grid: FlowGrid;
1823

@@ -200,4 +205,40 @@ describe('grid connector - tree', () => {
200205
// root item is outside the DOM so no requests should have been sent
201206
expect(grid.$server.setParentRequestedRanges).to.not.be.called;
202207
});
208+
209+
it('should not request items for expanded and immediately collapsed parent', async () => {
210+
// Add expandable root item
211+
const rootItem = { key: '0', name: 'foo', children: true };
212+
setRootItems(grid.$connector, [rootItem]);
213+
214+
// Expand the root item
215+
expandItems(grid.$connector, [rootItem]);
216+
expect(isGridInLoadingState(grid)).to.be.true;
217+
// Immediately collapse the root item
218+
collapseItems(grid.$connector, [rootItem]);
219+
await nextFrame();
220+
221+
await aTimeout(GRID_CONNECTOR_PARENT_REQUEST_DELAY);
222+
223+
expect(isGridInLoadingState(grid)).to.be.false;
224+
expect(grid.$server.setParentRequestedRanges).to.not.be.called;
225+
});
226+
227+
it('should not request items for expanded and shortly collapsed parent', async () => {
228+
// Add expandable root item
229+
const rootItem = { key: '0', name: 'foo', children: true };
230+
setRootItems(grid.$connector, [rootItem]);
231+
232+
// Expand the root item
233+
expandItems(grid.$connector, [rootItem]);
234+
expect(isGridInLoadingState(grid)).to.be.true;
235+
// Collapse the root item after a short delay
236+
await nextFrame();
237+
collapseItems(grid.$connector, [rootItem]);
238+
239+
await aTimeout(GRID_CONNECTOR_PARENT_REQUEST_DELAY);
240+
241+
expect(isGridInLoadingState(grid)).to.be.false;
242+
expect(grid.$server.setParentRequestedRanges).to.not.be.called;
243+
});
203244
});

‎vaadin-grid-flow-parent/vaadin-grid-flow-integration-tests/test/shared.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export type GridConnector = {
1717
confirmParent: (index: number, parentKey: string, levelSize: number) => void;
1818
setSelectionMode: (mode: 'SINGLE' | 'NONE' | 'MULTI') => void;
1919
expandItems: (items: Item[]) => void;
20+
collapseItems: (items: Item[]) => void;
2021
ensureHierarchy: () => void;
2122
reset: () => void;
2223
doSelection: (items: Item[] | [null], userOriginated: boolean) => void;
@@ -174,6 +175,13 @@ export function expandItems(gridConnector: GridConnector, items: Item[]): void {
174175
gridConnector.expandItems(items);
175176
}
176177

178+
/**
179+
* Collapse the given items.
180+
*/
181+
export function collapseItems(gridConnector: GridConnector, items: Item[]): void {
182+
gridConnector.collapseItems(items);
183+
}
184+
177185
/**
178186
* Clears the given range of the given parent's children.
179187
*/

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

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -803,19 +803,13 @@ import { isFocusable } from '@vaadin/grid/src/vaadin-grid-active-item-mixin.js';
803803

804804
grid.$connector.removeFromQueue = tryCatchWrapper(function (item) {
805805
let itemId = grid.getItemId(item);
806+
// The treePageCallbacks for the itemId are about to be discarded ->
807+
// Resolve the callbacks with an empty array to not leave grid in loading state
808+
Object.values(treePageCallbacks[itemId] || {}).forEach((callback) => callback([]));
809+
806810
delete treePageCallbacks[itemId];
807-
grid.$connector.removeFromArray(ensureSubCacheQueue, (item) => item.itemkey === itemId);
808-
grid.$connector.removeFromArray(parentRequestQueue, (item) => item.parentKey === itemId);
809-
});
810-
811-
grid.$connector.removeFromArray = tryCatchWrapper(function (array, removeTest) {
812-
if (array.length) {
813-
for (let index = array.length - 1; index--; ) {
814-
if (removeTest(array[index])) {
815-
array.splice(index, 1);
816-
}
817-
}
818-
}
811+
ensureSubCacheQueue = ensureSubCacheQueue.filter((item) => item.itemkey !== itemId);
812+
parentRequestQueue = parentRequestQueue.filter((item) => item.parentKey !== itemId);
819813
});
820814

821815
grid.$connector.confirmParent = tryCatchWrapper(function (id, parentKey, levelSize) {

0 commit comments

Comments
 (0)