Skip to content

Commit ee452de

Browse files
authored
perf(cdk/a11y): avoid triggering change detection if there are no subscribers to stream (#15077)
Currently we have an `NgZone.run` call on each `focus` and `blur` event of a monitored event in order to bring its subscribers into the `NgZone`, however this means that we're also triggering change detection to any consumers that aren't subscribed to changes (e.g. `mat-button` only cares about the classes being applied). These changes move around some logic so that the `NgZone.run` is only hit if somebody has subscribed to the observable.
1 parent 0276784 commit ee452de

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

src/cdk/a11y/focus-monitor/focus-monitor.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -436,11 +436,13 @@ export class FocusMonitor implements OnDestroy {
436436
}
437437

438438
this._setClasses(element);
439-
this._emitOrigin(elementInfo.subject, null);
439+
this._emitOrigin(elementInfo, null);
440440
}
441441

442-
private _emitOrigin(subject: Subject<FocusOrigin>, origin: FocusOrigin) {
443-
this._ngZone.run(() => subject.next(origin));
442+
private _emitOrigin(info: MonitoredElementInfo, origin: FocusOrigin) {
443+
if (info.subject.observers.length) {
444+
this._ngZone.run(() => info.subject.next(origin));
445+
}
444446
}
445447

446448
private _registerGlobalListeners(elementInfo: MonitoredElementInfo) {
@@ -530,7 +532,7 @@ export class FocusMonitor implements OnDestroy {
530532
elementInfo: MonitoredElementInfo,
531533
) {
532534
this._setClasses(element, origin);
533-
this._emitOrigin(elementInfo.subject, origin);
535+
this._emitOrigin(elementInfo, origin);
534536
this._lastFocusOrigin = origin;
535537
}
536538

0 commit comments

Comments
 (0)