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> {
3131 /** Subject for notifying the user that the snack bar action was called. */
3232 private _onAction : Subject < any > = new Subject ( ) ;
3333
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+
3440 constructor ( instance : T ,
3541 containerInstance : MdSnackBarContainer ,
3642 private _overlayRef : OverlayRef ) {
@@ -47,6 +53,12 @@ export class MdSnackBarRef<T> {
4753 if ( ! this . _afterClosed . closed ) {
4854 this . containerInstance . exit ( ) ;
4955 }
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 ) ;
5062 }
5163
5264 /** Marks the snackbar action clicked. */
Original file line number Diff line number Diff line change @@ -328,6 +328,20 @@ describe('MdSnackBar', () => {
328328 expect ( dismissObservableCompleted ) . toBeTruthy ( 'Expected the snack bar to be dismissed' ) ;
329329 } ) ) ;
330330
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+
331345 it ( 'should add extra classes to the container' , ( ) => {
332346 snackBar . open ( simpleMessage , simpleActionLabel , {
333347 viewContainerRef : testViewContainerRef ,
Original file line number Diff line number Diff line change @@ -81,7 +81,7 @@ export class MdSnackBar {
8181 // If a dismiss timeout is provided, set up dismiss based on after the snackbar is opened.
8282 if ( config . duration > 0 ) {
8383 snackBarRef . afterOpened ( ) . subscribe ( ( ) => {
84- setTimeout ( ( ) => snackBarRef . dismiss ( ) , config . duration ) ;
84+ snackBarRef . _dismissAfter ( config . duration ) ;
8585 } ) ;
8686 }
8787
You can’t perform that action at this time.
0 commit comments