Skip to content

Commit

Permalink
refactor: improve performance of checking if item is selected (#3845) (
Browse files Browse the repository at this point in the history
…#3858)

Co-authored-by: Pepijn Van Eeckhoudt <[email protected]>
  • Loading branch information
web-padawan and pepijnve authored May 16, 2022
1 parent 7fa1e56 commit e707adb
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions packages/grid/src/vaadin-grid-selection-mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,21 @@ export const SelectionMixin = (superClass) =>
type: Object,
notify: true,
value: () => []
},

/**
* Set of selected item ids
* @private
*/
__selectedKeys: {
type: Object,
value: () => new Set()
}
};
}

static get observers() {
return ['_selectedItemsChanged(selectedItems.*)'];
return ['_updateSelectedKeys(itemIdPath, selectedItems.*)'];
}

/**
Expand All @@ -33,7 +42,7 @@ export const SelectionMixin = (superClass) =>
* @protected
*/
_isSelected(item) {
return this.selectedItems && this._getItemIndexInArray(item, this.selectedItems) > -1;
return this.__selectedKeys.has(this.getItemId(item));
}

/**
Expand Down Expand Up @@ -68,16 +77,21 @@ export const SelectionMixin = (superClass) =>
* @protected
*/
_toggleItem(item) {
const index = this._getItemIndexInArray(item, this.selectedItems);
if (index === -1) {
if (!this._isSelected(item)) {
this.selectItem(item);
} else {
this.deselectItem(item);
}
}

/** @private */
_selectedItemsChanged() {
_updateSelectedKeys() {
const selectedItems = this.selectedItems || [];
this.__selectedKeys = new Set();
selectedItems.forEach((item) => {
this.__selectedKeys.add(this.getItemId(item));
});

this.requestContentUpdate();
}

Expand Down

0 comments on commit e707adb

Please sign in to comment.