Skip to content

fix(cdk/overlay): ensure error isn't thrown when many overlay items are closed from handler #20377

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {TestBed, inject} from '@angular/core/testing';
import {TestBed, inject, fakeAsync} from '@angular/core/testing';
import {Component, NgModule} from '@angular/core';
import {dispatchMouseEvent} from '@angular/cdk/testing/private';
import {OverlayModule, OverlayContainer, Overlay} from '../index';
Expand Down Expand Up @@ -55,7 +55,10 @@ describe('OverlayOutsideClickDispatcher', () => {
'should dispatch mouse click events to the attached overlays',
() => {
const overlayOne = overlay.create();
overlayOne.attach(new ComponentPortal(TestComponent));
const overlayTwo = overlay.create();
overlayTwo.attach(new ComponentPortal(TestComponent));

const overlayOneSpy = jasmine.createSpy('overlayOne mouse click event spy');
const overlayTwoSpy = jasmine.createSpy('overlayTwo mouse click event spy');

Expand All @@ -81,6 +84,7 @@ describe('OverlayOutsideClickDispatcher', () => {
'should dispatch mouse click events to the attached overlays even when propagation is stopped',
() => {
const overlayRef = overlay.create();
overlayRef.attach(new ComponentPortal(TestComponent));
const spy = jasmine.createSpy('overlay mouse click event spy');
overlayRef.outsidePointerEvents().subscribe(spy);

Expand Down Expand Up @@ -186,6 +190,40 @@ describe('OverlayOutsideClickDispatcher', () => {

overlayRef.dispose();
});

it(
'should not throw an error when when closing out related components via the' +
' outsidePointerEvents emitter on background click',
fakeAsync(() => {
const firstOverlayRef = overlay.create();
firstOverlayRef.attach(new ComponentPortal(TestComponent));
const secondOverlayRef = overlay.create();
secondOverlayRef.attach(new ComponentPortal(TestComponent));
const thirdOverlayRef = overlay.create();
thirdOverlayRef.attach(new ComponentPortal(TestComponent));

const spy = jasmine.createSpy('background click handler spy').and.callFake(() => {
// we close out both overlays from a single outside click event
firstOverlayRef.detach();
thirdOverlayRef.detach();
});
firstOverlayRef.outsidePointerEvents().subscribe(spy);
secondOverlayRef.outsidePointerEvents().subscribe(spy);
thirdOverlayRef.outsidePointerEvents().subscribe(spy);

const backgroundElement = document.createElement('div');
document.body.appendChild(backgroundElement);

expect(() => backgroundElement.click()).not.toThrowError();

expect(spy).toHaveBeenCalled();

backgroundElement.parentNode!.removeChild(backgroundElement);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this go in an afterEach block in a separate describe block, if it needs to be run even if the test fails?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The jasmine tests don't exit on a failed expect so the closing logic will run regardless.

firstOverlayRef.dispose();
secondOverlayRef.dispose();
thirdOverlayRef.dispose();
}
));
});


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,18 @@ export class OverlayOutsideClickDispatcher extends BaseOverlayDispatcher {
private _clickListener = (event: MouseEvent) => {
// Get the target through the `composedPath` if possible to account for shadow DOM.
const target = event.composedPath ? event.composedPath()[0] : event.target;
const overlays = this._attachedOverlays;
// We copy the array because the original may be modified asynchronously if the
// outsidePointerEvents listener decides to detach overlays resulting in index errors inside
// the for loop.
const overlays = this._attachedOverlays.slice();
Copy link
Member

@crisbeto crisbeto Aug 21, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would prevent the error, but doesn't it have the potential of firing off the event for an overlay that's already closed? It may not be a problem in problem in practice since the Subject gets completed, but it still seems smelly.

Looking through the loop again, shouldn't we be breaking off as soon as we find an overlay to emit to? @jelbourn I was under the impression that we want to emit to the top overlay that has subscribers since that's how the keyboard dispatcher works too, but this will emit to multiple overlays. I think the OverlayKeyboardDispatcher is doing it correctly.

It isn't something changed by this PR, but we may have to address it at some point before people start depending on it.

Copy link
Contributor Author

@andy9775 andy9775 Aug 21, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It may fire off on an already closed overlay. An alternative to this would be to also check overlayRef.hasAttached() and continue to the next one if it has nothing. Or modifying the loop to consider mutations to the list when decrementing the counter and not copying the list - but this didn't seem ideal either.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure about stopping at the first overlay (worth a follow-up discussion), but it does seem that we should check that the overlay is not already closed before emitting.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO we should be doing it, because the primary use case for this dispatcher is closing when the user clicks outside of an overlay. If we were to dispatch to all overlays, it would mean that when the user has multiple dialogs open at the same time, clicking on the top one will close everything else.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suspect different use-cases would call for one or the other


// Dispatch the mouse event to the top overlay which has subscribers to its mouse events.
// We want to target all overlays for which the click could be considered as outside click.
// As soon as we reach an overlay for which the click is not outside click we break off
// the loop.
for (let i = overlays.length - 1; i > -1; i--) {
const overlayRef = overlays[i];
if (overlayRef._outsidePointerEvents.observers.length < 1) {
if (overlayRef._outsidePointerEvents.observers.length < 1 || !overlayRef.hasAttached()) {
continue;
}

Expand Down