Skip to content

Commit

Permalink
fix(snack-bar): Clear duration timeout on dismiss
Browse files Browse the repository at this point in the history
When a duration is passed to MdSnackBar.prototype.openFromComponent(),
a timeout is created. This timeout is now cleared when dismissing the
snackbar.

Fixes #4859
  • Loading branch information
svi3c committed Jun 7, 2017
1 parent 12d6e96 commit 80b38f7
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
9 changes: 9 additions & 0 deletions src/lib/snack-bar/snack-bar-ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ export class MdSnackBarRef<T> {
/** Subject for notifying the user that the snack bar action was called. */
private _onAction: Subject<any> = new Subject();

/** Timeout handle for the dismiss timeout cleanup. */
private _dismissTimeoutHandle: number;

constructor(instance: T,
containerInstance: MdSnackBarContainer,
private _overlayRef: OverlayRef) {
Expand All @@ -47,6 +50,12 @@ export class MdSnackBarRef<T> {
if (!this._afterClosed.closed) {
this.containerInstance.exit();
}
clearTimeout(this._dismissTimeoutHandle);
}

/** Dismisses the snack bar after some duration */
dismissAfter(duration: number): void {
this._dismissTimeoutHandle = setTimeout(() => this.dismiss(), duration);
}

/** Marks the snackbar action clicked. */
Expand Down
2 changes: 1 addition & 1 deletion src/lib/snack-bar/snack-bar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export class MdSnackBar {
// If a dismiss timeout is provided, set up dismiss based on after the snackbar is opened.
if (config.duration > 0) {
snackBarRef.afterOpened().subscribe(() => {
setTimeout(() => snackBarRef.dismiss(), config.duration);
snackBarRef.dismissAfter(config.duration);
});
}

Expand Down

0 comments on commit 80b38f7

Please sign in to comment.