wait-utils
Version:
A modern, zero-dependency wait / timing utility toolkit for JavaScript and TypeScript.
3 lines • 2.89 kB
JavaScript
/*! @license wait-utils | MIT | https://github.com/havelessbemore/wait-utils */
var y=class extends DOMException{constructor(t="This operation was aborted"){super(t,"AbortError"),Object.setPrototypeOf(this,new.target.prototype)}};var c=class extends DOMException{constructor(t="This operation was timed out"){super(t,"TimeoutError"),Object.setPrototypeOf(this,new.target.prototype)}};function d(o,t,e){return new Promise((a,n)=>{if(e?.aborted){n(e.reason);return}let r=!0,i=0,l={tickCount:0,delay:t};Object.defineProperty(l,"tickCount",{configurable:!1,enumerable:!0,get:()=>i});let u=()=>{r&&(r=!1,clearTimeout(s),n(e?.reason))},p=x=>{r&&(r=!1,e?.removeEventListener("abort",u),n(x))},A=()=>{r&&(r=!1,e?.removeEventListener("abort",u),a())},f=async()=>{if(r)try{++i,await o(l),l.stop===!0?A():r&&(s=setTimeout(f,l.delay))}catch(x){p(x)}},s=setTimeout(f,l.delay);e?.addEventListener("abort",u,{once:!0})})}function m(o,t){return t==null?new Promise(e=>setTimeout(e,o)):new Promise((e,a)=>{if(t.aborted){a(t.reason);return}let n=()=>{clearTimeout(i),a(t.reason)},i=setTimeout(()=>{t.removeEventListener("abort",n),e()},o);t.addEventListener("abort",n,{once:!0})})}var g="throwIfAborted"in AbortSignal.prototype,b=g?T:C;function T(o){o?.throwIfAborted()}function C(o){if(o?.aborted)throw o.reason}async function w(o,t){if(b(t),o!=null&&(o=Number(o),!Number.isNaN(o)&&o>0))return m(o,t)}function v(o,t){return w(o,t).then(()=>{throw new c},e=>{if(t?.reason!==e)throw e})}function S(o,t){return Object.prototype.hasOwnProperty.call(o,t)}var k="any"in AbortSignal,P=k?h:I;function h(o,t){return t==null?o:o==null?t:AbortSignal.any([o,t])}function I(o,t){if(t==null)return o;if(o==null)return t;let e=new AbortController,a=()=>{e.signal.aborted||(e.abort(o.reason),t.removeEventListener("abort",n))},n=()=>{e.signal.aborted||(e.abort(t.reason),o.removeEventListener("abort",a))};return o.aborted?e.abort(o.reason):t.aborted?e.abort(t.reason):(o.addEventListener("abort",a,{once:!0}),t.addEventListener("abort",n,{once:!0})),e.signal}async function O(o,t={}){b(t.signal);let{delay:e,afterPoll:a,userData:n}=t,r=0,i={attempt:0,delay:e,userData:n};Object.defineProperty(i,"attempt",{configurable:!1,enumerable:!0,get:()=>r});let l=t.signal,u,p=async s=>{++r,u=await o(i),!l?.aborted&&(s.stop=i.stop,s.stop!==!0&&(await a?.(i),s.delay=i.delay??0,s.stop=i.stop))},A=S(t,"initialDelay")?t.initialDelay:e;if(t.timeout==null)return await d(p,A??0,l),u;let f=new AbortController;l=P(t.signal,f.signal);try{return await Promise.race([v(t.timeout,l),d(p,A??0,l)]),u}finally{f.abort()}}async function E(o,t){if(b(t),o==null)return;let e=o instanceof Date?o.getTime()-Date.now():Number(o)-performance.now();!Number.isNaN(e)&&e>0&&await m(e,t)}export{y as AbortError,c as TimeoutError,O as poll,d as setIntervalAsync,m as setTimeoutAsync,v as timeout,w as wait,E as waitUntil};
//# sourceMappingURL=index.js.map