Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clear duration timeout after dismiss #4860

Merged
merged 2 commits into from
Jun 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/lib/snack-bar/snack-bar-ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ export class MdSnackBarRef<T> {
/** Subject for notifying the user that the snack bar action was called. */
private _onAction: Subject<any> = new Subject();

/**
* Timeout ID for the duration setTimeout call. Used to clear the timeout if the snackbar is
* dismissed before the duration passes.
*/
private _durationTimeoutId: number;

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

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

/** Marks the snackbar action clicked. */
Expand Down
14 changes: 14 additions & 0 deletions src/lib/snack-bar/snack-bar.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,20 @@ describe('MdSnackBar', () => {
expect(dismissObservableCompleted).toBeTruthy('Expected the snack bar to be dismissed');
}));

it('should clear the dismiss timeout when dismissed before timeout expiration', fakeAsync(() => {
let config = new MdSnackBarConfig();
config.duration = 1000;
snackBar.open('content', 'test', config);

setTimeout(() => snackBar.dismiss(), 500);

tick(600);
viewContainerFixture.detectChanges();
flushMicrotasks();

expect(viewContainerFixture.isStable()).toBe(true);
}));

it('should add extra classes to the container', () => {
snackBar.open(simpleMessage, simpleActionLabel, { extraClasses: ['one', 'two'] });
viewContainerFixture.detectChanges();
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 @@ -89,7 +89,7 @@ export class MdSnackBar {
// If a dismiss timeout is provided, set up dismiss based on after the snackbar is opened.
if (config.duration && config.duration > 0) {
snackBarRef.afterOpened().subscribe(() => {
setTimeout(() => snackBarRef.dismiss(), config!.duration);
snackBarRef._dismissAfter(config!.duration!);
});
}

Expand Down