Skip to content

fix(dialog): DOM nodes not cleaned up if view container is destroyed mid-animation #16309

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
Jun 26, 2019
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
19 changes: 17 additions & 2 deletions src/material/dialog/dialog-ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ export class MatDialogRef<T, R = any> {
/** Result to be passed to afterClosed. */
private _result: R | undefined;

/** Handle to the timeout that's running as a fallback in case the exit animation doesn't fire. */
private _closeFallbackTimeout: number;

constructor(
private _overlayRef: OverlayRef,
public _containerInstance: MatDialogContainer,
Expand All @@ -66,7 +69,10 @@ export class MatDialogRef<T, R = any> {
_containerInstance._animationStateChanged.pipe(
filter(event => event.phaseName === 'done' && event.toState === 'exit'),
take(1)
).subscribe(() => this._overlayRef.dispose());
).subscribe(() => {
clearTimeout(this._closeFallbackTimeout);
this._overlayRef.dispose();
});

_overlayRef.detachments().subscribe(() => {
this._beforeClosed.next(this._result);
Expand Down Expand Up @@ -99,10 +105,19 @@ export class MatDialogRef<T, R = any> {
filter(event => event.phaseName === 'start'),
take(1)
)
.subscribe(() => {
.subscribe(event => {
this._beforeClosed.next(dialogResult);
this._beforeClosed.complete();
this._overlayRef.detachBackdrop();

// The logic that disposes of the overlay depends on the exit animation completing, however
// it isn't guaranteed if the parent view is destroyed while it's running. Add a fallback
// timeout which will clean everything up if the animation hasn't fired within the specified
// amount of time plus 100ms. We don't need to run this outside the NgZone, because for the
// vast majority of cases the timeout will have been cleared before it has the chance to fire.
this._closeFallbackTimeout = setTimeout(() => {
this._overlayRef.dispose();
}, event.totalTime + 100);
});

this._containerInstance._startExitAnimation();
Expand Down
12 changes: 12 additions & 0 deletions src/material/dialog/dialog.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,17 @@ describe('MatDialog', () => {
expect(overlayContainerElement.querySelector('mat-dialog-container')).toBeNull();
}));

it('should dispose of dialog if view container is destroyed while animating', fakeAsync(() => {
const dialogRef = dialog.open(PizzaMsg, {viewContainerRef: testViewContainerRef});

dialogRef.close();
viewContainerFixture.detectChanges();
viewContainerFixture.destroy();
flush();

expect(overlayContainerElement.querySelector('mat-dialog-container')).toBeNull();
}));

it('should dispatch the beforeClose and afterClose events when the ' +
'overlay is detached externally', fakeAsync(inject([Overlay], (overlay: Overlay) => {
const dialogRef = dialog.open(PizzaMsg, {
Expand Down Expand Up @@ -1064,6 +1075,7 @@ describe('MatDialog', () => {

document.body.removeChild(button);
document.body.removeChild(input);
flush();
}));

it('should move focus to the container if there are no focusable elements in the dialog',
Expand Down