Skip to content

Commit 36d7d2b

Browse files
authored
fix: remove exceeding items from cache when size is decreased (#10222)
1 parent 758a53e commit 36d7d2b

File tree

3 files changed

+22
-10
lines changed

3 files changed

+22
-10
lines changed

packages/component-base/src/data-provider-controller/cache.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,10 @@ export class Cache {
152152
}
153153
}
154154

155+
if (this.items.length > size) {
156+
this.items.length = size || 0;
157+
}
158+
155159
Object.keys(this.pendingRequests).forEach((page) => {
156160
const startIndex = parseInt(page) * this.pageSize;
157161
if (startIndex >= this.size || 0) {

packages/component-base/test/data-provider-controller-cache.test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,14 @@ describe('DataProviderController - Cache', () => {
231231
expect(cache.pendingRequests[8]).to.be.not.undefined;
232232
expect(cache.pendingRequests[9]).to.be.undefined;
233233
});
234+
235+
it('should remove exceeding items when decreasing size', () => {
236+
cache.setPage(0, ['Item 0', 'Item 1']);
237+
cache.setPage(1, ['Item 2', 'Item 3']);
238+
cache.size = 1;
239+
expect(cache.items).to.have.lengthOf(1);
240+
expect(cache.items[0]).to.equal('Item 0');
241+
});
234242
});
235243

236244
describe('size with placeholder', () => {

packages/grid/test/basic.test.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -248,42 +248,42 @@ describe('basic features', () => {
248248
return column.renderer.getCalls().filter((call) => call.args[2].index === 0).length;
249249
}
250250

251-
it('should have rendered the first cell once', () => {
251+
it('should render the first cell once during initialization', () => {
252252
expect(getFirstCellRenderCount()).to.equal(1);
253253
});
254254

255-
it('should re-render the cell when last row enters the viewport on resize', () => {
255+
it('should re-render the first cell when last row enters the viewport on resize', () => {
256256
column.renderer.resetHistory();
257257
grid.size = 1;
258-
expect(getFirstCellRenderCount()).to.equal(1);
258+
expect(getFirstCellRenderCount()).to.equal(1); // once on size change
259259
});
260260

261-
it('should re-render the cell when last row leaves the viewport on resize', () => {
261+
it('should re-render the first cell when last row leaves the viewport on resize', () => {
262262
grid.size = 1;
263263
column.renderer.resetHistory();
264264
grid.size = 1000;
265-
expect(getFirstCellRenderCount()).to.equal(1);
265+
expect(getFirstCellRenderCount()).to.equal(2); // once on size change and once when data provider responds
266266
});
267267

268-
it('should not re-render the cell when last row change happens outside the viewport', () => {
268+
it('should not re-render the first cell when last row change happens outside the viewport', () => {
269269
column.renderer.resetHistory();
270270
grid.size = 100;
271271
grid.size = 200;
272272
expect(getFirstCellRenderCount()).to.equal(0);
273273
});
274274

275-
it('should not re-render the cell when last row change happens on other visible rows', () => {
275+
it('should re-render the first cell when last row change happens inside the viewport', () => {
276276
column.renderer.resetHistory();
277277
grid.size = 2;
278278
grid.size = 3;
279-
expect(getFirstCellRenderCount()).to.equal(0);
279+
expect(getFirstCellRenderCount()).to.equal(1); // once when data provider responds
280280
});
281281

282-
it('should have rendered the first cell once on resize from 0', () => {
282+
it('should re-render the first cell on resize from 0', () => {
283283
column.renderer.resetHistory();
284284
grid.size = 0;
285285
grid.size = 1;
286-
expect(getFirstCellRenderCount()).to.equal(1);
286+
expect(getFirstCellRenderCount()).to.equal(2); // once on size change and once when data provider responds
287287
});
288288

289289
it('should render all items with varying row heights when all rows visible', async () => {

0 commit comments

Comments
 (0)