Skip to content

refactor: extract grid data provider functionality into a controller - #5961

Merged
vursen merged 5 commits into
mainfrom
feat/data-provider-controller
Sep 22, 2023
Merged

vursen merged 5 commits into
mainfrom
feat/data-provider-controller

Conversation

@vursen

@vursen vursen commented Jun 9, 2023 •

Copy link
Copy Markdown
Contributor

Description

The PR extracts the grid data provider functionality into a controller in order to separate concerns, thereby reducing cognitive load, making testing easier, and potentially allowing reusing it in other components such as combo-box. In the future, the controller can provide some additional methods or events to be used on the Flow connector side without having to make them public web component API.

TODO:

  • Fix existing component unit tests
  • Update the Grid connector to make it work with the controller (flow-components#5239).
  • Add basic unit tests for the controller (can be done separately?)
  • TypeScript declarations
  • JSDoc descriptions

Type of change

  • Refactor

@vursen vursen changed the title refactor: extract grid data provider logic into a controller (WIP) refactor: extract grid data provider functionality into a controller (WIP) Jun 9, 2023
Comment thread packages/grid/src/vaadin-grid.js Outdated
Comment on lines +296 to +304
ArrayDataProviderMixin(
DataProviderMixin(

@vursen vursen Jun 9, 2023 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

explanation: Both DataProviderMixin and ArrayDataProviderMixin have an observer that is triggered when a new grid.dataProvider is set.

This change modifies the order in which observers are run so that the dataProvider observer of DataProviderMixin runs before the one of ArrayDataProviderMixin. This ensures that the controller will register the new data provider callback before ArrayDataProviderMixin attempts to request data here:

} else if (this._arrayDataProvider.__items === items) {
// The items array was modified
this.clearCache();
this.size = this._effectiveSize;
} else {

@vursen
vursen force-pushed the feat/data-provider-controller branch from 748633b to 72b9db0 Compare June 11, 2023 13:58
this.rootCache = new Cache(undefined, undefined, pageSize, size, this.isExpanded);
}

get effectiveSize() {

@vursen vursen Jun 11, 2023 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

idea: rename effectiveSize to flatSize to be aligned with flatIndex.

Suggested change
get effectiveSize() {
get flatSize() {

@vursen
vursen force-pushed the feat/data-provider-controller branch from a15a95d to 9566f03 Compare June 12, 2023 18:07
@vursen vursen changed the title refactor: extract grid data provider functionality into a controller (WIP) [WIP] refactor: extract grid data provider functionality into a controller Jun 29, 2023
@vursen
vursen force-pushed the feat/data-provider-controller branch 3 times, most recently from acdc5a0 to c9a73f7 Compare July 7, 2023 08:26
@vursen
vursen force-pushed the feat/data-provider-controller branch from 950c754 to e99eb5a Compare July 10, 2023 14:05
@vursen
vursen force-pushed the feat/data-provider-controller branch 2 times, most recently from 12034fa to e4b5cdf Compare July 26, 2023 11:31
import { Cache } from './data-provider-controller/cache.js';
import { getFlatIndexByPath, getFlatIndexInfo } from './data-provider-controller/helpers.js';

export class DataProviderController extends EventTarget {

@vursen vursen Jul 28, 2023 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: Should we name it DataController?

@vursen
vursen force-pushed the feat/data-provider-controller branch from 97aa19f to 56a2daf Compare July 30, 2023 17:15
* @param {number} flatIndex
* @return {{ cache: Cache, item: object | undefined, index: number, page: number, level: number }}
*/
export function getFlatIndexInfo(cache, flatIndex, level = 0) {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note: Replaces ItemCache.getCacheAndIndex and ItemCache.getItemForIndex

getItemForIndex(index) {
const { cache, scaledIndex } = this.getCacheAndIndex(index);
return cache.items[scaledIndex];
}

getCacheAndIndex(index) {
let thisLevelIndex = index;
for (const [index, subCache] of Object.entries(this.itemCaches)) {
const numberIndex = Number(index);
if (thisLevelIndex <= numberIndex) {
return { cache: this, scaledIndex: thisLevelIndex };
} else if (thisLevelIndex <= numberIndex + subCache.effectiveSize) {
return subCache.getCacheAndIndex(thisLevelIndex - numberIndex - 1);
}
thisLevelIndex -= subCache.effectiveSize;
}
return { cache: this, scaledIndex: thisLevelIndex };
}

@vursen vursen Aug 4, 2023 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: Should these new functions be separate helpers or should they still be methods of the Cache class?

* @param {number} flatIndex
* @return {number}
*/
export function getFlatIndexByPath(cache, [levelIndex, ...subIndexes], flatIndex = 0) {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note: Replaces grid's __getGlobalFlatIndex method:

__getGlobalFlatIndex([levelIndex, ...subIndexes], cache = this._cache, flatIndex = 0) {
if (levelIndex === Infinity) {
// Treat Infinity as the last index on the level
levelIndex = cache.size - 1;
}
const flatIndexOnLevel = cache.getFlatIndex(levelIndex);
const subCache = cache.itemCaches[levelIndex];
if (subCache && subCache.effectiveSize && subIndexes.length) {
return this.__getGlobalFlatIndex(subIndexes, subCache, flatIndex + flatIndexOnLevel + 1);
}
return flatIndex + flatIndexOnLevel;
}

return this.rootCache.effectiveSize;
}

isLoading() {

@vursen vursen Aug 4, 2023 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note: I chose not to make it a getter because that complicated overriding it in the Grid connector.

if (size !== undefined) {
cache.size = size;
} else if (params.parentItem) {
cache.size = items.length;

@vursen vursen Aug 4, 2023 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note: This seems a bit inconsistent. Why do we set the size to the length of the items only for nested levels? However, when I tried to change it, it broke some grid tests, so I decided to leave it as it is for now.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, could be because the root cache size can be defined as grid.size, but there's no such option for child caches. I think grid.size property could be removed (and callback size made mandatory), because of this inconsistency.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Created a ticket proposing to deprecate and eventually drop the size property: #6585

this.size = size || 0;
this.effectiveSize = size || 0;
this.parentCache = parentCache;
this.parentCacheIndex = parentCacheIndex;

@vursen vursen Aug 4, 2023 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note: Originally, the ItemCache class held a direct reference to the parentItem. However, by passing parentCacheIndex, we can simplify this place in the Grid connector.

@vursen vursen changed the title [WIP] refactor: extract grid data provider functionality into a controller refactor: extract grid data provider functionality into a controller Aug 4, 2023
@vursen
vursen requested a review from tomivirkki August 4, 2023 08:36
@vursen
vursen marked this pull request as ready for review August 4, 2023 08:42
@vursen

vursen commented Aug 4, 2023 •

Copy link
Copy Markdown
Contributor Author

The test coverage is currently ensured by the grid tests, and the controller lacks its own unit tests to test it as an individual module. This doesn't seem like a blocker though. How about we add them separately? Until then, we could keep the controller inside the grid package.

@tomivirkki tomivirkki left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a great cleanup/refactor 💪

It's also quite a fundamental refactor to the very core of Grid, taking into account the changes it also requires to the connector. Is the intention to have this merged for a minor or a major?

@vursen
vursen requested a review from sissbruecker August 11, 2023 07:30
@vursen

vursen commented Aug 11, 2023 •

Copy link
Copy Markdown
Contributor Author

It's also quite a fundamental refactor to the very core of Grid, taking into account the changes it also requires to the connector. Is the intention to have this merged for a minor or a major?

Good question. At least, the goal was to make the controller close to the original implementation as much as possible. It's assumed that the controller doesn't introduce any breaking changes to the API and behavior. As long as this is the case, I'd treat it as an internal refactoring that could potentially land in a minor version.

@vursen
vursen force-pushed the feat/data-provider-controller branch from e8007a2 to 67ab097 Compare August 11, 2023 08:06
@vursen
vursen force-pushed the feat/data-provider-controller branch from 4415f30 to f027684 Compare August 25, 2023 11:54
@vursen
vursen removed the request for review from sissbruecker August 25, 2023 12:01
@vursen
vursen requested a review from web-padawan August 25, 2023 12:01
@vursen
vursen force-pushed the feat/data-provider-controller branch from de0b920 to df8f546 Compare August 26, 2023 10:08
@vursen
vursen force-pushed the feat/data-provider-controller branch 4 times, most recently from f28d68d to 3691fad Compare September 22, 2023 09:30
@vursen
vursen force-pushed the feat/data-provider-controller branch from 3691fad to 398e4c3 Compare September 22, 2023 10:42
@sonarqubecloud

Copy link
Copy Markdown

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 1 Code Smell

No Coverage information No Coverage information
0.0% 0.0% Duplication

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants