Skip to content

Commit

Permalink
perf(focus-monitor): mark event listeners as passive (#13532)
Browse files Browse the repository at this point in the history
  • Loading branch information
devversion authored and josephperrott committed Oct 19, 2018
1 parent 2c4c5ba commit ba0c820
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions src/cdk/a11y/focus-monitor/focus-monitor.ts
Expand Up @@ -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.
Expand Down

0 comments on commit ba0c820

Please sign in to comment.