UNPKG

@jingoz/enhanced-abort-controller

Version:

EnhancedAbortController 是一种具有超时功能的增强版本的 AbortController,使用 TypeScript 编写。

2 lines (1 loc) 1.12 kB
(function(t,e){typeof exports=="object"&&typeof module<"u"?e(exports):typeof define=="function"&&define.amd?define(["exports"],e):(t=typeof globalThis<"u"?globalThis:t||self,e(t.enhancedAbortController={}))})(this,function(t){"use strict";var r=Object.defineProperty;var n=(t,e,o)=>e in t?r(t,e,{enumerable:!0,configurable:!0,writable:!0,value:o}):t[e]=o;var s=(t,e,o)=>n(t,typeof e!="symbol"?e+"":e,o);class e extends AbortController{constructor(i){super();s(this,"_isDisposed",!1);s(this,"_timer",null);s(this,"_onAbort",()=>{this._clearTimeout()});this.signal.addEventListener("abort",this._onAbort),i&&this._createTimeout(i)}_createTimeout(i){this._throwIfDisposed(),this.signal.throwIfAborted(),this._clearTimeout(),this._timer=setTimeout(()=>this.abort(),i)}_clearTimeout(){this._timer&&(clearTimeout(this._timer),this._timer=null)}_throwIfDisposed(){if(this._isDisposed)throw new Error("EnhancedAbortController instance is disposed")}abortAfter(i){this._createTimeout(i)}destroy(){this._isDisposed||(this._isDisposed=!0)}}t.EnhancedAbortController=e,Object.defineProperty(t,Symbol.toStringTag,{value:"Module"})});