timeout-abort-controller
Version:
An AbortController that aborts after a specified timeout
48 lines (40 loc) • 846 B
JavaScript
const { AbortController } = globalThis
// @ts-expect-error no types
const retimer = require('retimer')
class TimeoutController extends AbortController {
/**
* @constructor
* @param {number} ms milliseconds
*/
constructor (ms) {
super()
this._ms = ms
this._timer = retimer(() => this.abort(), ms)
// Patch for safari not supported extending built in classes
Object.setPrototypeOf(this, TimeoutController.prototype)
}
/**
* Aborts the controller and clears the timer
*/
abort () {
this._timer.clear()
return super.abort()
}
/**
* Clears the timer
*/
clear () {
this._timer.clear()
}
/**
* Resets the timer
*/
reset () {
this._timer.clear()
this._timer = retimer(() => this.abort(), this._ms)
}
}
module.exports = {
TimeoutController
}