Skip to content

Commit e6b9679

Browse files
authored
Revert "perf(cdk/overlay): add event listeners for overlay dispatchers outside of zone (#23962)" (#24353)
This reverts commit e761455.
1 parent 7b19fd1 commit e6b9679

File tree

5 files changed

+23
-149
lines changed

5 files changed

+23
-149
lines changed

src/cdk/overlay/dispatchers/overlay-keyboard-dispatcher.spec.ts

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
import {TestBed, inject} from '@angular/core/testing';
22
import {dispatchKeyboardEvent} from '../../testing/private';
33
import {ESCAPE} from '@angular/cdk/keycodes';
4-
import {ApplicationRef, Component} from '@angular/core';
4+
import {Component} from '@angular/core';
55
import {OverlayModule, Overlay} from '../index';
66
import {OverlayKeyboardDispatcher} from './overlay-keyboard-dispatcher';
77
import {ComponentPortal} from '@angular/cdk/portal';
88

99
describe('OverlayKeyboardDispatcher', () => {
10-
let appRef: ApplicationRef;
1110
let keyboardDispatcher: OverlayKeyboardDispatcher;
1211
let overlay: Overlay;
1312

@@ -17,14 +16,10 @@ describe('OverlayKeyboardDispatcher', () => {
1716
declarations: [TestComponent],
1817
});
1918

20-
inject(
21-
[ApplicationRef, OverlayKeyboardDispatcher, Overlay],
22-
(ar: ApplicationRef, kbd: OverlayKeyboardDispatcher, o: Overlay) => {
23-
appRef = ar;
24-
keyboardDispatcher = kbd;
25-
overlay = o;
26-
},
27-
)();
19+
inject([OverlayKeyboardDispatcher, Overlay], (kbd: OverlayKeyboardDispatcher, o: Overlay) => {
20+
keyboardDispatcher = kbd;
21+
overlay = o;
22+
})();
2823
});
2924

3025
it('should track overlays in order as they are attached and detached', () => {
@@ -184,21 +179,6 @@ describe('OverlayKeyboardDispatcher', () => {
184179
expect(overlayTwoSpy).not.toHaveBeenCalled();
185180
expect(overlayOneSpy).toHaveBeenCalled();
186181
});
187-
188-
it('should not run change detection if there are no `keydownEvents` observers', () => {
189-
spyOn(appRef, 'tick');
190-
const overlayRef = overlay.create();
191-
keyboardDispatcher.add(overlayRef);
192-
193-
expect(appRef.tick).toHaveBeenCalledTimes(0);
194-
dispatchKeyboardEvent(document.body, 'keydown', ESCAPE);
195-
expect(appRef.tick).toHaveBeenCalledTimes(0);
196-
197-
overlayRef.keydownEvents().subscribe();
198-
dispatchKeyboardEvent(document.body, 'keydown', ESCAPE);
199-
200-
expect(appRef.tick).toHaveBeenCalledTimes(1);
201-
});
202182
});
203183

204184
@Component({

src/cdk/overlay/dispatchers/overlay-keyboard-dispatcher.ts

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88

99
import {DOCUMENT} from '@angular/common';
10-
import {Inject, Injectable, NgZone, Optional} from '@angular/core';
10+
import {Inject, Injectable} from '@angular/core';
1111
import {OverlayReference} from '../overlay-reference';
1212
import {BaseOverlayDispatcher} from './base-overlay-dispatcher';
1313

@@ -18,11 +18,7 @@ import {BaseOverlayDispatcher} from './base-overlay-dispatcher';
1818
*/
1919
@Injectable({providedIn: 'root'})
2020
export class OverlayKeyboardDispatcher extends BaseOverlayDispatcher {
21-
constructor(
22-
@Inject(DOCUMENT) document: any,
23-
/** @breaking-change 14.0.0 _ngZone will be required. */
24-
@Optional() private _ngZone?: NgZone,
25-
) {
21+
constructor(@Inject(DOCUMENT) document: any) {
2622
super(document);
2723
}
2824

@@ -32,14 +28,7 @@ export class OverlayKeyboardDispatcher extends BaseOverlayDispatcher {
3228

3329
// Lazily start dispatcher once first overlay is added
3430
if (!this._isAttached) {
35-
/** @breaking-change 14.0.0 _ngZone will be required. */
36-
if (this._ngZone) {
37-
this._ngZone.runOutsideAngular(() =>
38-
this._document.body.addEventListener('keydown', this._keydownListener),
39-
);
40-
} else {
41-
this._document.body.addEventListener('keydown', this._keydownListener);
42-
}
31+
this._document.body.addEventListener('keydown', this._keydownListener);
4332
this._isAttached = true;
4433
}
4534
}
@@ -64,13 +53,7 @@ export class OverlayKeyboardDispatcher extends BaseOverlayDispatcher {
6453
// because we don't want overlays that don't handle keyboard events to block the ones below
6554
// them that do.
6655
if (overlays[i]._keydownEvents.observers.length > 0) {
67-
const keydownEvents = overlays[i]._keydownEvents;
68-
/** @breaking-change 14.0.0 _ngZone will be required. */
69-
if (this._ngZone) {
70-
this._ngZone.run(() => keydownEvents.next(event));
71-
} else {
72-
keydownEvents.next(event);
73-
}
56+
overlays[i]._keydownEvents.next(event);
7457
break;
7558
}
7659
}

src/cdk/overlay/dispatchers/overlay-outside-click-dispatcher.spec.ts

Lines changed: 3 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import {TestBed, inject, fakeAsync} from '@angular/core/testing';
2-
import {ApplicationRef, Component} from '@angular/core';
2+
import {Component} from '@angular/core';
33
import {dispatchFakeEvent, dispatchMouseEvent} from '../../testing/private';
44
import {OverlayModule, Overlay} from '../index';
55
import {OverlayOutsideClickDispatcher} from './overlay-outside-click-dispatcher';
66
import {ComponentPortal} from '@angular/cdk/portal';
77

88
describe('OverlayOutsideClickDispatcher', () => {
9-
let appRef: ApplicationRef;
109
let outsideClickDispatcher: OverlayOutsideClickDispatcher;
1110
let overlay: Overlay;
1211

@@ -17,9 +16,8 @@ describe('OverlayOutsideClickDispatcher', () => {
1716
});
1817

1918
inject(
20-
[ApplicationRef, OverlayOutsideClickDispatcher, Overlay],
21-
(ar: ApplicationRef, ocd: OverlayOutsideClickDispatcher, o: Overlay) => {
22-
appRef = ar;
19+
[OverlayOutsideClickDispatcher, Overlay],
20+
(ocd: OverlayOutsideClickDispatcher, o: Overlay) => {
2321
outsideClickDispatcher = ocd;
2422
overlay = o;
2523
},
@@ -338,70 +336,6 @@ describe('OverlayOutsideClickDispatcher', () => {
338336
thirdOverlayRef.dispose();
339337
}),
340338
);
341-
342-
describe('change detection behavior', () => {
343-
it('should not run change detection if there is no portal attached to the overlay', () => {
344-
spyOn(appRef, 'tick');
345-
const overlayRef = overlay.create();
346-
outsideClickDispatcher.add(overlayRef);
347-
348-
const context = document.createElement('div');
349-
document.body.appendChild(context);
350-
351-
overlayRef.outsidePointerEvents().subscribe();
352-
dispatchMouseEvent(context, 'click');
353-
354-
expect(appRef.tick).toHaveBeenCalledTimes(0);
355-
});
356-
357-
it('should not run change detection if the click was made outside the overlay but there are no `outsidePointerEvents` observers', () => {
358-
spyOn(appRef, 'tick');
359-
const portal = new ComponentPortal(TestComponent);
360-
const overlayRef = overlay.create();
361-
overlayRef.attach(portal);
362-
outsideClickDispatcher.add(overlayRef);
363-
364-
const context = document.createElement('div');
365-
document.body.appendChild(context);
366-
367-
dispatchMouseEvent(context, 'click');
368-
369-
expect(appRef.tick).toHaveBeenCalledTimes(0);
370-
});
371-
372-
it('should not run change detection if the click was made inside the overlay and there are `outsidePointerEvents` observers', () => {
373-
spyOn(appRef, 'tick');
374-
const portal = new ComponentPortal(TestComponent);
375-
const overlayRef = overlay.create();
376-
overlayRef.attach(portal);
377-
outsideClickDispatcher.add(overlayRef);
378-
379-
overlayRef.outsidePointerEvents().subscribe();
380-
dispatchMouseEvent(overlayRef.overlayElement, 'click');
381-
382-
expect(appRef.tick).toHaveBeenCalledTimes(0);
383-
});
384-
385-
it('should run change detection if the click was made outside the overlay and there are `outsidePointerEvents` observers', () => {
386-
spyOn(appRef, 'tick');
387-
const portal = new ComponentPortal(TestComponent);
388-
const overlayRef = overlay.create();
389-
overlayRef.attach(portal);
390-
outsideClickDispatcher.add(overlayRef);
391-
392-
const context = document.createElement('div');
393-
document.body.appendChild(context);
394-
395-
expect(appRef.tick).toHaveBeenCalledTimes(0);
396-
dispatchMouseEvent(context, 'click');
397-
expect(appRef.tick).toHaveBeenCalledTimes(0);
398-
399-
overlayRef.outsidePointerEvents().subscribe();
400-
401-
dispatchMouseEvent(context, 'click');
402-
expect(appRef.tick).toHaveBeenCalledTimes(1);
403-
});
404-
});
405339
});
406340

407341
@Component({

src/cdk/overlay/dispatchers/overlay-outside-click-dispatcher.ts

Lines changed: 7 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88

99
import {DOCUMENT} from '@angular/common';
10-
import {Inject, Injectable, NgZone, Optional} from '@angular/core';
10+
import {Inject, Injectable} from '@angular/core';
1111
import {OverlayReference} from '../overlay-reference';
1212
import {Platform, _getEventTarget} from '@angular/cdk/platform';
1313
import {BaseOverlayDispatcher} from './base-overlay-dispatcher';
@@ -23,12 +23,7 @@ export class OverlayOutsideClickDispatcher extends BaseOverlayDispatcher {
2323
private _cursorStyleIsSet = false;
2424
private _pointerDownEventTarget: EventTarget | null;
2525

26-
constructor(
27-
@Inject(DOCUMENT) document: any,
28-
private _platform: Platform,
29-
/** @breaking-change 14.0.0 _ngZone will be required. */
30-
@Optional() private _ngZone?: NgZone,
31-
) {
26+
constructor(@Inject(DOCUMENT) document: any, private _platform: Platform) {
3227
super(document);
3328
}
3429

@@ -44,13 +39,10 @@ export class OverlayOutsideClickDispatcher extends BaseOverlayDispatcher {
4439
// https://developer.apple.com/library/archive/documentation/AppleApplications/Reference/SafariWebContent/HandlingEvents/HandlingEvents.html
4540
if (!this._isAttached) {
4641
const body = this._document.body;
47-
48-
/** @breaking-change 14.0.0 _ngZone will be required. */
49-
if (this._ngZone) {
50-
this._ngZone.runOutsideAngular(() => this._addEventListeners(body));
51-
} else {
52-
this._addEventListeners(body);
53-
}
42+
body.addEventListener('pointerdown', this._pointerDownListener, true);
43+
body.addEventListener('click', this._clickListener, true);
44+
body.addEventListener('auxclick', this._clickListener, true);
45+
body.addEventListener('contextmenu', this._clickListener, true);
5446

5547
// click event is not fired on iOS. To make element "clickable" we are
5648
// setting the cursor to pointer
@@ -80,13 +72,6 @@ export class OverlayOutsideClickDispatcher extends BaseOverlayDispatcher {
8072
}
8173
}
8274

83-
private _addEventListeners(body: HTMLElement): void {
84-
body.addEventListener('pointerdown', this._pointerDownListener, true);
85-
body.addEventListener('click', this._clickListener, true);
86-
body.addEventListener('auxclick', this._clickListener, true);
87-
body.addEventListener('contextmenu', this._clickListener, true);
88-
}
89-
9075
/** Store pointerdown event target to track origin of click. */
9176
private _pointerDownListener = (event: PointerEvent) => {
9277
this._pointerDownEventTarget = _getEventTarget(event);
@@ -134,13 +119,7 @@ export class OverlayOutsideClickDispatcher extends BaseOverlayDispatcher {
134119
break;
135120
}
136121

137-
const outsidePointerEvents = overlayRef._outsidePointerEvents;
138-
/** @breaking-change 14.0.0 _ngZone will be required. */
139-
if (this._ngZone) {
140-
this._ngZone.run(() => outsidePointerEvents.next(event));
141-
} else {
142-
outsidePointerEvents.next(event);
143-
}
122+
overlayRef._outsidePointerEvents.next(event);
144123
}
145124
};
146125
}

tools/public_api_guard/cdk/overlay.md

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -317,12 +317,11 @@ export class OverlayContainer implements OnDestroy {
317317

318318
// @public
319319
export class OverlayKeyboardDispatcher extends BaseOverlayDispatcher {
320-
constructor(document: any,
321-
_ngZone?: NgZone | undefined);
320+
constructor(document: any);
322321
add(overlayRef: OverlayReference): void;
323322
protected detach(): void;
324323
// (undocumented)
325-
static ɵfac: i0.ɵɵFactoryDeclaration<OverlayKeyboardDispatcher, [null, { optional: true; }]>;
324+
static ɵfac: i0.ɵɵFactoryDeclaration<OverlayKeyboardDispatcher, never>;
326325
// (undocumented)
327326
static ɵprov: i0.ɵɵInjectableDeclaration<OverlayKeyboardDispatcher>;
328327
}
@@ -339,12 +338,11 @@ export class OverlayModule {
339338

340339
// @public
341340
export class OverlayOutsideClickDispatcher extends BaseOverlayDispatcher {
342-
constructor(document: any, _platform: Platform,
343-
_ngZone?: NgZone | undefined);
341+
constructor(document: any, _platform: Platform);
344342
add(overlayRef: OverlayReference): void;
345343
protected detach(): void;
346344
// (undocumented)
347-
static ɵfac: i0.ɵɵFactoryDeclaration<OverlayOutsideClickDispatcher, [null, null, { optional: true; }]>;
345+
static ɵfac: i0.ɵɵFactoryDeclaration<OverlayOutsideClickDispatcher, never>;
348346
// (undocumented)
349347
static ɵprov: i0.ɵɵInjectableDeclaration<OverlayOutsideClickDispatcher>;
350348
}

0 commit comments

Comments
 (0)