File tree Expand file tree Collapse file tree 2 files changed +22
-0
lines changed
Expand file tree Collapse file tree 2 files changed +22
-0
lines changed Original file line number Diff line number Diff line change @@ -8,6 +8,7 @@ class TimeoutController extends AbortController {
88 */
99 constructor ( ms ) {
1010 super ( )
11+ this . _ms = ms
1112 this . _timer = retimer ( ( ) => this . abort ( ) , ms )
1213 }
1314
@@ -25,6 +26,14 @@ class TimeoutController extends AbortController {
2526 clear ( ) {
2627 this . _timer . clear ( )
2728 }
29+
30+ /**
31+ * Resets the timer
32+ */
33+ reset ( ) {
34+ this . _timer . clear ( )
35+ this . _timer = retimer ( ( ) => this . abort ( ) , this . _ms )
36+ }
2837}
2938
3039module . exports = TimeoutController
Original file line number Diff line number Diff line change @@ -40,3 +40,16 @@ test('can clear the timeout', async t => {
4040
4141 t . is ( timeoutController . signal . aborted , false )
4242} )
43+
44+ test ( 'can reset the timeout' , async t => {
45+ const timeoutController = new TimeoutController ( 50 )
46+
47+ await delay ( 30 )
48+ timeoutController . reset ( ) // now expires at 80
49+
50+ await delay ( 30 )
51+ t . is ( timeoutController . signal . aborted , false ) // should not have expired at 60
52+
53+ await delay ( 30 )
54+ t . is ( timeoutController . signal . aborted , true ) // should have now expired at 90
55+ } )
You can’t perform that action at this time.
0 commit comments