Skip to content

Commit e1f00b9

Browse files
committed
feat: add timeout controller
0 parents  commit e1f00b9

File tree

8 files changed

+5868
-0
lines changed

8 files changed

+5868
-0
lines changed

.gitignore

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
yarn-debug.log*
6+
yarn-error.log*
7+
8+
# Runtime data
9+
pids
10+
*.pid
11+
*.seed
12+
*.pid.lock
13+
14+
# Directory for instrumented libs generated by jscoverage/JSCover
15+
lib-cov
16+
17+
# Coverage directory used by tools like istanbul
18+
coverage
19+
20+
# nyc test coverage
21+
.nyc_output
22+
23+
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
24+
.grunt
25+
26+
# Bower dependency directory (https://bower.io/)
27+
bower_components
28+
29+
# node-waf configuration
30+
.lock-wscript
31+
32+
# Compiled binary addons (https://nodejs.org/api/addons.html)
33+
build/Release
34+
35+
# Dependency directories
36+
node_modules/
37+
jspm_packages/
38+
39+
# TypeScript v1 declaration files
40+
typings/
41+
42+
# Optional npm cache directory
43+
.npm
44+
45+
# Optional eslint cache
46+
.eslintcache
47+
48+
# Optional REPL history
49+
.node_repl_history
50+
51+
# Output of 'npm pack'
52+
*.tgz
53+
54+
# Yarn Integrity file
55+
.yarn-integrity
56+
57+
# dotenv environment variables file
58+
.env
59+
60+
# next.js build output
61+
.next

.travis.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
language: node_js
2+
node_js:
3+
- "stable"

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2019 Jacob Heun
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# timeout-controller
2+
3+
[![Build Status](https://travis-ci.org/jacobheun/timeout-controller.svg?branch=master)](https://travis-ci.org/jacobheun/timeout-controller) [![dependencies Status](https://david-dm.org/jacobheun/timeout-controller/status.svg)](https://david-dm.org/jacobheun/timeout-controller) [![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)
4+
5+
> An AbortController that aborts after a specified timeout.
6+
7+
`timeout-controller` uses [`retimer`](https://github.com/mcollina/retimer) internally to help reduce the impact of having numerous timers running.
8+
9+
## Install
10+
11+
```
12+
npm install timeout-controller
13+
```
14+
15+
## Usage
16+
17+
```js
18+
const AbortController = require('abort-controller')
19+
const TimeoutController = require('timeout-controller')
20+
const anySignal = require('any-signal')
21+
22+
const userController = new AbortController()
23+
// Aborts after 1 second
24+
const timeoutController = new TimeoutController(1000)
25+
26+
const combinedSignal = anySignal([userController.signal, timeoutController.signal])
27+
combinedSignal.addEventListener('abort', () => console.log('Abort!'))
28+
29+
// The user or the timeout can now abort the action
30+
await performSomeAction({ signal: combinedSignal })
31+
timeoutController.clear()
32+
```
33+
34+
## API
35+
36+
### `new TimeoutController(ms)`
37+
38+
Creates an [`AbortController`](https://developer.mozilla.org/en-US/docs/Web/API/AbortController) compliant `TimeoutController`.
39+
40+
#### Parameters
41+
42+
| Name | Type | Description |
43+
|------|------|-------------|
44+
| ms | number | The time in milliseconds for when the TimeoutController should abort |
45+
46+
### `timeoutController.clear()`
47+
48+
Clears the internal timer.
49+
50+
### `timeoutController.abort()`
51+
52+
Aborts the controller and clears the internal timeout.
53+
54+
## Related
55+
56+
[`abort-controller`](https://github.com/mysticatea/abort-controller)
57+
[`any-signal`](https://github.com/jacobheun/any-signal) Combines an array of AbortSignals into a single signal.
58+
59+
## LICENSE
60+
61+
[MIT](LICENSE) © Jacob Heun

index.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
const AbortController = require('abort-controller')
2+
const retimer = require('retimer')
3+
4+
class TimeoutController extends AbortController {
5+
/**
6+
* @constructor
7+
* @param {number} ms milliseconds
8+
*/
9+
constructor (ms) {
10+
super()
11+
this._timer = retimer(() => this.abort(), ms)
12+
}
13+
14+
/**
15+
* Aborts the controller and clears the timer
16+
*/
17+
abort () {
18+
this._timer.clear()
19+
return super.abort()
20+
}
21+
22+
/**
23+
* Clears the timer
24+
*/
25+
clear () {
26+
this._timer.clear()
27+
}
28+
}
29+
30+
module.exports = TimeoutController
31+
module.exports.TimeoutController = TimeoutController

0 commit comments

Comments
 (0)