Skip to content

Commit bb83add

Browse files
authored
perf: improve attribute selectors by adding single listeners (#10384)
1 parent b8fff38 commit bb83add

File tree

3 files changed

+19
-7
lines changed

3 files changed

+19
-7
lines changed

packages/core/css/system-classes.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export namespace CSSUtils {
66
export const CLASS_PREFIX = 'ns-';
77
export const MODAL_ROOT_VIEW_CSS_CLASS = `${CLASS_PREFIX}${MODAL}`;
88
export const ROOT_VIEW_CSS_CLASS = `${CLASS_PREFIX}${ROOT}`;
9+
export const IgnoredCssDynamicAttributeTracking = new Set<string>();
910

1011
export function getSystemCssClasses(): string[] {
1112
return cssClasses;

packages/core/ui/styling/css-selector.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { Trace } from '../../trace';
55
import { isNullOrUndefined } from '../../utils/types';
66

77
import * as ReworkCSS from '../../css';
8+
import { CSSUtils } from '../../css/system-classes';
89

910
/**
1011
* An interface describing the shape of a type on which the selectors may apply.
@@ -1014,6 +1015,9 @@ export class SelectorsMatch<T extends Node> implements ChangeAccumulator {
10141015
public selectors: SelectorCore[];
10151016

10161017
public addAttribute(node: T, attribute: string): void {
1018+
if (CSSUtils.IgnoredCssDynamicAttributeTracking.has(attribute)) {
1019+
return;
1020+
}
10171021
const deps: Changes = this.properties(node);
10181022
if (!deps.attributes) {
10191023
deps.attributes = new Set();

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)