Skip to content

Revert "perf(focus-monitor): avoid triggering change detection if there are no subscribers to stream" #15076

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 4, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 14 additions & 24 deletions src/cdk/a11y/focus-monitor/focus-monitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
Output,
SkipSelf,
} from '@angular/core';
import {Observable, of as observableOf, Subject, Subscription, Observer} from 'rxjs';
import {Observable, of as observableOf, Subject, Subscription} from 'rxjs';
import {coerceElement} from '@angular/cdk/coercion';


Expand All @@ -41,8 +41,7 @@ export interface FocusOptions {
type MonitoredElementInfo = {
unlisten: Function,
checkChildren: boolean,
subject: Subject<FocusOrigin>,
observable: Observable<FocusOrigin>
subject: Subject<FocusOrigin>
};

/**
Expand Down Expand Up @@ -170,30 +169,17 @@ export class FocusMonitor implements OnDestroy {
}

// Create monitored element info.
const subject = new Subject<FocusOrigin>();
const info: MonitoredElementInfo = {
let info: MonitoredElementInfo = {
unlisten: () => {},
checkChildren,
subject,
// 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();
};
})
checkChildren: checkChildren,
subject: new Subject<FocusOrigin>()
};
this._elementInfo.set(nativeElement, info);
this._incrementMonitoredElementCount();

// Start listening. We need to listen in capture phase since focus events don't bubble.
const focusListener = (event: FocusEvent) => this._onFocus(event, nativeElement);
const blurListener = (event: FocusEvent) => this._onBlur(event, nativeElement);
let focusListener = (event: FocusEvent) => this._onFocus(event, nativeElement);
let blurListener = (event: FocusEvent) => this._onBlur(event, nativeElement);
this._ngZone.runOutsideAngular(() => {
nativeElement.addEventListener('focus', focusListener, true);
nativeElement.addEventListener('blur', blurListener, true);
Expand All @@ -205,7 +191,7 @@ export class FocusMonitor implements OnDestroy {
nativeElement.removeEventListener('blur', blurListener, true);
};

return info.observable;
return info.subject.asObservable();
}

/**
Expand Down Expand Up @@ -372,7 +358,7 @@ export class FocusMonitor implements OnDestroy {
}

this._setClasses(element, origin);
elementInfo.subject.next(origin);
this._emitOrigin(elementInfo.subject, origin);
this._lastFocusOrigin = origin;
}

Expand All @@ -392,7 +378,11 @@ export class FocusMonitor implements OnDestroy {
}

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

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

private _incrementMonitoredElementCount() {
Expand Down