From 1c38056ba50e8708861c4099faadbd21152282f7 Mon Sep 17 00:00:00 2001 From: Sergey Vinogradov Date: Mon, 20 Nov 2023 09:10:19 +0200 Subject: [PATCH] refactor: maintain size only within cache instance (#6806) --- .../data-provider-controller.js | 20 +++---------- .../test/data-provider-controller.test.js | 29 ------------------- .../src/vaadin-grid-data-provider-mixin.js | 5 +++- 3 files changed, 8 insertions(+), 46 deletions(-) diff --git a/packages/component-base/src/data-provider-controller/data-provider-controller.js b/packages/component-base/src/data-provider-controller/data-provider-controller.js index 1e3d16689a..186a979665 100644 --- a/packages/component-base/src/data-provider-controller/data-provider-controller.js +++ b/packages/component-base/src/data-provider-controller/data-provider-controller.js @@ -68,13 +68,12 @@ export class DataProviderController extends EventTarget { constructor(host, { size, pageSize, isExpanded, getItemId, dataProvider, dataProviderParams }) { super(); this.host = host; - this.size = size; this.pageSize = pageSize; this.getItemId = getItemId; this.isExpanded = isExpanded; this.dataProvider = dataProvider; this.dataProviderParams = dataProviderParams; - this.rootCache = this.__createRootCache(); + this.rootCache = this.__createRootCache(size); } /** @@ -102,17 +101,6 @@ export class DataProviderController extends EventTarget { return this.rootCache.isLoading; } - /** - * Sets the size for the root cache and recalculates the flattened size. - * - * @param {number} size - */ - setSize(size) { - this.size = size; - this.rootCache.size = size; - this.recalculateFlatSize(); - } - /** * Sets the page size and clears the cache. * @@ -144,7 +132,7 @@ export class DataProviderController extends EventTarget { * Clears the cache. */ clearCache() { - this.rootCache = this.__createRootCache(); + this.rootCache = this.__createRootCache(this.rootCache.size); } /** @@ -228,8 +216,8 @@ export class DataProviderController extends EventTarget { } /** @private */ - __createRootCache() { - return new Cache(this.__cacheContext, this.pageSize, this.size); + __createRootCache(size) { + return new Cache(this.__cacheContext, this.pageSize, size); } /** @private */ diff --git a/packages/component-base/test/data-provider-controller.test.js b/packages/component-base/test/data-provider-controller.test.js index 1f5fc3bf2c..a1d2386301 100644 --- a/packages/component-base/test/data-provider-controller.test.js +++ b/packages/component-base/test/data-provider-controller.test.js @@ -82,10 +82,6 @@ describe('DataProviderController', () => { }); }); - it('should have size', () => { - expect(controller.size).to.equal(500); - }); - it('should have rootCache size', () => { expect(controller.rootCache.size).to.equal(500); }); @@ -144,31 +140,6 @@ describe('DataProviderController', () => { }); }); - describe('changing size', () => { - beforeEach(() => { - controller = new DataProviderController(host, { - pageSize: 50, - isExpanded, - dataProvider: (_params, callback) => callback([], 0), - }); - }); - - it('should set the new size', () => { - controller.setSize(100); - expect(controller.size).to.equal(100); - }); - - it('should set the new rootCache size', () => { - controller.setSize(100); - expect(controller.rootCache.size).to.equal(100); - }); - - it('should recalculate flatSize', () => { - controller.setSize(100); - expect(controller.flatSize).to.equal(100); - }); - }); - describe('changing pageSize', () => { beforeEach(() => { controller = new DataProviderController(host, { diff --git a/packages/grid/src/vaadin-grid-data-provider-mixin.js b/packages/grid/src/vaadin-grid-data-provider-mixin.js index 91d398fa12..ca2942daaf 100644 --- a/packages/grid/src/vaadin-grid-data-provider-mixin.js +++ b/packages/grid/src/vaadin-grid-data-provider-mixin.js @@ -186,7 +186,8 @@ export const DataProviderMixin = (superClass) => /** @private */ _sizeChanged(size) { - this._dataProviderController.setSize(size); + this._dataProviderController.rootCache.size = size; + this._dataProviderController.recalculateFlatSize(); this._flatSize = this._dataProviderController.flatSize; } @@ -363,6 +364,8 @@ export const DataProviderMixin = (superClass) => */ clearCache() { this._dataProviderController.clearCache(); + this._dataProviderController.rootCache.size = this.size; + this._dataProviderController.recalculateFlatSize(); this._hasData = false; this.__updateVisibleRows();