mirror of
https://github.com/angular/angular.git
synced 2026-09-14 13:54:52 +08:00
perf(focus-monitor): mark event listeners as passive (#13532)
This commit is contained in:
committed by
Joey Perrott
parent
b5a0b168ee
commit
bfc00a005e
@@ -241,21 +241,28 @@ export class FocusMonitor implements OnDestroy {
|
||||
this._windowFocusTimeoutId = setTimeout(() => this._windowFocused = false);
|
||||
};
|
||||
|
||||
// Event listener options that enable capturing and also mark the the listener as passive
|
||||
// if the browser supports it.
|
||||
const captureEventListenerOptions = supportsPassiveEventListeners() ?
|
||||
{passive: true, capture: true} : true;
|
||||
|
||||
// Note: we listen to events in the capture phase so we can detect them even if the user stops
|
||||
// propagation.
|
||||
this._ngZone.runOutsideAngular(() => {
|
||||
document.addEventListener('keydown', documentKeydownListener, true);
|
||||
document.addEventListener('mousedown', documentMousedownListener, true);
|
||||
document.addEventListener('keydown', documentKeydownListener, captureEventListenerOptions);
|
||||
document.addEventListener('mousedown', documentMousedownListener,
|
||||
captureEventListenerOptions);
|
||||
document.addEventListener('touchstart', documentTouchstartListener,
|
||||
supportsPassiveEventListeners() ? ({passive: true, capture: true} as any) : true);
|
||||
captureEventListenerOptions);
|
||||
window.addEventListener('focus', windowFocusListener);
|
||||
});
|
||||
|
||||
this._unregisterGlobalListeners = () => {
|
||||
document.removeEventListener('keydown', documentKeydownListener, true);
|
||||
document.removeEventListener('mousedown', documentMousedownListener, true);
|
||||
document.removeEventListener('keydown', documentKeydownListener, captureEventListenerOptions);
|
||||
document.removeEventListener('mousedown', documentMousedownListener,
|
||||
captureEventListenerOptions);
|
||||
document.removeEventListener('touchstart', documentTouchstartListener,
|
||||
supportsPassiveEventListeners() ? ({passive: true, capture: true} as any) : true);
|
||||
captureEventListenerOptions);
|
||||
window.removeEventListener('focus', windowFocusListener);
|
||||
|
||||
// Clear timeouts for all potentially pending timeouts to prevent the leaks.
|
||||
|
||||
Reference in New Issue
Block a user