Skip to content

Commit 2ae1da1

Browse files
committed
Update code
Signed-off-by: Richie Bendall <[email protected]>
1 parent caeca05 commit 2ae1da1

File tree

5 files changed

+68
-49
lines changed

5 files changed

+68
-49
lines changed

README.md

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,38 @@
1-
# the-module [![Travis CI Build Status](https://img.shields.io/travis/com/Richienb/the-module/master.svg?style=for-the-badge)](https://travis-ci.com/Richienb/the-module)
1+
# timeout-signal [![Travis CI Build Status](https://img.shields.io/travis/com/Richienb/timeout-signal/master.svg?style=for-the-badge)](https://travis-ci.com/Richienb/timeout-signal)
22

3-
My awesome module.
3+
Create an AbortSignal that aborts after a delay.
44

5-
[![NPM Badge](https://nodei.co/npm/the-module.png)](https://npmjs.com/package/the-module)
5+
[![NPM Badge](https://nodei.co/npm/timeout-signal.png)](https://npmjs.com/package/timeout-signal)
66

77
## Install
88

99
```sh
10-
npm install the-module
10+
npm install timeout-signal
1111
```
1212

1313
## Usage
1414

1515
```js
16-
const theModule = require("the-module");
17-
18-
theModule("unicorns");
19-
//=> 'unicorns & rainbows'
16+
const timeoutSignal = require("timeout-signal");
17+
const fetch = require("cross-fetch");
18+
19+
fetch("https://www.google.com", { signal: timeoutSignal(5000) })
20+
.then(response => {
21+
// Handle response
22+
})
23+
.catch(error => {
24+
if (error.message === "The user aborted a request.") {
25+
// Handle abortion
26+
}
27+
})
2028
```
2129

2230
## API
2331

24-
### theModule(input, options?)
25-
26-
#### input
27-
28-
Type: `string`
29-
30-
Lorem ipsum.
31-
32-
#### options
33-
34-
Type: `object`
32+
### timeoutSignal(timeout)
3533

36-
##### postfix
34+
#### timeout
3735

38-
Type: `string`\
39-
Default: `rainbows`
36+
Type: `integer`
4037

41-
Lorem ipsum.
38+
The milliseconds to wait.

index.d.ts

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,24 @@
1+
import { AbortSignal } from "abort-controller"
2+
13
/**
2-
My awesome module.
3-
@param input Lorem ipsum.
4-
@param postfix Lorem ipsum.
4+
Create an AbortSignal that aborts after a delay.
5+
@param timeout The milliseconds to wait.
56
@example
67
```
7-
const theModule = require("the-module");
8+
const timeoutSignal = require("timeout-signal");
9+
const fetch = require("cross-fetch");
810
9-
theModule("unicorns");
10-
//=> 'unicorns & rainbows'
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+
})
1120
```
1221
*/
13-
declare function theModule(input: string, { postfix }: { postfix?: string }): string
22+
declare function timeoutSignal(timeout: number): AbortSignal
1423

15-
export = theModule
24+
export = timeoutSignal

index.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
11
"use strict"
22

3-
module.exports = (input, { postfix = "rainbows" } = {}) => {
4-
if (typeof input !== "string") {
5-
throw new TypeError(`Expected a string, got ${typeof input}`)
3+
const { AbortController } = require("abort-controller")
4+
5+
module.exports = timeout => {
6+
if (!Number.isInteger(timeout)) {
7+
throw new TypeError(`Expected an integer, got ${typeof timeout}`)
68
}
79

8-
return `${input} & ${postfix}`
10+
const controller = new AbortController()
11+
12+
setTimeout(() => {
13+
controller.abort()
14+
}, timeout)
15+
16+
return controller.signal
917
}

package.json

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
2-
"name": "the-module",
2+
"name": "timeout-signal",
33
"version": "0.0.0",
4-
"description": "My awesome module.",
5-
"repository": "https://github.com/Richienb/the-module.git",
4+
"description": "Create an AbortSignal that aborts after a delay.",
5+
"repository": "https://github.com/Richienb/timeout-signal.git",
66
"author": "Richie Bendall <[email protected]>",
77
"license": "MIT",
88
"main": "index.js",
@@ -17,11 +17,18 @@
1717
"lint": "xo",
1818
"test": "xo && ava"
1919
},
20-
"keywords": [],
21-
"dependencies": {},
20+
"keywords": [
21+
"timeout",
22+
"signal",
23+
"abort"
24+
],
25+
"dependencies": {
26+
"abort-controller": "^3.0.0"
27+
},
2228
"devDependencies": {
2329
"ava": "^3.8.1",
2430
"eslint-config-richienb": "^0.4.2",
31+
"p-event": "^4.1.0",
2532
"xo": "^0.30.0"
2633
},
2734
"resolutions": {

test.js

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
const test = require("ava")
2-
const theModule = require(".")
2+
const pEvent = require("p-event")
3+
const timeoutSignal = require(".")
34

4-
test("main", t => {
5-
t.throws(() => {
6-
theModule(123)
7-
}, {
8-
instanceOf: TypeError,
9-
message: "Expected a string, got number"
10-
})
5+
test("main", async t => {
6+
const signal = timeoutSignal(0)
117

12-
t.is(theModule("unicorns"), "unicorns & rainbows")
8+
await pEvent(signal, "abort")
9+
10+
t.pass()
1311
})

0 commit comments

Comments
 (0)