Skip to content

Commit bc6b700

Browse files
authored
Revert "perf(focus-monitor): avoid triggering change detection if there 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.
1 parent 5846038 commit bc6b700

File tree

1 file changed

+14
-24
lines changed

1 file changed

+14
-24
lines changed

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

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import {
1818
Output,
1919
SkipSelf,
2020
} from '@angular/core';
21-
import {Observable, of as observableOf, Subject, Subscription, Observer} from 'rxjs';
21+
import {Observable, of as observableOf, Subject, Subscription} from 'rxjs';
2222
import {coerceElement} from '@angular/cdk/coercion';
2323

2424

@@ -41,8 +41,7 @@ export interface FocusOptions {
4141
type MonitoredElementInfo = {
4242
unlisten: Function,
4343
checkChildren: boolean,
44-
subject: Subject<FocusOrigin>,
45-
observable: Observable<FocusOrigin>
44+
subject: Subject<FocusOrigin>
4645
};
4746

4847
/**
@@ -170,30 +169,17 @@ export class FocusMonitor implements OnDestroy {
170169
}
171170

172171
// Create monitored element info.
173-
const subject = new Subject<FocusOrigin>();
174-
const info: MonitoredElementInfo = {
172+
let info: MonitoredElementInfo = {
175173
unlisten: () => {},
176-
checkChildren,
177-
subject,
178-
// Note that we want the observable to emit inside the NgZone, however we don't want to
179-
// trigger change detection if nobody has subscribed to it. We do so by creating the
180-
// observable manually.
181-
observable: new Observable((observer: Observer<FocusOrigin>) => {
182-
const subscription = subject.subscribe(origin => {
183-
this._ngZone.run(() => observer.next(origin));
184-
});
185-
186-
return () => {
187-
subscription.unsubscribe();
188-
};
189-
})
174+
checkChildren: checkChildren,
175+
subject: new Subject<FocusOrigin>()
190176
};
191177
this._elementInfo.set(nativeElement, info);
192178
this._incrementMonitoredElementCount();
193179

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

208-
return info.observable;
194+
return info.subject.asObservable();
209195
}
210196

211197
/**
@@ -372,7 +358,7 @@ export class FocusMonitor implements OnDestroy {
372358
}
373359

374360
this._setClasses(element, origin);
375-
elementInfo.subject.next(origin);
361+
this._emitOrigin(elementInfo.subject, origin);
376362
this._lastFocusOrigin = origin;
377363
}
378364

@@ -392,7 +378,11 @@ export class FocusMonitor implements OnDestroy {
392378
}
393379

394380
this._setClasses(element);
395-
elementInfo.subject.next(null);
381+
this._emitOrigin(elementInfo.subject, null);
382+
}
383+
384+
private _emitOrigin(subject: Subject<FocusOrigin>, origin: FocusOrigin) {
385+
this._ngZone.run(() => subject.next(origin));
396386
}
397387

398388
private _incrementMonitoredElementCount() {

0 commit comments

Comments
 (0)