File tree Expand file tree Collapse file tree 3 files changed +27
-1
lines changed Expand file tree Collapse file tree 3 files changed +27
-1
lines changed Original file line number Diff line number Diff line change @@ -31,6 +31,12 @@ export class MdSnackBarRef<T> {
31
31
/** Subject for notifying the user that the snack bar action was called. */
32
32
private _onAction : Subject < any > = new Subject ( ) ;
33
33
34
+ /**
35
+ * Timeout ID for the duration setTimeout call. Used to clear the timeout if the snackbar is
36
+ * dismissed before the duration passes.
37
+ */
38
+ private _durationTimeoutId : number ;
39
+
34
40
constructor ( instance : T ,
35
41
containerInstance : MdSnackBarContainer ,
36
42
private _overlayRef : OverlayRef ) {
@@ -47,6 +53,12 @@ export class MdSnackBarRef<T> {
47
53
if ( ! this . _afterClosed . closed ) {
48
54
this . containerInstance . exit ( ) ;
49
55
}
56
+ clearTimeout ( this . _durationTimeoutId ) ;
57
+ }
58
+
59
+ /** Dismisses the snack bar after some duration */
60
+ _dismissAfter ( duration : number ) : void {
61
+ this . _durationTimeoutId = setTimeout ( ( ) => this . dismiss ( ) , duration ) ;
50
62
}
51
63
52
64
/** Marks the snackbar action clicked. */
Original file line number Diff line number Diff line change @@ -328,6 +328,20 @@ describe('MdSnackBar', () => {
328
328
expect ( dismissObservableCompleted ) . toBeTruthy ( 'Expected the snack bar to be dismissed' ) ;
329
329
} ) ) ;
330
330
331
+ it ( 'should clear the dismiss timeout when dismissed before timeout expiration' , fakeAsync ( ( ) => {
332
+ let config = new MdSnackBarConfig ( ) ;
333
+ config . duration = 1000 ;
334
+ snackBar . open ( 'content' , 'test' , config ) ;
335
+
336
+ setTimeout ( ( ) => snackBar . dismiss ( ) , 500 ) ;
337
+
338
+ tick ( 600 ) ;
339
+ viewContainerFixture . detectChanges ( ) ;
340
+ flushMicrotasks ( ) ;
341
+
342
+ expect ( viewContainerFixture . isStable ( ) ) . toBe ( true ) ;
343
+ } ) ) ;
344
+
331
345
it ( 'should add extra classes to the container' , ( ) => {
332
346
snackBar . open ( simpleMessage , simpleActionLabel , {
333
347
viewContainerRef : testViewContainerRef ,
Original file line number Diff line number Diff line change @@ -81,7 +81,7 @@ export class MdSnackBar {
81
81
// If a dismiss timeout is provided, set up dismiss based on after the snackbar is opened.
82
82
if ( config . duration > 0 ) {
83
83
snackBarRef . afterOpened ( ) . subscribe ( ( ) => {
84
- setTimeout ( ( ) => snackBarRef . dismiss ( ) , config . duration ) ;
84
+ snackBarRef . _dismissAfter ( config . duration ) ;
85
85
} ) ;
86
86
}
87
87
You can’t perform that action at this time.
0 commit comments