Skip to content

Commit 25f73af

Browse files
Alan Shawjacobheun
authored andcommitted
feat: add reset method (#1)
Allows us to implement rolling window timeouts. i.e. timeout 10s after last received byte, not after 10s and still recieving data.
1 parent 2916b50 commit 25f73af

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

index.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff 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

3039
module.exports = TimeoutController

test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff 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+
})

0 commit comments

Comments
 (0)