Repository navigation
refactor: extract grid data provider functionality into a controller - #5961
Conversation
| ArrayDataProviderMixin( | ||
| DataProviderMixin( |
There was a problem hiding this comment.
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:
748633b to
72b9db0
Compare
| this.rootCache = new Cache(undefined, undefined, pageSize, size, this.isExpanded); | ||
| } | ||
|
|
||
| get effectiveSize() { |
There was a problem hiding this comment.
idea: rename effectiveSize to flatSize to be aligned with flatIndex.
| get effectiveSize() { | |
| get flatSize() { |
a15a95d to
9566f03
Compare
acdc5a0 to
c9a73f7
Compare
950c754 to
e99eb5a
Compare
12034fa to
e4b5cdf
Compare
| import { Cache } from './data-provider-controller/cache.js'; | ||
| import { getFlatIndexByPath, getFlatIndexInfo } from './data-provider-controller/helpers.js'; | ||
|
|
||
| export class DataProviderController extends EventTarget { |
There was a problem hiding this comment.
question: Should we name it DataController?
97aa19f to
56a2daf
Compare
| * @param {number} flatIndex | ||
| * @return {{ cache: Cache, item: object | undefined, index: number, page: number, level: number }} | ||
| */ | ||
| export function getFlatIndexInfo(cache, flatIndex, level = 0) { |
There was a problem hiding this comment.
note: Replaces ItemCache.getCacheAndIndex and ItemCache.getItemForIndex
web-components/packages/grid/src/vaadin-grid-data-provider-mixin.js
Lines 54 to 57 in f1cea1b
web-components/packages/grid/src/vaadin-grid-data-provider-mixin.js
Lines 86 to 98 in f1cea1b
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
note: Replaces grid's __getGlobalFlatIndex method:
web-components/packages/grid/src/vaadin-grid-data-provider-mixin.js
Lines 585 to 596 in f1cea1b
| return this.rootCache.effectiveSize; | ||
| } | ||
|
|
||
| isLoading() { |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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.
|
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
left a comment
There was a problem hiding this comment.
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?
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. |
e8007a2 to
67ab097
Compare
4415f30 to
f027684
Compare
de0b920 to
df8f546
Compare
f28d68d to
3691fad
Compare
3691fad to
398e4c3
Compare
|
Kudos, SonarCloud Quality Gate passed!
|








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:
Type of change