UNPKG

wait-utils

Version:

A modern, zero-dependency wait / timing utility toolkit for JavaScript and TypeScript.

3 lines 3.35 kB
/*! @license wait-utils | MIT | https://github.com/havelessbemore/wait-utils */ "use strict";var S=Object.defineProperty;var k=Object.getOwnPropertyDescriptor;var h=Object.getOwnPropertyNames;var I=Object.prototype.hasOwnProperty;var O=(o,t)=>{for(var e in t)S(o,e,{get:t[e],enumerable:!0})},E=(o,t,e,n)=>{if(t&&typeof t=="object"||typeof t=="function")for(let r of h(t))!I.call(o,r)&&r!==e&&S(o,r,{get:()=>t[r],enumerable:!(n=k(t,r))||n.enumerable});return o};var D=o=>E(S({},"__esModule",{value:!0}),o);var j={};O(j,{AbortError:()=>x,TimeoutError:()=>b,poll:()=>T,setIntervalAsync:()=>p,setTimeoutAsync:()=>f,timeout:()=>w,wait:()=>y,waitUntil:()=>C});module.exports=D(j);var x=class extends DOMException{constructor(t="This operation was aborted"){super(t,"AbortError"),Object.setPrototypeOf(this,new.target.prototype)}};var b=class extends DOMException{constructor(t="This operation was timed out"){super(t,"TimeoutError"),Object.setPrototypeOf(this,new.target.prototype)}};function p(o,t,e){return new Promise((n,r)=>{if(e?.aborted){r(e.reason);return}let i=!0,l=0,a={tickCount:0,delay:t};Object.defineProperty(a,"tickCount",{configurable:!1,enumerable:!0,get:()=>l});let u=()=>{i&&(i=!1,clearTimeout(s),r(e?.reason))},A=v=>{i&&(i=!1,e?.removeEventListener("abort",u),r(v))},d=()=>{i&&(i=!1,e?.removeEventListener("abort",u),n())},m=async()=>{if(i)try{++l,await o(a),a.stop===!0?d():i&&(s=setTimeout(m,a.delay))}catch(v){A(v)}},s=setTimeout(m,a.delay);e?.addEventListener("abort",u,{once:!0})})}function f(o,t){return t==null?new Promise(e=>setTimeout(e,o)):new Promise((e,n)=>{if(t.aborted){n(t.reason);return}let r=()=>{clearTimeout(l),n(t.reason)},l=setTimeout(()=>{t.removeEventListener("abort",r),e()},o);t.addEventListener("abort",r,{once:!0})})}var R="throwIfAborted"in AbortSignal.prototype,c=R?F:L;function F(o){o?.throwIfAborted()}function L(o){if(o?.aborted)throw o.reason}async function y(o,t){if(c(t),o!=null&&(o=Number(o),!Number.isNaN(o)&&o>0))return f(o,t)}function w(o,t){return y(o,t).then(()=>{throw new b},e=>{if(t?.reason!==e)throw e})}function P(o,t){return Object.prototype.hasOwnProperty.call(o,t)}var N="any"in AbortSignal,g=N?_:H;function _(o,t){return t==null?o:o==null?t:AbortSignal.any([o,t])}function H(o,t){if(t==null)return o;if(o==null)return t;let e=new AbortController,n=()=>{e.signal.aborted||(e.abort(o.reason),t.removeEventListener("abort",r))},r=()=>{e.signal.aborted||(e.abort(t.reason),o.removeEventListener("abort",n))};return o.aborted?e.abort(o.reason):t.aborted?e.abort(t.reason):(o.addEventListener("abort",n,{once:!0}),t.addEventListener("abort",r,{once:!0})),e.signal}async function T(o,t={}){c(t.signal);let{delay:e,afterPoll:n,userData:r}=t,i=0,l={attempt:0,delay:e,userData:r};Object.defineProperty(l,"attempt",{configurable:!1,enumerable:!0,get:()=>i});let a=t.signal,u,A=async s=>{++i,u=await o(l),!a?.aborted&&(s.stop=l.stop,s.stop!==!0&&(await n?.(l),s.delay=l.delay??0,s.stop=l.stop))},d=P(t,"initialDelay")?t.initialDelay:e;if(t.timeout==null)return await p(A,d??0,a),u;let m=new AbortController;a=g(t.signal,m.signal);try{return await Promise.race([w(t.timeout,a),p(A,d??0,a)]),u}finally{m.abort()}}async function C(o,t){if(c(t),o==null)return;let e=o instanceof Date?o.getTime()-Date.now():Number(o)-performance.now();!Number.isNaN(e)&&e>0&&await f(e,t)} //# sourceMappingURL=index.cjs.map