Skip to content

Commit 938c2b6

Browse files
committed
Add .clear() method
Fixes: #1 Signed-off-by: Richie Bendall <[email protected]>
1 parent 4a8e7a3 commit 938c2b6

File tree

5 files changed

+100
-20
lines changed

5 files changed

+100
-20
lines changed

README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,31 @@ fetch("https://www.google.com", { signal: timeoutSignal(5000) })
3636
Type: `integer`
3737

3838
The milliseconds to wait.
39+
40+
### timeoutSignal.clear(signal)
41+
42+
Clear the timeout associated with a signal.
43+
44+
#### signal
45+
46+
Type: Return value from `timeoutSignal()`
47+
48+
The signal to clear the timeout for.
49+
50+
```js
51+
const timeoutSignal = require("timeout-signal");
52+
const fetch = require("cross-fetch");
53+
54+
const signal = timeoutSignal(5000)
55+
56+
fetch("https://www.google.com", { signal })
57+
.then(response => {
58+
timeoutSignal.clear(signal)
59+
// Handle response
60+
})
61+
.catch(error => {
62+
if (error.message === "The user aborted a request.") {
63+
// Handle abortion
64+
}
65+
})
66+
```

index.d.ts

Lines changed: 45 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,50 @@
11
import { AbortSignal } from "abort-controller"
22

3-
/**
4-
Create an AbortSignal that aborts after a delay.
5-
@param timeout The milliseconds to wait.
6-
@example
7-
```
8-
const timeoutSignal = require("timeout-signal");
9-
const fetch = require("cross-fetch");
3+
declare const timeoutSignal: {
4+
/**
5+
Create an AbortSignal that aborts after a delay.
6+
@param timeout The milliseconds to wait.
7+
@example
8+
```
9+
const timeoutSignal = require("timeout-signal");
10+
const fetch = require("cross-fetch");
1011
11-
fetch("https://www.google.com", { signal: timeoutSignal(5000) })
12-
.then(response => {
13-
// Handle response
14-
})
15-
.catch(error => {
16-
if (error.message === "The user aborted a request.") {
17-
// Handle abortion
18-
}
19-
})
20-
```
21-
*/
22-
declare function timeoutSignal(timeout: number): AbortSignal
12+
fetch("https://www.google.com", { signal: timeoutSignal(5000) })
13+
.then(response => {
14+
// Handle response
15+
})
16+
.catch(error => {
17+
if (error.message === "The user aborted a request.") {
18+
// Handle abortion
19+
}
20+
})
21+
```
22+
*/
23+
(timeout: number): AbortSignal
24+
25+
/**
26+
The signal to clear the timeout for.
27+
@param signal The signal to clear the timeout for.
28+
@example
29+
```
30+
const timeoutSignal = require("timeout-signal");
31+
const fetch = require("cross-fetch");
32+
33+
const signal = timeoutSignal(5000)
34+
35+
fetch("https://www.google.com", { signal })
36+
.then(response => {
37+
timeoutSignal.clear(signal)
38+
// Handle response
39+
})
40+
.catch(error => {
41+
if (error.message === "The user aborted a request.") {
42+
// Handle abortion
43+
}
44+
})
45+
```
46+
*/
47+
clear(signal: AbortSignal): void
48+
}
2349

2450
export = timeoutSignal

index.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,24 @@
22

33
const { AbortController } = require("abort-controller")
44

5+
const signalMap = new WeakMap()
6+
57
module.exports = timeout => {
68
if (!Number.isInteger(timeout)) {
79
throw new TypeError(`Expected an integer, got ${typeof timeout}`)
810
}
911

1012
const controller = new AbortController()
1113

12-
setTimeout(() => {
14+
const timeoutId = setTimeout(() => {
1315
controller.abort()
1416
}, timeout)
1517

18+
signalMap.set(controller.signal, timeoutId)
19+
1620
return controller.signal
1721
}
22+
23+
module.exports.clear = signal => {
24+
clearTimeout(signalMap.get(signal))
25+
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
},
2828
"devDependencies": {
2929
"ava": "^3.8.1",
30+
"delay": "^4.4.0",
3031
"eslint-config-richienb": "^0.4.2",
3132
"p-event": "^4.1.0",
3233
"xo": "^0.30.0"

test.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const test = require("ava")
22
const pEvent = require("p-event")
3+
const delay = require("delay")
34
const timeoutSignal = require(".")
45

56
test("main", async t => {
@@ -9,3 +10,19 @@ test("main", async t => {
910

1011
t.pass()
1112
})
13+
14+
test(".clear()", async t => {
15+
const signal = timeoutSignal(250)
16+
17+
timeoutSignal.clear(signal)
18+
19+
let hasAborted = false
20+
21+
signal.addEventListener("abort", () => {
22+
hasAborted = true
23+
})
24+
25+
await delay(500)
26+
27+
t.false(hasAborted)
28+
})

0 commit comments

Comments
 (0)