Skip to content

Commit 632fea0

Browse files
edusperoniNathanWalker
authored andcommitted
perf: improve attribute selectors by adding single listeners
1 parent 632a348 commit 632fea0

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

packages/core/ui/styling/style-scope.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import * as capm from './css-animation-parser';
2222
import { sanitizeModuleName } from '../../utils/common';
2323
import { resolveModuleName } from '../../module-name-resolver';
2424
import { cleanupImportantFlags } from './css-utils';
25+
import { Observable, PropertyChangeData } from '../../data/observable';
2526

2627
let cssAnimationParserModule: typeof capm;
2728
function ensureCssAnimationParserModule() {
@@ -418,6 +419,8 @@ export class CssState {
418419
_matchInvalid: boolean;
419420
_playsKeyframeAnimations: boolean;
420421

422+
private _dynamicUpdateListenerMap: Map<ViewBase, (t: any) => void> = new Map();
423+
421424
constructor(private viewRef: WeakRef<ViewBase>) {
422425
this._onDynamicStateChangeHandler = () => this.updateDynamicState();
423426
}
@@ -650,9 +653,14 @@ export class CssState {
650653
const changeMap = this._match.changeMap;
651654
changeMap.forEach((changes, view) => {
652655
if (changes.attributes) {
653-
changes.attributes.forEach((attribute) => {
654-
view.addEventListener(attribute + 'Change', this._onDynamicStateChangeHandler);
655-
});
656+
const attributes = changes.attributes;
657+
const listener = (args: PropertyChangeData) => {
658+
if (attributes.has(args.propertyName)) {
659+
this._onDynamicStateChangeHandler();
660+
}
661+
};
662+
this._dynamicUpdateListenerMap.set(view, listener);
663+
view.addEventListener(Observable.propertyChangeEvent, listener);
656664
}
657665
if (changes.pseudoClasses) {
658666
changes.pseudoClasses.forEach((pseudoClass) => {
@@ -669,10 +677,8 @@ export class CssState {
669677

670678
private unsubscribeFromDynamicUpdates(): void {
671679
this._appliedChangeMap.forEach((changes, view) => {
672-
if (changes.attributes) {
673-
changes.attributes.forEach((attribute) => {
674-
view.removeEventListener(attribute + 'Change', this._onDynamicStateChangeHandler);
675-
});
680+
if (this._dynamicUpdateListenerMap.has(view)) {
681+
view.removeEventListener(Observable.propertyChangeEvent, this._dynamicUpdateListenerMap.get(view));
676682
}
677683
if (changes.pseudoClasses) {
678684
changes.pseudoClasses.forEach((pseudoClass) => {
@@ -684,6 +690,7 @@ export class CssState {
684690
});
685691
}
686692
});
693+
this._dynamicUpdateListenerMap.clear();
687694
this._appliedChangeMap = CssState.emptyChangeMap;
688695
}
689696

0 commit comments

Comments
 (0)