Skip to content

Commit

Permalink
refactor: maintain size only within cache instance (#6806)
Browse files Browse the repository at this point in the history
  • Loading branch information
vursen authored Nov 20, 2023
1 parent 3cbb9bc commit 1c38056
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -144,7 +132,7 @@ export class DataProviderController extends EventTarget {
* Clears the cache.
*/
clearCache() {
this.rootCache = this.__createRootCache();
this.rootCache = this.__createRootCache(this.rootCache.size);
}

/**
Expand Down Expand Up @@ -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 */
Expand Down
29 changes: 0 additions & 29 deletions packages/component-base/test/data-provider-controller.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand Down Expand Up @@ -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, {
Expand Down
5 changes: 4 additions & 1 deletion packages/grid/src/vaadin-grid-data-provider-mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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();

Expand Down

0 comments on commit 1c38056

Please sign in to comment.