Skip to content

Commit

Permalink
fix(menu): complete close stream on destroy (#5368)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
crisbeto authored and mmalerba committed Jul 9, 2017
1 parent 8817b4d commit d810138
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/lib/menu/menu-directive.ts
Expand Up @@ -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. */
Expand Down
15 changes: 14 additions & 1 deletion src/lib/menu/menu.spec.ts
Expand Up @@ -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';
Expand Down Expand Up @@ -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', () => {
Expand All @@ -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');
}

Expand Down

0 comments on commit d810138

Please sign in to comment.