Skip to content

Commit

Permalink
perf(cdk/a11y): avoid triggering change detection if there are no sub…
Browse files Browse the repository at this point in the history
…scribers to stream

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.
  • Loading branch information
crisbeto committed Dec 25, 2020
1 parent 71b7b15 commit f3ce4d8
Showing 1 changed file with 20 additions and 10 deletions.
30 changes: 20 additions & 10 deletions src/cdk/a11y/focus-monitor/focus-monitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
Output,
AfterViewInit,
} from '@angular/core';
import {Observable, of as observableOf, Subject, Subscription} from 'rxjs';
import {Observable, of as observableOf, Subject, Subscription, Observer} from 'rxjs';
import {coerceElement} from '@angular/cdk/coercion';
import {DOCUMENT} from '@angular/common';
import {isFakeMousedownFromScreenReader} from '../fake-mousedown';
Expand Down Expand Up @@ -70,6 +70,7 @@ type MonitoredElementInfo = {
checkChildren: boolean,
subject: Subject<FocusOrigin>,
rootNode: HTMLElement|Document
observable: Observable<FocusOrigin>
};

/**
Expand Down Expand Up @@ -251,15 +252,28 @@ export class FocusMonitor implements OnDestroy {
}

// Create monitored element info.
const subject = new Subject<FocusOrigin>();
const info: MonitoredElementInfo = {
checkChildren: checkChildren,
subject: new Subject<FocusOrigin>(),
rootNode
subject,
rootNode,
// Note that we want the observable to emit inside the NgZone, however we don't want to
// trigger change detection if nobody has subscribed to it. We do so by creating the
// observable manually.
observable: new Observable((observer: Observer<FocusOrigin>) => {
const subscription = subject.subscribe(origin => {
this._ngZone.run(() => observer.next(origin));
});

return () => {
subscription.unsubscribe();
};
})
};
this._elementInfo.set(nativeElement, info);
this._registerGlobalListeners(info);

return info.subject;
return info.observable;
}

/**
Expand Down Expand Up @@ -464,11 +478,7 @@ export class FocusMonitor implements OnDestroy {
}

this._setClasses(element);
this._emitOrigin(elementInfo.subject, null);
}

private _emitOrigin(subject: Subject<FocusOrigin>, origin: FocusOrigin) {
this._ngZone.run(() => subject.next(origin));
elementInfo.subject.next(null);
}

private _registerGlobalListeners(elementInfo: MonitoredElementInfo) {
Expand Down Expand Up @@ -550,7 +560,7 @@ export class FocusMonitor implements OnDestroy {
private _originChanged(element: HTMLElement, origin: FocusOrigin,
elementInfo: MonitoredElementInfo) {
this._setClasses(element, origin);
this._emitOrigin(elementInfo.subject, origin);
elementInfo.subject.next(origin);
this._lastFocusOrigin = origin;
}
}
Expand Down

0 comments on commit f3ce4d8

Please sign in to comment.