Skip to content

Commit

Permalink
Revert "perf(focus-monitor): avoid triggering change detection if the…
Browse files Browse the repository at this point in the history
…re are no subscribers to stream" (#15076)

* Revert "fix(radio): unable to click to select button if text is marked (#14967)"

This reverts commit 5846038.

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

This reverts commit 085bbb7.
  • Loading branch information
mmalerba authored Feb 4, 2019
1 parent 5846038 commit bc6b700
Showing 1 changed file with 14 additions and 24 deletions.
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

0 comments on commit bc6b700

Please sign in to comment.