fix(menu): complete close stream on destroy (#5368)

This is something I noticed while working on the nested menus: We're subscribing to the `close` event internally inside the menu trigger, but we're never unsubscribing. These changes complete the observable so we don't have to unsubscribe manually.
This commit is contained in:
Kristiyan Kostadinov
2017-07-09 03:48:58 +02:00
committed by mmalerba
parent 8817b4da8d
commit d810138475
2 changed files with 17 additions and 1 deletions
+3
View File
@@ -116,6 +116,9 @@ export class MdMenu implements AfterContentInit, MdMenuPanel, OnDestroy {
if (this._tabSubscription) {
this._tabSubscription.unsubscribe();
}
this._emitCloseEvent();
this.close.complete();
}
/** Handle a keyboard event from the menu, delegating to the appropriate action. */
+14 -1
View File
@@ -15,7 +15,8 @@ import {
MdMenuTrigger,
MdMenuPanel,
MenuPositionX,
MenuPositionY
MenuPositionY,
MdMenu
} from './index';
import {OverlayContainer} from '../core/overlay/overlay-container';
import {Directionality, Direction} from '../core/bidi/index';
@@ -477,6 +478,17 @@ describe('MdMenu', () => {
expect(fixture.componentInstance.closeCallback).toHaveBeenCalled();
});
it('should complete the callback when the menu is destroyed', () => {
let emitCallback = jasmine.createSpy('emit callback');
let completeCallback = jasmine.createSpy('complete callback');
fixture.componentInstance.menu.close.subscribe(emitCallback, null, completeCallback);
fixture.destroy();
expect(emitCallback).toHaveBeenCalled();
expect(completeCallback).toHaveBeenCalled();
});
});
describe('destroy', () => {
@@ -499,6 +511,7 @@ describe('MdMenu', () => {
class SimpleMenu {
@ViewChild(MdMenuTrigger) trigger: MdMenuTrigger;
@ViewChild('triggerEl') triggerEl: ElementRef;
@ViewChild(MdMenu) menu: MdMenu;
closeCallback = jasmine.createSpy('menu closed callback');
}