Skip to content

Commit

Permalink
fix(dialog): clean up open dialogs on destroy (#12835)
Browse files Browse the repository at this point in the history
Closes all of the open dialogs when `MatDialog` is destroyed.
  • Loading branch information
crisbeto authored and mmalerba committed Sep 18, 2018
1 parent 2b89342 commit 4e15ba9
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 10 deletions.
30 changes: 30 additions & 0 deletions src/lib/dialog/dialog.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,20 @@ describe('MatDialog', () => {
expect(overlayContainerElement.querySelectorAll('mat-dialog-container').length).toBe(0);
}));

it('should close all of the dialogs when the injectable is destroyed', fakeAsync(() => {
dialog.open(PizzaMsg);
dialog.open(PizzaMsg);
dialog.open(PizzaMsg);

expect(overlayContainerElement.querySelectorAll('mat-dialog-container').length).toBe(3);

dialog.ngOnDestroy();
viewContainerFixture.detectChanges();
flush();

expect(overlayContainerElement.querySelectorAll('mat-dialog-container').length).toBe(0);
}));

it('should allow the consumer to disable closing a dialog on navigation', fakeAsync(() => {
dialog.open(PizzaMsg);
dialog.open(PizzaMsg, {closeOnNavigation: false});
Expand Down Expand Up @@ -1232,6 +1246,22 @@ describe('MatDialog with a parent MatDialog', () => {

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

it('should not close the parent dialogs when a child is destroyed', fakeAsync(() => {
parentDialog.open(PizzaMsg);
fixture.detectChanges();
flush();

expect(overlayContainerElement.textContent)
.toContain('Pizza', 'Expected a dialog to be opened');

childDialog.ngOnDestroy();
fixture.detectChanges();
flush();

expect(overlayContainerElement.textContent)
.toContain('Pizza', 'Expected a dialog to be opened');
}));
});

describe('MatDialog with default options', () => {
Expand Down
31 changes: 21 additions & 10 deletions src/lib/dialog/dialog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
Injectable,
InjectionToken,
Injector,
OnDestroy,
Optional,
SkipSelf,
TemplateRef,
Expand Down Expand Up @@ -66,7 +67,7 @@ export const MAT_DIALOG_SCROLL_STRATEGY_PROVIDER = {
* Service to open Material Design modal dialogs.
*/
@Injectable()
export class MatDialog {
export class MatDialog implements OnDestroy {
private _openDialogsAtThisLevel: MatDialogRef<any>[] = [];
private readonly _afterAllClosedAtThisLevel = new Subject<void>();
private readonly _afterOpenedAtThisLevel = new Subject<MatDialogRef<any>>();
Expand Down Expand Up @@ -152,15 +153,7 @@ export class MatDialog {
* Closes all of the currently-open dialogs.
*/
closeAll(): void {
let i = this.openDialogs.length;

while (i--) {
// The `_openDialogs` property isn't updated after close until the rxjs subscription
// runs on the next microtask, in addition to modifying the array as we're going
// through it. We loop through all of them and call close without assuming that
// they'll be removed from the list instantaneously.
this.openDialogs[i].close();
}
this._closeDialogs(this.openDialogs);
}

/**
Expand All @@ -171,6 +164,12 @@ export class MatDialog {
return this.openDialogs.find(dialog => dialog.id === id);
}

ngOnDestroy() {
// Only close the dialogs at this level on destroy
// since the parent service may still be active.
this._closeDialogs(this._openDialogsAtThisLevel);
}

/**
* Creates the overlay into which the dialog will be loaded.
* @param config The dialog configuration.
Expand Down Expand Up @@ -357,7 +356,19 @@ export class MatDialog {
}
}
}
}

/** Closes all of the dialogs in an array. */
private _closeDialogs(dialogs: MatDialogRef<any>[]) {
let i = dialogs.length;

while (i--) {
// The `_openDialogs` property isn't updated after close until the rxjs subscription
// runs on the next microtask, in addition to modifying the array as we're going
// through it. We loop through all of them and call close without assuming that
// they'll be removed from the list instantaneously.
dialogs[i].close();
}
}

}
Expand Down

0 comments on commit 4e15ba9

Please sign in to comment.