Skip to content

Commit

Permalink
(fix): make sure no leaking subscription
Browse files Browse the repository at this point in the history
make sure subscription on fromEvent observable is unsubscribed when the
component is destroyed
  • Loading branch information
erhise committed Oct 1, 2018
1 parent 8ed0129 commit be2df73
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/lib/sidenav/drawer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,9 @@ export class MatDrawer implements AfterContentInit, AfterContentChecked, OnDestr
);
}

/** Emits when the component is destroyed. */
private readonly _destroyed = new Subject<void>();

/** Event emitted when the drawer's position changes. */
// tslint:disable-next-line:no-output-on-prefix
@Output('positionChanged') onPositionChanged: EventEmitter<void> = new EventEmitter<void>();
Expand Down Expand Up @@ -249,7 +252,8 @@ export class MatDrawer implements AfterContentInit, AfterContentChecked, OnDestr
*/
this._ngZone.runOutsideAngular(() => {
fromEvent<KeyboardEvent>(this._elementRef.nativeElement, 'keydown').pipe(
filter(event => event.keyCode === ESCAPE && !this.disableClose)
filter(event => event.keyCode === ESCAPE && !this.disableClose),
takeUntil(this._destroyed)
).subscribe(event => this._ngZone.run(() => {
this.close();
event.stopPropagation();
Expand Down Expand Up @@ -314,6 +318,7 @@ export class MatDrawer implements AfterContentInit, AfterContentChecked, OnDestr
if (this._focusTrap) {
this._focusTrap.destroy();
}
this._destroyed.next();
}

/**
Expand Down

0 comments on commit be2df73

Please sign in to comment.