timeout-flow 0.0.19 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/ADDITIONAL_TERMS.md +122 -122
- package/LICENSE +694 -694
- package/README.md +475 -307
- package/dist/timeout-flow.min.js +1 -1
- package/package.json +79 -68
- package/robots.txt +79 -79
- package/src/AfterTimer.js +81 -81
- package/src/EveryTimer.js +208 -208
- package/src/TimeoutFlow.js +400 -400
- package/src/TimerBase.js +137 -132
- package/src/abort.js +87 -75
- package/src/after.js +32 -32
- package/src/debounce.js +105 -105
- package/src/every.js +34 -34
- package/src/index.js +24 -23
- package/src/now.js +22 -22
- package/src/parseDuration.js +43 -46
- package/src/raf/ReadMe-Raf.md +94 -94
- package/src/raf/afterRaf.js +203 -203
- package/src/raf/debounceRaf.js +126 -126
- package/src/raf/everyRaf.js +314 -316
- package/src/raf/index.js +27 -27
- package/src/raf/throttleRaf.js +181 -181
- package/src/raf/waitForRaf.js +100 -100
- package/src/resolveDelayAndFn.js +65 -89
- package/src/retry.js +205 -164
- package/src/sleep.js +58 -0
- package/src/throttle.js +141 -141
- package/src/waitFor.js +99 -77
- package/types/TimerBase.d.ts +2 -2
- package/types/abort.d.ts +9 -0
- package/types/index.d.ts +1 -0
- package/types/raf/waitForRaf.d.ts +1 -1
- package/types/resolveDelayAndFn.d.ts +1 -15
- package/types/retry.d.ts +57 -18
- package/types/sleep.d.ts +29 -0
- package/types/waitFor.d.ts +19 -8
package/dist/timeout-flow.min.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
// TimeoutFlow - Fluent timer + RAF control library
|
|
2
|
-
function k(e){if(typeof e=="number"){if(!Number.isFinite(e))throw new TypeError(`Expected a finite number of ms, got: ${e}`);return e}if(typeof e!="string")throw new TypeError(`Expected a duration string or number, got ${typeof e}`);let n=e.toLowerCase().trim().match(/^(\d+(?:\.\d+)?)\s*(ms|s|m|h)$/);if(!n)throw new Error(`Invalid duration format: "${e}". Expected formats like "500ms", "2s", "1.5m", or "1h".`);let i=Number(n[1]);if(!Number.isFinite(i))throw new TypeError(`Invalid duration value: "${n[1]}"`);let o=n[2];switch(o){case"ms":return i;case"s":return i*1e3;case"m":return i*6e4;case"h":return i*36e5;default:throw new Error(`Unsupported duration unit: "${o}"`)}}var I;typeof performance<"u"&&typeof performance.now=="function"?I=()=>performance.now():I=()=>Date.now();var W=class{#s;#o=null;#e;#n=null;#t=!1;#r=!1;#i=!1;constructor(r){this.#s=k(r),this.#e=this.#s}get isRunning(){return this.#t}get isPaused(){return this.#r}get isFinished(){return this.#i}pause(){if(!this.#t)return;clearTimeout(this.#n),this.#n=null;let r=I()-this.#o;this.#e=Math.max(0,this.#e-r),this.#t=!1,this.#r=!0}resume(r,n=null){if(this.#i||this.#t)return;let i=n??this.#e;if(!Number.isFinite(i)||i<0)return;let o=Math.max(0,i);this.#e=o,this.#o=I(),this.#t=!0,this.#r=!1,this.#n=setTimeout(()=>{this.#n=null,this.#t=!1,this.#r=!1,this.#i=!0,this.#e=0,r?.()},o)}cancel(){this.#n&&clearTimeout(this.#n),this.#n=null,this.#t=!1,this.#r=!1,this.#i=!0,this.#e=0}reset(){this.#n&&clearTimeout(this.#n),this.#n=null,this.#o=null,this.#e=this.#s,this.#t=!1,this.#r=!1,this.#i=!1}};var L;typeof DOMException<"u"?L=(e="Aborted")=>new DOMException(e,"AbortError"):L=(e="Aborted")=>{let r=new Error(e);return r.name="AbortError",r};function C(e,r){return e?e.aborted?(r(),()=>{}):(e.addEventListener("abort",r,{once:!0}),()=>e.removeEventListener("abort",r)):()=>{}}function $(e,r){let n=null;return{add(){n||(n=C(e,r))},remove(){n&&(n(),n=null)},reset(){n&&n(),n=null}}}var U=class extends W{#s=null;#o=null;#e=()=>{};constructor(r,n,{onFinish:i,signal:o}={}){super(r),this.#s=o??null,this.#o=()=>this.cancel(),this.#n(),!this.isFinished&&this.resume(()=>{this.#t(),n?.(),i?.()})}#n(){if(!(!this.#s||!this.#o)){if(this.#s.aborted){super.cancel(),this.#t();return}this.#e=C(this.#s,this.#o)}}#t(){this.#e(),this.#e=()=>{},this.#o=null}cancel(){this.isFinished||(this.#t(),super.cancel())}reset(){this.#e(),this.#e=()=>{},this.#o=()=>this.cancel(),super.reset(),this.#n()}};var Q=e=>e!=null&&typeof e=="object"&&!Array.isArray(e);function q(e,r,n,i=0){let o,l,f;if(typeof e=="function"?(o=e,Q(r)?(l=i,f=r):(l=r,f=Q(n)?n:{})):(l=e,o=r,f=Q(n)?n:{}),typeof o!="function")throw new TypeError(`Expected a function as one argument, got: ${typeof o}`);let a=l??i;if(a==null)throw new TypeError("Expected a duration (string/number) for delay");let s=k(a);return{fn:o,delay:s,options:f}}function V(e,r,n){let{fn:i,delay:o,options:l}=q(e,r,n,void 0);return new U(o,i,l)}var z=class{#s;#o;#e;#n=0;#t=!1;#r=null;#i=null;#l=()=>{};constructor(r,n,{max:i=1/0,runImmediately:o=!1,signal:l}={}){if(this.#s=k(r),this.#o=n,this.#e=i,this.#i=l??null,!(this.#e>0)&&this.#e!==1/0){this.#t=!0,this.#l();return}if(this.#i?.aborted){this.#t=!0,this.#l();return}this.#l=C(this.#i,()=>this.cancel()),o?this.#f():this.#u()}get count(){return this.#n}get isRunning(){return!this.#t&&!!this.#r&&this.#r.isRunning}get isPaused(){return!this.#t&&!!this.#r&&this.#r.isPaused}get isFinished(){return this.#t}#a=()=>{this.#r?.cancel(),this.#r=null,this.#t=!0,this.#l(),this.#l=()=>{},this.#i=null};#f=()=>{if(!this.#t){if(this.#i?.aborted){this.#a();return}if(this.#n>=this.#e){this.#a();return}if(this.#o?.(this.#n++),this.#n>=this.#e){this.#a();return}this.#u()}};#u(){if(!this.#t){if(this.#i?.aborted){this.#a();return}this.#r=new W(this.#s),this.#r.resume(this.#f)}}pause(){this.#t||this.#r?.pause()}resume(){if(!this.#t){if(this.#i?.aborted){this.#a();return}if(this.#n>=this.#e){this.#a();return}if(this.#r){this.#r.isPaused?this.#r.resume(this.#f):this.#r.isRunning||this.#u();return}this.#u()}}cancel(){this.#t||this.#a()}reset(r=!1){if(this.#r?.cancel(),this.#r=null,this.#n=0,this.#t=!1,this.#i?.aborted){this.#t=!0,this.#l(),this.#l=()=>{},this.#i=null;return}if(r){if(!(this.#e>0)&&this.#e!==1/0){this.#t=!0,this.#l(),this.#l=()=>{},this.#i=null;return}this.#u()}}};function X(e,r,n){let{fn:i,delay:o,options:l}=q(e,r,n,void 0);return new z(o,i,l)}function at({signal:e}={}){let r=[],n=new Map,i=0,o=!1,l=!1,f=!1,a=!1,s=!1,c=1/0,p=0,b=null,h=null,y=null,m=[],u=!1,x=null,w=null,d,A=$(e,()=>d.cancel()),v=t=>{t&&t.controller&&(t.controller=null)},F=()=>{let t=r[i];t?.controller?.cancel?.(),t&&(t.controller=null)},T=()=>{a||f||(o=!1,l=!1,a=!0,F(),A.remove(),b?.())},R=()=>{f||(o=!1,l=!1,a=!1,f=!0,F(),A.remove())},E=(t,g)=>{if(R(),h){try{h(t,{step:g,index:i})}catch{}return}throw t},D=(t,g)=>{try{return t()}catch(M){E(M,g);return}},P=t=>{v(t),i+=1,queueMicrotask(_)};function _(){if(f||l||a)return;if(e?.aborted){R();return}if(y){let g=n.get(y);if(typeof g=="number")i=g;else{D(()=>{throw new Error(`Label "${y}" not found`)},r[i]);return}y=null}if(m.length>0){let g=m.shift();D(g,r[i])||(u=!0)}if(u){let g=r[i];if(!g||g.type==="label")u=!1;else return P(g)}let t=r[i];if(!t||i>=r.length){s&&++p<c?(i=0,_()):T();return}if(t.type==="label")return P(t);if(t.type==="after"){t.controller=V(()=>D(t.fn,t),t.duration,{signal:e,onFinish:()=>P(t)});return}if(t.type==="every"){if(!(t.times>0)&&t.times!==1/0)return P(t);let g=0,M=X(()=>{if(!(l||f||a)){if(e?.aborted){R();return}if(t.whileCondition&&!D(t.whileCondition,t)||(D(()=>t.fn(g++),t),t.doWhileCondition&&!D(t.doWhileCondition,t))||g>=t.times)return M.cancel(),P(t)}},t.duration,{max:t.times,signal:e});t.controller=M}}return d={after(t,g){return r.push({type:"after",duration:t,fn:g,controller:null}),d},every(t,g,M=1/0){let N=typeof M=="number"?M:M?.max??1/0,S=N===1/0?1/0:Math.max(0,N|0);return r.push({type:"every",duration:t,fn:g,times:S,whileCondition:x,doWhileCondition:w,controller:null}),x=null,w=null,d},loop(t=!0){return s=!0,c=t===!0?1/0:t,d},onFinish(t){return b=t,d},onError(t){return h=t,d},start(){return F(),f=!1,a=!1,l=!1,o=!0,i=0,p=0,A.reset(),e?.aborted||A.add(),e?.aborted?(R(),d):(_(),d)},pause(){l||f||a||(l=!0,r[i]?.controller?.pause?.())},resume(){if(!(!l||f||a)){if(e?.aborted){R();return}l=!1,r[i]?.controller?.resume?.()}},cancel(){R()},reset(){return R(),r.length=0,n.clear(),i=0,p=0,b=null,h=null,y=null,m=[],u=!1,x=null,w=null,o=!1,l=!1,f=!1,a=!1,d},label(t){return n.set(t,r.length),r.push({type:"label",name:t,controller:null}),d},jumpTo(t){return y=t,d},if(t){return m.push(()=>typeof t=="function"?!!t():!!t),d},unless(t){return m.push(()=>typeof t=="function"?!t():!t),d},while(t){return x=()=>typeof t=="function"?!!t():!!t,d},doWhile(t){return w=()=>typeof t=="function"?!!t():!!t,d},get isRunning(){return o&&!l&&!f&&!a},get isPaused(){return l},get isCancelled(){return f},get isFinished(){return a}},d}function ut(e,r,n){let{fn:i,delay:o,options:l}=q(e,r,n,void 0),f=l?.signal,a=l?.trailing??!0,s=Math.max(0,o),c=-1/0,p=0,b=null,h=null,y=$(f,()=>u()),m=()=>{if(!b)return;let d=b,A=h;b=null,h=null,y.remove(),i.apply(A,d)},u=()=>{p&&clearTimeout(p),p=0,b=null,h=null,y.remove()},x=d=>{y.add(),p=setTimeout(()=>{p=0,c=I(),m()},d)},w=function(...d){if(f?.aborted)return;let A=I(),v=A-c;if(b=d,h=this,v>=s){c=A,p&&(clearTimeout(p),p=0,y.remove()),m();return}if(a&&!p){let F=Math.max(0,s-v);x(F)}};return w.cancel=u,w.flush=()=>{p&&(clearTimeout(p),p=0,c=I(),m())},f?.aborted&&u(),w}function ft(e,r,n){let{fn:i,delay:o,options:l}=q(e,r,n,0),f=l?.signal,a=Math.max(0,o),s=0,c=null,p=null,b=$(f,()=>y()),h=()=>{if(!c)return;let u=c,x=p;c=null,p=null,b.remove(),i.apply(x,u)},y=()=>{s&&clearTimeout(s),s=0,c=null,p=null,b.remove()},m=function(...u){f?.aborted||(c=u,p=this,s&&clearTimeout(s),b.add(),s=setTimeout(()=>{s=0,h()},a))};return m.cancel=y,m.flush=()=>{s&&(clearTimeout(s),s=0,h())},f?.aborted&&y(),m}function ct(e,{interval:r="250ms",timeout:n,immediate:i=!1,signal:o}={}){return new Promise((l,f)=>{if(o?.aborted){f(L());return}let a=I(),s=typeof r=="string"?k(r):r,c=n!=null?k(n):null,p=null,b=()=>{p!=null&&(clearInterval(p),p=null),y()},y=C(o,()=>{b(),f(L())}),m=()=>{let u=!1;try{u=!!e()}catch(x){b(),f(x);return}if(u){b(),l();return}c!=null&&I()-a>=c&&(b(),f(new Error("waitFor timed out")))};i&&m(),p=setInterval(m,s)})}async function ht(e,{attempts:r=3,delay:n="500ms",backoff:i=!1,factor:o=2,maxDelay:l,jitter:f=!1,signal:a,random:s=Math.random,shouldRetry:c,onRetry:p}={}){if(typeof e!="function")throw new TypeError("retry: fn must be a function");let b=Math.max(1,r|0);if(a?.aborted)throw L();let h=k(n),y=l!=null?k(l):1/0,m=Math.min(h,y);for(let u=0;u<b;u++){if(a?.aborted)throw L();try{return await e(u)}catch(x){if(u===b-1||c&&!await c(x,u))throw x;let d=i?h*Math.pow(o,u):h,A=mt({expDelay:d,baseDelay:h,maxDelayMs:y,jitter:f,random:s,prevSleep:m});m=A,p&&await p(x,u,A),await pt(A,a)}}throw new Error("retry: exhausted attempts unexpectedly")}function mt({expDelay:e,baseDelay:r,maxDelayMs:n,jitter:i,random:o,prevSleep:l}){let f=Math.min(e,n);if(!i)return f;let a=i===!0?"full":i;if(a==="full")return Math.min(n,Y(0,f,o));if(a==="equal"){let s=f/2;return Math.min(n,s+Y(0,s,o))}if(a==="decorrelated"){let s=Y(r,l*3,o);return Math.min(n,s)}return f}function Y(e,r,n){let i=n(),o=Number.isFinite(i)?Math.min(1,Math.max(0,i)):0;return e+(r-e)*o}function pt(e,r){return new Promise((n,i)=>{if(r?.aborted){i(L());return}let o=0,l=()=>{o&&clearTimeout(o),o=0,a()},a=C(r,()=>{l(),i(L())});o=setTimeout(()=>{l(),n()},e)})}function j(e,r,n,i){let o=typeof n=="function"?n:void 0,l=o?i:n,{fn:f,delay:a,options:s}=q(e,r,l,void 0),{signal:c,onFinish:p}=o?{...s,onFinish:o}:s,b=Math.max(0,a),h=0,y=b,m=null,u=!1,x=!1,w=!1,d=()=>{h&&cancelAnimationFrame(h),h=0},A=()=>{},v=()=>{A(),A=()=>{}},F=()=>{d(),u=!1,x=!1,w=!0,m=null,y=0,v()},T=()=>{F(),f?.(),p?.()},R=()=>{w||F()},E=()=>{if(v(),c?.aborted){w=!0,y=0;return}A=C(c,R)};E();let D=g=>{if(!u||w)return;if(m==null&&(m=g),g-m>=y){T();return}h=requestAnimationFrame(D)},P=()=>{if(!u||w)return;d();let g=I();if(m!=null){let M=g-m;y=Math.max(0,y-M)}m=null,u=!1,x=!0},_=()=>{if(!(w||u||!x)&&!c?.aborted){if(!(y>0)){T();return}x=!1,u=!0,m=null,h=requestAnimationFrame(D)}},t=()=>{d(),m=null,y=b,u=!1,x=!1,w=!1,E()};return w?v():y>0?(u=!0,h=requestAnimationFrame(D)):T(),{pause:P,resume:_,cancel:R,reset:t,get isRunning(){return u},get isPaused(){return x},get isFinished(){return w}}}var G=e=>e!=null&&typeof e=="object"&&!Array.isArray(e);function tt(e,r,n,i,o){let l=typeof e!="function",f,a,s;l?(G(n)?s=n:G(i)?s=i:G(o)?s=o:s={},typeof n=="number"&&(f=n),typeof i=="boolean"&&(a=i),a==null&&typeof n=="boolean"&&(a=n)):s=G(n)?n:{};let{fn:c,delay:p,options:b}=q(e,r,s,void 0),h=b?.signal,y=b?.max??1/0,m=b?.runImmediately??!1,u=f??y,x=a??m,w=Math.max(0,p),d=0,A=0,v=w,F=null,T=!1,R=!1,E=!1,D=()=>{d&&cancelAnimationFrame(d),d=0},P=()=>{},_=()=>{P(),P=()=>{}},t=()=>{D(),T=!1,R=!1,E=!0,F=null,v=0,_()},g=()=>{E||t()},M=()=>{if(_(),h?.aborted){E=!0,v=0;return}P=C(h,g)};M();let N=()=>A<u,S=()=>(c?.(A++),N()?!0:(t(),!1)),H=O=>{if(!T||E)return;if(h?.aborted){t();return}if(!N()){t();return}F==null&&(F=O);let K=O-F;F=O,v-=K;let Z=Math.max(0,w);if(Z===0){v=0,S(),d=requestAnimationFrame(H);return}for(;v<=0&&!E;){if(!S())return;v+=Z}d=requestAnimationFrame(H)},J=()=>{if(!E){if(h?.aborted){t();return}if(!N()){t();return}T=!0,R=!1,F=null,d=requestAnimationFrame(H)}},ot=()=>{if(!T||E)return;D();let O=I();if(F!=null){let K=O-F;v=Math.max(0,v-K)}F=null,T=!1,R=!0},st=()=>{if(!(E||T||!R)&&!h?.aborted){if(!N()){t();return}J()}},lt=(O=!1)=>{if(D(),A=0,v=w,F=null,T=!1,R=!1,E=!1,M(),!E){if(!(u>0)&&u!==1/0){t();return}if(O){if(x&&N()&&(S(),E))return;J()}}};return E?_():!(u>0)&&u!==1/0?t():(x&&N()&&S(),E||(v=Math.max(0,w),J())),{pause:ot,resume:st,cancel:g,reset:lt,get isRunning(){return T},get isPaused(){return R},get isFinished(){return E},get count(){return A}}}function et(e,r,n){let{fn:i,delay:o,options:l}=q(e,r,n,0),f=Math.max(0,o),a=l?.signal,s=0,c=null,p=null,b=0,h=()=>{s&&cancelAnimationFrame(s),s=0},y=$(a,()=>m()),m=()=>{h(),c=null,p=null,b=0,y.remove()},u=()=>{if(!c)return;let d=c,A=p;c=null,p=null,y.remove(),i.apply(A,d)},x=d=>{if(!s)return;if(!c){m();return}if(d-b>=f){h(),u();return}s=requestAnimationFrame(x)},w=function(...d){a?.aborted||(c=d,p=this,b=I(),y.add(),h(),s=requestAnimationFrame(x))};return w.cancel=m,w.flush=()=>{!s||!c||(h(),u())},a?.aborted&&m(),w}var rt=e=>e!=null&&typeof e=="object"&&!Array.isArray(e);function nt(e,r=0,n){if(typeof e!="function")throw new TypeError(`Expected a function, got: ${typeof e}`);let i=typeof r=="number"?r:0,o=rt(r)?r:rt(n)?n:{},l=o.signal,f=o.trailing??!0,a=Math.max(0,i|0),s=!1,c=0,p=null,b=null,h=a,y=$(l,()=>u()),m=()=>{s=!1,c&&cancelAnimationFrame(c),c=0,h=a,p=null,b=null,y.remove()},u=()=>{m()},x=()=>{if(!p)return;let T=p,R=b;p=null,b=null,y.remove(),e.apply(R,T)},w=()=>{s=!1,c=0,h=a,x()},d=()=>{if(s){if(l?.aborted){u();return}if(h===0){w();return}h-=1,c=requestAnimationFrame(d)}},A=()=>{if(s){if(l?.aborted){u();return}w()}},v=a===0?()=>{s=!0,y.add(),c=requestAnimationFrame(A)}:()=>{s=!0,h=a,y.add(),c=requestAnimationFrame(d)},F=function(...T){l?.aborted||((!s||f)&&(p=T,b=this),!s&&v())};return F.cancel=u,F.flush=()=>{if(s){if(l?.aborted){u();return}s=!1,c&&cancelAnimationFrame(c),c=0,h=a,x()}},l?.aborted&&u(),F}function it(e,{timeout:r,signal:n,immediate:i=!1}={}){return new Promise((o,l)=>{if(typeof e!="function"){l(new TypeError(`waitForRaf: condition must be a function, got: ${typeof e}`));return}if(n?.aborted){l(L());return}let f=r!=null?Math.max(0,k(r)):null,a=0,s=null,c=()=>{a&&cancelAnimationFrame(a),a=0,b()},b=C(n,()=>{c(),l(L())}),h=()=>{try{return!!e()}catch(m){return c(),l(m),null}};if(i){let m=h();if(m===null)return;if(m){c(),o();return}if(f===0){c(),l(new Error("waitForRaf timed out"));return}}let y=m=>{s==null&&(s=m);let u=h();if(u!==null){if(u){c(),o();return}if(f!=null&&m-s>=f){c(),l(new Error("waitForRaf timed out"));return}a=requestAnimationFrame(y)}};a=requestAnimationFrame(y)})}function dt(){if(typeof requestAnimationFrame!="function")throw new Error("requestAnimationFrame is not available in this environment. Use the non-RAF timers (after/every/waitFor) in Node, or provide a RAF polyfill.")}var B=e=>(...r)=>(dt(),e(...r)),yt=B(j),bt=B(tt),wt=B(et),xt=B(nt),At=B(it);export{V as after,yt as afterRaf,ft as debounce,wt as debounceRaf,X as every,bt as everyRaf,at as flow,k as parseDuration,ht as retry,ut as throttle,xt as throttleRaf,ct as waitFor,At as waitForRaf};
|
|
2
|
+
function P(e){if(typeof e=="number"){if(!Number.isFinite(e))throw new TypeError(`Expected a finite number of ms, got: ${e}`);return e}if(typeof e!="string")throw new TypeError(`Expected a duration string or number, got ${typeof e}`);let n=e.toLowerCase().trim().match(/^(\d+(?:\.\d+)?)\s*(ms|s|m|h)$/);if(!n)throw new Error(`Invalid duration format: "${e}". Expected formats like "500ms", "2s", "1.5m", or "1h".`);let i=Number(n[1]);if(!Number.isFinite(i))throw new TypeError(`Invalid duration value: "${n[1]}"`);switch(n[2]){case"ms":return i;case"s":return i*1e3;case"m":return i*6e4;case"h":return i*36e5}}var k;typeof performance<"u"&&typeof performance.now=="function"?k=()=>performance.now():k=()=>Date.now();var U=class{#l;#s=null;#e;#n=null;#r;#t=!1;#i=!1;#o=!1;constructor(r){this.#l=P(r),this.#e=this.#l}get isRunning(){return this.#t}get isPaused(){return this.#i}get isFinished(){return this.#o}pause(){if(!this.#t)return;clearTimeout(this.#n),this.#n=null;let r=k()-this.#s;this.#e=Math.max(0,this.#e-r),this.#t=!1,this.#i=!0}resume(r,n=null){if(this.#o||this.#t)return;let i=n??this.#e;if(!Number.isFinite(i))return;let s=Math.max(0,i);r??=this.#r,this.#r=r,this.#e=s,this.#s=k(),this.#t=!0,this.#i=!1,this.#n=setTimeout(()=>{this.#n=null,this.#t=!1,this.#i=!1,this.#o=!0,this.#e=0,r?.()},s)}cancel(){this.#n&&clearTimeout(this.#n),this.#n=null,this.#t=!1,this.#i=!1,this.#o=!0,this.#e=0}reset(){this.#n&&clearTimeout(this.#n),this.#n=null,this.#s=null,this.#e=this.#l,this.#t=!1,this.#i=!1,this.#o=!1}};var Q;typeof DOMException<"u"?Q=(e="Aborted")=>new DOMException(e,"AbortError"):Q=(e="Aborted")=>{let r=new Error(e);return r.name="AbortError",r};function L(e,r){return e?e.aborted?(r(),()=>{}):(e.addEventListener("abort",r,{once:!0}),()=>e.removeEventListener("abort",r)):()=>{}}function O(e,r){let n=null;return{add(){n||(n=L(e,r))},remove(){n&&(n(),n=null)},reset(){n&&n(),n=null}}}function $(e){return e?.reason??Q()}var Y=class extends U{#l=null;#s=null;#e=()=>{};constructor(r,n,{onFinish:i,signal:s}={}){super(r),this.#l=s??null,this.#s=()=>this.cancel(),this.#n(),!this.isFinished&&this.resume(()=>{this.#r(),n?.(),i?.()})}#n(){if(!(!this.#l||!this.#s)){if(this.#l.aborted){super.cancel(),this.#r();return}this.#e=L(this.#l,this.#s)}}#r(){this.#e(),this.#e=()=>{},this.#s=null}cancel(){this.isFinished||(this.#r(),super.cancel())}reset(){this.#e(),this.#e=()=>{},this.#s=()=>this.cancel(),super.reset(),this.#n()}};var N=e=>e!=null&&typeof e=="object"&&!Array.isArray(e);function _(e,r,n,i=0){let s,l,a;if(typeof e=="function"?(s=e,N(r)?(l=i,a=r):(l=r,a=N(n)?n:{})):(l=e,s=r,a=N(n)?n:{}),typeof s!="function")throw new TypeError(`Expected a function as one argument, got: ${typeof s}`);let u=l??i;if(u==null)throw new TypeError("Expected a duration (string/number) for delay");let o=P(u);return{fn:s,delay:o,options:a}}function V(e,r,n){let{fn:i,delay:s,options:l}=_(e,r,n,void 0);return new Y(s,i,l)}var z=class{#l;#s;#e;#n=0;#r=!1;#t=null;#i=null;#o=()=>{};constructor(r,n,{max:i=1/0,runImmediately:s=!1,signal:l}={}){if(this.#l=P(r),this.#s=n,this.#e=i,this.#i=l??null,!(this.#e>0)&&this.#e!==1/0){this.#r=!0,this.#o();return}if(this.#i?.aborted){this.#r=!0,this.#o();return}this.#o=L(this.#i,()=>this.cancel()),s?this.#f():this.#u()}get count(){return this.#n}get isRunning(){return!this.#r&&!!this.#t&&this.#t.isRunning}get isPaused(){return!this.#r&&!!this.#t&&this.#t.isPaused}get isFinished(){return this.#r}#a=()=>{this.#t?.cancel(),this.#t=null,this.#r=!0,this.#o(),this.#o=()=>{},this.#i=null};#f=()=>{if(!this.#r){if(this.#i?.aborted){this.#a();return}if(this.#n>=this.#e){this.#a();return}if(this.#s?.(this.#n++),this.#n>=this.#e){this.#a();return}this.#u()}};#u(){if(!this.#r){if(this.#i?.aborted){this.#a();return}this.#t=new U(this.#l),this.#t.resume(this.#f)}}pause(){this.#r||this.#t?.pause()}resume(){if(!this.#r){if(this.#i?.aborted){this.#a();return}if(this.#n>=this.#e){this.#a();return}if(this.#t){this.#t.isPaused?this.#t.resume(this.#f):this.#t.isRunning||this.#u();return}this.#u()}}cancel(){this.#r||this.#a()}reset(r=!1){if(this.#t?.cancel(),this.#t=null,this.#n=0,this.#r=!1,this.#i?.aborted){this.#r=!0,this.#o(),this.#o=()=>{},this.#i=null;return}if(r){if(!(this.#e>0)&&this.#e!==1/0){this.#r=!0,this.#o(),this.#o=()=>{},this.#i=null;return}this.#u()}}};function Z(e,r,n){let{fn:i,delay:s,options:l}=_(e,r,n,void 0);return new z(s,i,l)}function ft({signal:e}={}){let r=[],n=new Map,i=0,s=!1,l=!1,a=!1,u=!1,o=!1,f=1/0,d=0,w=null,p=null,c=null,y=[],m=!1,x=null,b=null,h,A=O(e,()=>h.cancel()),v=t=>{t&&t.controller&&(t.controller=null)},F=()=>{let t=r[i];t?.controller?.cancel?.(),t&&(t.controller=null)},M=()=>{u||a||(s=!1,l=!1,u=!0,F(),A.remove(),w?.())},I=()=>{a||(s=!1,l=!1,u=!1,a=!0,F(),A.remove())},R=(t,g)=>{if(I(),p){try{p(t,{step:g,index:i})}catch{}return}throw t},D=(t,g)=>{try{return t()}catch(T){R(T,g);return}},C=t=>{v(t),i+=1,queueMicrotask(E)};function E(){if(a||l||u)return;if(e?.aborted){I();return}if(c){let g=n.get(c);if(typeof g=="number")i=g;else{D(()=>{throw new Error(`Label "${c}" not found`)},r[i]);return}c=null}if(y.length>0){let g=y.shift();D(g,r[i])||(m=!0)}if(m){let g=r[i];if(!g||g.type==="label")m=!1;else return C(g)}let t=r[i];if(!t||i>=r.length){o&&++d<f?(i=0,E()):M();return}if(t.type==="label")return C(t);if(t.type==="after"){t.controller=V(()=>D(t.fn,t),t.duration,{signal:e,onFinish:()=>C(t)});return}if(t.type==="every"){if(!(t.times>0)&&t.times!==1/0)return C(t);let g=0,T=Z(()=>{if(!(l||a||u)){if(e?.aborted){I();return}if(t.whileCondition&&!D(t.whileCondition,t)||(D(()=>t.fn(g++),t),t.doWhileCondition&&!D(t.doWhileCondition,t))||g>=t.times)return T.cancel(),C(t)}},t.duration,{max:t.times,signal:e});t.controller=T}}return h={after(t,g){return r.push({type:"after",duration:t,fn:g,controller:null}),h},every(t,g,T=1/0){let q=typeof T=="number"?T:T?.max??1/0,B=q===1/0?1/0:Math.max(0,q|0);return r.push({type:"every",duration:t,fn:g,times:B,whileCondition:x,doWhileCondition:b,controller:null}),x=null,b=null,h},loop(t=!0){return o=!0,f=t===!0?1/0:t,h},onFinish(t){return w=t,h},onError(t){return p=t,h},start(){return F(),a=!1,u=!1,l=!1,s=!0,i=0,d=0,A.reset(),e?.aborted||A.add(),e?.aborted?(I(),h):(E(),h)},pause(){l||a||u||(l=!0,r[i]?.controller?.pause?.())},resume(){if(!(!l||a||u)){if(e?.aborted){I();return}l=!1,r[i]?.controller?.resume?.()}},cancel(){I()},reset(){return I(),r.length=0,n.clear(),i=0,d=0,w=null,p=null,c=null,y=[],m=!1,x=null,b=null,s=!1,l=!1,a=!1,u=!1,h},label(t){return n.set(t,r.length),r.push({type:"label",name:t,controller:null}),h},jumpTo(t){return c=t,h},if(t){return y.push(()=>typeof t=="function"?!!t():!!t),h},unless(t){return y.push(()=>typeof t=="function"?!t():!t),h},while(t){return x=()=>typeof t=="function"?!!t():!!t,h},doWhile(t){return b=()=>typeof t=="function"?!!t():!!t,h},get isRunning(){return s&&!l&&!a&&!u},get isPaused(){return l},get isCancelled(){return a},get isFinished(){return u}},h}function ct(e,r,n){let{fn:i,delay:s,options:l}=_(e,r,n,void 0),a=l?.signal,u=l?.trailing??!0,o=Math.max(0,s),f=-1/0,d=0,w=null,p=null,c=O(a,()=>m()),y=()=>{if(!w)return;let h=w,A=p;w=null,p=null,c.remove(),i.apply(A,h)},m=()=>{d&&clearTimeout(d),d=0,w=null,p=null,c.remove()},x=h=>{c.add(),d=setTimeout(()=>{d=0,f=k(),y()},h)},b=function(...h){if(a?.aborted)return;let A=k(),v=A-f;if(w=h,p=this,v>=o){f=A,d&&(clearTimeout(d),d=0,c.remove()),y();return}if(u&&!d){let F=Math.max(0,o-v);x(F)}};return b.cancel=m,b.flush=()=>{d&&(clearTimeout(d),d=0,f=k(),y())},a?.aborted&&m(),b}function mt(e,r,n){let{fn:i,delay:s,options:l}=_(e,r,n,0),a=l?.signal,u=Math.max(0,s),o=0,f=null,d=null,w=O(a,()=>c()),p=()=>{if(!f)return;let m=f,x=d;f=null,d=null,w.remove(),i.apply(x,m)},c=()=>{o&&clearTimeout(o),o=0,f=null,d=null,w.remove()},y=function(...m){a?.aborted||(f=m,d=this,o&&clearTimeout(o),w.add(),o=setTimeout(()=>{o=0,p()},u))};return y.cancel=c,y.flush=()=>{o&&(clearTimeout(o),o=0,p())},a?.aborted&&c(),y}var ht=2147483647;function W(e){if(e==null)return 0;let r=typeof e=="number"?e:P(e);return r>0?Math.min(r,ht):0}function j(e=0,{signal:r}={}){return new Promise((n,i)=>{let s=W(e);if(r?.aborted){i($(r));return}let l=L(r,()=>{clearTimeout(a),i($(r))}),a=setTimeout(()=>{l(),n()},s)})}function pt(e,{interval:r="250ms",timeout:n,immediate:i=!1,signal:s}={}){return new Promise((l,a)=>{if(s?.aborted){a($(s));return}let u=W(r),o=n!=null?W(n):null,f=!1,d=!1,w=0,p=0,c=(x,b)=>{f||(f=!0,clearTimeout(w),clearTimeout(p),y(),x(b))},y=L(s,()=>c(a,$(s))),m=async()=>{let x;d=!0;try{x=await e()}catch(b){c(a,b);return}finally{d=!1}f||(x?c(l,x):w=setTimeout(m,u))};i?m():w=setTimeout(m,u),!f&&o!=null&&(p=setTimeout(()=>{if(!d)try{let b=e();if(b){if(typeof b.then!="function"){c(l,b);return}Promise.resolve(b).catch(()=>{})}}catch(b){c(a,b);return}let x=new Error("waitFor timed out");x.name="TimeoutError",c(a,x)},o))})}async function dt(e,{attempts:r=3,delay:n="500ms",backoff:i=!1,factor:s=2,maxDelay:l,jitter:a=!1,delayFirst:u=!1,timeout:o,signal:f,isSuccess:d,shouldRetry:w,onAttempt:p,onRetry:c,sleep:y=j,now:m=k,random:x=Math.random}={}){if(typeof e!="function")throw new TypeError("retry: fn must be a function");let b=r===1/0?1/0:Math.max(1,r|0),h=()=>{if(f?.aborted)throw $(f)};if(h(),typeof a=="number"&&!(a>=0&&a<=1))throw new RangeError(`retry: numeric jitter must be within [0, 1], got: ${a}`);let A=P(n),v=l!=null?P(l):1/0,F=o!=null?P(o):1/0,M=i===!0?"exponential":i||"fixed",I=m(),R=()=>m()-I,D=async E=>{await y(E,{signal:f}),h()};u&&await D(W(u===!0?A:u));let C=Math.min(A,v);for(let E=0;;E++){h(),p&&await p(E,R());let t;try{let q=await e(E);if(!d||await d(q,E))return q;t=new tt(q,E+1)}catch(q){t=q}if(E+1>=b||w&&!await w(t,E))throw t;let g=M==="exponential"?Math.pow(s,E):M==="linear"?E+1:1,T=W(Math.round(yt({expDelay:A*g,baseDelay:A,maxDelayMs:v,jitter:a,random:x,prevSleep:C})));if(C=T,R()+T>F)throw t;c&&await c(t,E,T),await D(T)}}var tt=class extends Error{constructor(r,n){super(`retry: unsuccessful result after ${n} attempt(s)`),this.name="RetryError",this.result=r,this.attempts=n}};function yt({expDelay:e,baseDelay:r,maxDelayMs:n,jitter:i,random:s,prevSleep:l}){let a=Math.min(e,n);if(!i)return a;if(typeof i=="number"){let o=G(-1,1,s)*i;return Math.min(n,Math.max(0,a*(1+o)))}let u=i===!0?"full":i;if(u==="full")return Math.min(n,G(0,a,s));if(u==="equal"){let o=a/2;return Math.min(n,o+G(0,o,s))}if(u==="decorrelated"){let o=G(r,l*3,s);return Math.min(n,o)}return a}function G(e,r,n){let i=n(),s=Number.isFinite(i)?Math.min(1,Math.max(0,i)):0;return e+(r-e)*s}function rt(e,r,n,i){let s=typeof n=="function"?n:void 0,l=s?i:n,{fn:a,delay:u,options:o}=_(e,r,l,void 0),{signal:f,onFinish:d}=s?{...o,onFinish:s}:o,w=Math.max(0,u),p=0,c=w,y=null,m=!1,x=!1,b=!1,h=()=>{p&&cancelAnimationFrame(p),p=0},A=()=>{},v=()=>{A(),A=()=>{}},F=()=>{h(),m=!1,x=!1,b=!0,y=null,c=0,v()},M=()=>{F(),a?.(),d?.()},I=()=>{b||F()},R=()=>{if(v(),f?.aborted){b=!0,c=0;return}A=L(f,I)};R();let D=g=>{if(!m||b)return;if(y==null&&(y=g),g-y>=c){M();return}p=requestAnimationFrame(D)},C=()=>{if(!m||b)return;h();let g=k();if(y!=null){let T=g-y;c=Math.max(0,c-T)}y=null,m=!1,x=!0},E=()=>{if(!(b||m||!x)&&!f?.aborted){if(!(c>0)){M();return}x=!1,m=!0,y=null,p=requestAnimationFrame(D)}},t=()=>{h(),y=null,c=w,m=!1,x=!1,b=!1,R()};return b?v():c>0?(m=!0,p=requestAnimationFrame(D)):M(),{pause:C,resume:E,cancel:I,reset:t,get isRunning(){return m},get isPaused(){return x},get isFinished(){return b}}}function nt(e,r,n,i,s){let l=typeof e!="function",a,u,o;l?(N(n)?o=n:N(i)?o=i:N(s)?o=s:o={},typeof n=="number"&&(a=n),typeof i=="boolean"&&(u=i),u==null&&typeof n=="boolean"&&(u=n)):o=N(n)?n:{};let{fn:f,delay:d,options:w}=_(e,r,o,void 0),p=w?.signal,c=w?.max??1/0,y=w?.runImmediately??!1,m=a??c,x=u??y,b=Math.max(0,d),h=0,A=0,v=b,F=null,M=!1,I=!1,R=!1,D=()=>{h&&cancelAnimationFrame(h),h=0},C=()=>{},E=()=>{C(),C=()=>{}},t=()=>{D(),M=!1,I=!1,R=!0,F=null,v=0,E()},g=()=>{R||t()},T=()=>{if(E(),p?.aborted){R=!0,v=0;return}C=L(p,g)};T();let q=()=>A<m,B=()=>(f?.(A++),q()?M&&!R:(t(),!1)),H=S=>{if(!M||R)return;if(p?.aborted){t();return}if(!q()){t();return}F==null&&(F=S);let K=S-F;F=S,v-=K;let et=Math.max(0,b);if(et===0){v=0,B()&&(h=requestAnimationFrame(H));return}for(;v<=0&&!R;){if(!B())return;v+=et}h=requestAnimationFrame(H)},J=()=>{if(!R){if(p?.aborted){t();return}if(!q()){t();return}M=!0,I=!1,F=null,h=requestAnimationFrame(H)}},lt=()=>{if(!M||R)return;D();let S=k();if(F!=null){let K=S-F;v=Math.max(0,v-K)}F=null,M=!1,I=!0},at=()=>{if(!(R||M||!I)&&!p?.aborted){if(!q()){t();return}J()}},ut=(S=!1)=>{if(D(),A=0,v=b,F=null,M=!1,I=!1,R=!1,T(),!R){if(!(m>0)&&m!==1/0){t();return}if(S){if(x&&q()&&(B(),R))return;J()}}};return R?E():!(m>0)&&m!==1/0?t():(x&&q()&&B(),R||(v=Math.max(0,b),J())),{pause:lt,resume:at,cancel:g,reset:ut,get isRunning(){return M},get isPaused(){return I},get isFinished(){return R},get count(){return A}}}function it(e,r,n){let{fn:i,delay:s,options:l}=_(e,r,n,0),a=Math.max(0,s),u=l?.signal,o=0,f=null,d=null,w=0,p=()=>{o&&cancelAnimationFrame(o),o=0},c=O(u,()=>y()),y=()=>{p(),f=null,d=null,w=0,c.remove()},m=()=>{if(!f)return;let h=f,A=d;f=null,d=null,c.remove(),i.apply(A,h)},x=h=>{if(!o)return;if(!f){y();return}if(h-w>=a){p(),m();return}o=requestAnimationFrame(x)},b=function(...h){u?.aborted||(f=h,d=this,w=k(),c.add(),p(),o=requestAnimationFrame(x))};return b.cancel=y,b.flush=()=>{!o||!f||(p(),m())},u?.aborted&&y(),b}function ot(e,r=0,n){if(typeof e!="function")throw new TypeError(`Expected a function, got: ${typeof e}`);let i=typeof r=="number"?r:0,s=N(r)?r:N(n)?n:{},l=s.signal,a=s.trailing??!0,u=Math.max(0,i|0),o=!1,f=0,d=null,w=null,p=u,c=O(l,()=>m()),y=()=>{o=!1,f&&cancelAnimationFrame(f),f=0,p=u,d=null,w=null,c.remove()},m=()=>{y()},x=()=>{if(!d)return;let M=d,I=w;d=null,w=null,c.remove(),e.apply(I,M)},b=()=>{o=!1,f=0,p=u,x()},h=()=>{if(o){if(l?.aborted){m();return}if(p===0){b();return}p-=1,f=requestAnimationFrame(h)}},A=()=>{if(o){if(l?.aborted){m();return}b()}},v=u===0?()=>{o=!0,c.add(),f=requestAnimationFrame(A)}:()=>{o=!0,p=u,c.add(),f=requestAnimationFrame(h)},F=function(...M){l?.aborted||((!o||a)&&(d=M,w=this),!o&&v())};return F.cancel=m,F.flush=()=>{if(o){if(l?.aborted){m();return}o=!1,f&&cancelAnimationFrame(f),f=0,p=u,x()}},l?.aborted&&m(),F}function st(e,{timeout:r,signal:n,immediate:i=!1}={}){return new Promise((s,l)=>{if(typeof e!="function"){l(new TypeError(`waitForRaf: condition must be a function, got: ${typeof e}`));return}if(n?.aborted){l($(n));return}let a=r!=null?Math.max(0,P(r)):null,u=0,o=null,f=()=>{u&&cancelAnimationFrame(u),u=0,w()},w=L(n,()=>{f(),l($(n))}),p=()=>{try{return!!e()}catch(y){return f(),l(y),null}};if(i){let y=p();if(y===null)return;if(y){f(),s();return}if(a===0){f(),l(new Error("waitForRaf timed out"));return}}let c=y=>{o==null&&(o=y);let m=p();if(m!==null){if(m){f(),s();return}if(a!=null&&y-o>=a){f(),l(new Error("waitForRaf timed out"));return}u=requestAnimationFrame(c)}};u=requestAnimationFrame(c)})}function bt(){if(typeof requestAnimationFrame!="function")throw new Error("requestAnimationFrame is not available in this environment. Use the non-RAF timers (after/every/waitFor) in Node, or provide a RAF polyfill.")}var X=e=>(...r)=>(bt(),e(...r)),wt=X(rt),xt=X(nt),gt=X(it),At=X(ot),Ft=X(st);export{V as after,wt as afterRaf,mt as debounce,gt as debounceRaf,Z as every,xt as everyRaf,ft as flow,P as parseDuration,dt as retry,j as sleep,ct as throttle,At as throttleRaf,pt as waitFor,Ft as waitForRaf};
|
package/package.json
CHANGED
|
@@ -1,69 +1,80 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "timeout-flow",
|
|
3
|
-
"version": "0.0
|
|
4
|
-
"type": "module",
|
|
5
|
-
"description": "Fluent, composable, pauseable JavaScript timers and time control flows
|
|
6
|
-
"keywords": [
|
|
7
|
-
"timeout",
|
|
8
|
-
"interval",
|
|
9
|
-
"flow",
|
|
10
|
-
"raf",
|
|
11
|
-
"animationframe",
|
|
12
|
-
"debounce",
|
|
13
|
-
"retry",
|
|
14
|
-
"javascript",
|
|
15
|
-
"timers",
|
|
16
|
-
"throttle",
|
|
17
|
-
"asynchronous",
|
|
18
|
-
"control-flow",
|
|
19
|
-
"utilities"
|
|
20
|
-
],
|
|
21
|
-
"author": "WATT3D <WATT3D@protonmail.com>",
|
|
22
|
-
"license": "SEE LICENSE IN LICENSE",
|
|
23
|
-
"
|
|
24
|
-
|
|
25
|
-
"url": "
|
|
26
|
-
},
|
|
27
|
-
"
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
"
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
"
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
"./
|
|
46
|
-
"./
|
|
47
|
-
"./
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
"
|
|
51
|
-
"src/",
|
|
52
|
-
"
|
|
53
|
-
"
|
|
54
|
-
"
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
"
|
|
58
|
-
"
|
|
59
|
-
"
|
|
60
|
-
"
|
|
61
|
-
"
|
|
62
|
-
"
|
|
63
|
-
|
|
64
|
-
"
|
|
65
|
-
|
|
66
|
-
"
|
|
67
|
-
"
|
|
68
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "timeout-flow",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"description": "Fluent, composable, pauseable JavaScript timers and time control flows, plus RAF utilities for frame-based logic.",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"timeout",
|
|
8
|
+
"interval",
|
|
9
|
+
"flow",
|
|
10
|
+
"raf",
|
|
11
|
+
"animationframe",
|
|
12
|
+
"debounce",
|
|
13
|
+
"retry",
|
|
14
|
+
"javascript",
|
|
15
|
+
"timers",
|
|
16
|
+
"throttle",
|
|
17
|
+
"asynchronous",
|
|
18
|
+
"control-flow",
|
|
19
|
+
"utilities"
|
|
20
|
+
],
|
|
21
|
+
"author": "WATT3D <WATT3D@protonmail.com>",
|
|
22
|
+
"license": "SEE LICENSE IN LICENSE",
|
|
23
|
+
"homepage": "https://github.com/iWhatty/timeout-flow#readme",
|
|
24
|
+
"bugs": {
|
|
25
|
+
"url": "https://github.com/iWhatty/timeout-flow/issues"
|
|
26
|
+
},
|
|
27
|
+
"repository": {
|
|
28
|
+
"type": "git",
|
|
29
|
+
"url": "git+https://github.com/iWhatty/timeout-flow.git"
|
|
30
|
+
},
|
|
31
|
+
"sideEffects": false,
|
|
32
|
+
"engines": {
|
|
33
|
+
"node": ">=18"
|
|
34
|
+
},
|
|
35
|
+
"main": "./dist/timeout-flow.min.js",
|
|
36
|
+
"module": "./src/index.js",
|
|
37
|
+
"browser": "./dist/timeout-flow.min.js",
|
|
38
|
+
"exports": {
|
|
39
|
+
".": {
|
|
40
|
+
"types": "./types/index.d.ts",
|
|
41
|
+
"import": "./src/index.js",
|
|
42
|
+
"require": "./dist/timeout-flow.min.js",
|
|
43
|
+
"default": "./src/index.js"
|
|
44
|
+
},
|
|
45
|
+
"./after": "./src/after.js",
|
|
46
|
+
"./every": "./src/every.js",
|
|
47
|
+
"./debounce": "./src/debounce.js",
|
|
48
|
+
"./throttle": "./src/throttle.js",
|
|
49
|
+
"./sleep": "./src/sleep.js",
|
|
50
|
+
"./retry": "./src/retry.js",
|
|
51
|
+
"./wait-for": "./src/waitFor.js",
|
|
52
|
+
"./flow": "./src/TimeoutFlow.js",
|
|
53
|
+
"./parse-duration": "./src/parseDuration.js",
|
|
54
|
+
"./raf": "./src/raf/index.js",
|
|
55
|
+
"./min": "./dist/timeout-flow.min.js"
|
|
56
|
+
},
|
|
57
|
+
"files": [
|
|
58
|
+
"dist",
|
|
59
|
+
"src",
|
|
60
|
+
"types",
|
|
61
|
+
"robots.txt",
|
|
62
|
+
"ADDITIONAL_TERMS.md"
|
|
63
|
+
],
|
|
64
|
+
"types": "./types/index.d.ts",
|
|
65
|
+
"scripts": {
|
|
66
|
+
"dev": "serve testing",
|
|
67
|
+
"test": "node --test testing/*.test.mjs && npm run build && npm run test:node",
|
|
68
|
+
"test:unit": "node --test testing/*.test.mjs",
|
|
69
|
+
"test:node": "node testing/node-harness.mjs",
|
|
70
|
+
"build": "node build.js",
|
|
71
|
+
"types": "tsc",
|
|
72
|
+
"prepub": "npm run build && npm run types",
|
|
73
|
+
"prepublishOnly": "npm test && npm run types"
|
|
74
|
+
},
|
|
75
|
+
"devDependencies": {
|
|
76
|
+
"esbuild": "^0.25.3",
|
|
77
|
+
"serve": "^14.2.4",
|
|
78
|
+
"typescript": "^5.8.3"
|
|
79
|
+
}
|
|
69
80
|
}
|
package/robots.txt
CHANGED
|
@@ -1,79 +1,79 @@
|
|
|
1
|
-
# WATT3D — AI scraping policy
|
|
2
|
-
#
|
|
3
|
-
# This file enumerates known AI / LLM training and inference crawlers
|
|
4
|
-
# that the WATT3D AI Training Rider does not consent to. See
|
|
5
|
-
# ./ADDITIONAL_TERMS.md for the legal scope and definitions.
|
|
6
|
-
#
|
|
7
|
-
# Honoring robots.txt is voluntary. The rider applies whether or not
|
|
8
|
-
# a bot respects this file. The enumeration is non-exhaustive — bots
|
|
9
|
-
# not listed here are still bound by the rider if they perform
|
|
10
|
-
# Training Use as defined in ADDITIONAL_TERMS.md §1.
|
|
11
|
-
|
|
12
|
-
User-agent: GPTBot
|
|
13
|
-
Disallow: /
|
|
14
|
-
|
|
15
|
-
User-agent: ChatGPT-User
|
|
16
|
-
Disallow: /
|
|
17
|
-
|
|
18
|
-
User-agent: OAI-SearchBot
|
|
19
|
-
Disallow: /
|
|
20
|
-
|
|
21
|
-
User-agent: anthropic-ai
|
|
22
|
-
Disallow: /
|
|
23
|
-
|
|
24
|
-
User-agent: ClaudeBot
|
|
25
|
-
Disallow: /
|
|
26
|
-
|
|
27
|
-
User-agent: Claude-Web
|
|
28
|
-
Disallow: /
|
|
29
|
-
|
|
30
|
-
User-agent: Claude-SearchBot
|
|
31
|
-
Disallow: /
|
|
32
|
-
|
|
33
|
-
User-agent: Google-Extended
|
|
34
|
-
Disallow: /
|
|
35
|
-
|
|
36
|
-
User-agent: Applebot-Extended
|
|
37
|
-
Disallow: /
|
|
38
|
-
|
|
39
|
-
User-agent: Meta-ExternalAgent
|
|
40
|
-
Disallow: /
|
|
41
|
-
|
|
42
|
-
User-agent: Meta-ExternalFetcher
|
|
43
|
-
Disallow: /
|
|
44
|
-
|
|
45
|
-
User-agent: FacebookBot
|
|
46
|
-
Disallow: /
|
|
47
|
-
|
|
48
|
-
User-agent: CCBot
|
|
49
|
-
Disallow: /
|
|
50
|
-
|
|
51
|
-
User-agent: PerplexityBot
|
|
52
|
-
Disallow: /
|
|
53
|
-
|
|
54
|
-
User-agent: Perplexity-User
|
|
55
|
-
Disallow: /
|
|
56
|
-
|
|
57
|
-
User-agent: cohere-ai
|
|
58
|
-
Disallow: /
|
|
59
|
-
|
|
60
|
-
User-agent: Bytespider
|
|
61
|
-
Disallow: /
|
|
62
|
-
|
|
63
|
-
User-agent: Amazonbot
|
|
64
|
-
Disallow: /
|
|
65
|
-
|
|
66
|
-
User-agent: Omgilibot
|
|
67
|
-
Disallow: /
|
|
68
|
-
|
|
69
|
-
User-agent: AI2Bot
|
|
70
|
-
Disallow: /
|
|
71
|
-
|
|
72
|
-
User-agent: Diffbot
|
|
73
|
-
Disallow: /
|
|
74
|
-
|
|
75
|
-
# Everyone else: full access. Search engines and human-driven crawlers
|
|
76
|
-
# remain welcome.
|
|
77
|
-
|
|
78
|
-
User-agent: *
|
|
79
|
-
Allow: /
|
|
1
|
+
# WATT3D — AI scraping policy
|
|
2
|
+
#
|
|
3
|
+
# This file enumerates known AI / LLM training and inference crawlers
|
|
4
|
+
# that the WATT3D AI Training Rider does not consent to. See
|
|
5
|
+
# ./ADDITIONAL_TERMS.md for the legal scope and definitions.
|
|
6
|
+
#
|
|
7
|
+
# Honoring robots.txt is voluntary. The rider applies whether or not
|
|
8
|
+
# a bot respects this file. The enumeration is non-exhaustive — bots
|
|
9
|
+
# not listed here are still bound by the rider if they perform
|
|
10
|
+
# Training Use as defined in ADDITIONAL_TERMS.md §1.
|
|
11
|
+
|
|
12
|
+
User-agent: GPTBot
|
|
13
|
+
Disallow: /
|
|
14
|
+
|
|
15
|
+
User-agent: ChatGPT-User
|
|
16
|
+
Disallow: /
|
|
17
|
+
|
|
18
|
+
User-agent: OAI-SearchBot
|
|
19
|
+
Disallow: /
|
|
20
|
+
|
|
21
|
+
User-agent: anthropic-ai
|
|
22
|
+
Disallow: /
|
|
23
|
+
|
|
24
|
+
User-agent: ClaudeBot
|
|
25
|
+
Disallow: /
|
|
26
|
+
|
|
27
|
+
User-agent: Claude-Web
|
|
28
|
+
Disallow: /
|
|
29
|
+
|
|
30
|
+
User-agent: Claude-SearchBot
|
|
31
|
+
Disallow: /
|
|
32
|
+
|
|
33
|
+
User-agent: Google-Extended
|
|
34
|
+
Disallow: /
|
|
35
|
+
|
|
36
|
+
User-agent: Applebot-Extended
|
|
37
|
+
Disallow: /
|
|
38
|
+
|
|
39
|
+
User-agent: Meta-ExternalAgent
|
|
40
|
+
Disallow: /
|
|
41
|
+
|
|
42
|
+
User-agent: Meta-ExternalFetcher
|
|
43
|
+
Disallow: /
|
|
44
|
+
|
|
45
|
+
User-agent: FacebookBot
|
|
46
|
+
Disallow: /
|
|
47
|
+
|
|
48
|
+
User-agent: CCBot
|
|
49
|
+
Disallow: /
|
|
50
|
+
|
|
51
|
+
User-agent: PerplexityBot
|
|
52
|
+
Disallow: /
|
|
53
|
+
|
|
54
|
+
User-agent: Perplexity-User
|
|
55
|
+
Disallow: /
|
|
56
|
+
|
|
57
|
+
User-agent: cohere-ai
|
|
58
|
+
Disallow: /
|
|
59
|
+
|
|
60
|
+
User-agent: Bytespider
|
|
61
|
+
Disallow: /
|
|
62
|
+
|
|
63
|
+
User-agent: Amazonbot
|
|
64
|
+
Disallow: /
|
|
65
|
+
|
|
66
|
+
User-agent: Omgilibot
|
|
67
|
+
Disallow: /
|
|
68
|
+
|
|
69
|
+
User-agent: AI2Bot
|
|
70
|
+
Disallow: /
|
|
71
|
+
|
|
72
|
+
User-agent: Diffbot
|
|
73
|
+
Disallow: /
|
|
74
|
+
|
|
75
|
+
# Everyone else: full access. Search engines and human-driven crawlers
|
|
76
|
+
# remain welcome.
|
|
77
|
+
|
|
78
|
+
User-agent: *
|
|
79
|
+
Allow: /
|
package/src/AfterTimer.js
CHANGED
|
@@ -1,82 +1,82 @@
|
|
|
1
|
-
// ./src/AfterTimer.js
|
|
2
|
-
import { TimerBase } from './TimerBase.js';
|
|
3
|
-
import { attachAbort } from './abort.js';
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Executes a function once after a delay.
|
|
7
|
-
*/
|
|
8
|
-
export class AfterTimer extends TimerBase {
|
|
9
|
-
/** @type {AbortSignal | null} */
|
|
10
|
-
#signal = null;
|
|
11
|
-
|
|
12
|
-
/** @type {(() => void) | null} */
|
|
13
|
-
#onAbort = null;
|
|
14
|
-
|
|
15
|
-
/** @type {() => void} */
|
|
16
|
-
#cleanupAbort = () => { };
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* @param {string|number} duration - Delay before firing (e.g., '1s', 300)
|
|
20
|
-
* @param {Function} fn - Main function to execute
|
|
21
|
-
* @param {Object} [options]
|
|
22
|
-
* @param {Function} [options.onFinish] - Optional callback when done
|
|
23
|
-
* @param {AbortSignal} [options.signal] - Optional AbortSignal to auto-cancel
|
|
24
|
-
*/
|
|
25
|
-
constructor(duration, fn, { onFinish, signal } = {}) {
|
|
26
|
-
super(duration);
|
|
27
|
-
|
|
28
|
-
this.#signal = signal ?? null;
|
|
29
|
-
this.#onAbort = () => this.cancel();
|
|
30
|
-
|
|
31
|
-
this.#attachAbort();
|
|
32
|
-
|
|
33
|
-
// If already aborted, #attachAbort() will have canceled us.
|
|
34
|
-
if (this.isFinished) return;
|
|
35
|
-
|
|
36
|
-
this.resume(() => {
|
|
37
|
-
this.#cleanupAbortListener();
|
|
38
|
-
fn?.();
|
|
39
|
-
onFinish?.();
|
|
40
|
-
});
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
#attachAbort() {
|
|
44
|
-
if (!this.#signal || !this.#onAbort) return;
|
|
45
|
-
|
|
46
|
-
// If already aborted, terminate immediately.
|
|
47
|
-
if (this.#signal.aborted) {
|
|
48
|
-
super.cancel();
|
|
49
|
-
this.#cleanupAbortListener();
|
|
50
|
-
return;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
this.#cleanupAbort = attachAbort(this.#signal, this.#onAbort);
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
#cleanupAbortListener() {
|
|
57
|
-
this.#cleanupAbort();
|
|
58
|
-
this.#cleanupAbort = () => { };
|
|
59
|
-
|
|
60
|
-
// Keep signal reference so reset() can reattach.
|
|
61
|
-
this.#onAbort = null;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
cancel() {
|
|
65
|
-
if (this.isFinished) return;
|
|
66
|
-
|
|
67
|
-
// Remove listener first to avoid keeping instance alive.
|
|
68
|
-
this.#cleanupAbortListener();
|
|
69
|
-
|
|
70
|
-
super.cancel();
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
reset() {
|
|
74
|
-
// Preserve AbortSignal semantics.
|
|
75
|
-
this.#cleanupAbort();
|
|
76
|
-
this.#cleanupAbort = () => { };
|
|
77
|
-
this.#onAbort = () => this.cancel();
|
|
78
|
-
|
|
79
|
-
super.reset();
|
|
80
|
-
this.#attachAbort();
|
|
81
|
-
}
|
|
1
|
+
// ./src/AfterTimer.js
|
|
2
|
+
import { TimerBase } from './TimerBase.js';
|
|
3
|
+
import { attachAbort } from './abort.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Executes a function once after a delay.
|
|
7
|
+
*/
|
|
8
|
+
export class AfterTimer extends TimerBase {
|
|
9
|
+
/** @type {AbortSignal | null} */
|
|
10
|
+
#signal = null;
|
|
11
|
+
|
|
12
|
+
/** @type {(() => void) | null} */
|
|
13
|
+
#onAbort = null;
|
|
14
|
+
|
|
15
|
+
/** @type {() => void} */
|
|
16
|
+
#cleanupAbort = () => { };
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* @param {string|number} duration - Delay before firing (e.g., '1s', 300)
|
|
20
|
+
* @param {Function} fn - Main function to execute
|
|
21
|
+
* @param {Object} [options]
|
|
22
|
+
* @param {Function} [options.onFinish] - Optional callback when done
|
|
23
|
+
* @param {AbortSignal} [options.signal] - Optional AbortSignal to auto-cancel
|
|
24
|
+
*/
|
|
25
|
+
constructor(duration, fn, { onFinish, signal } = {}) {
|
|
26
|
+
super(duration);
|
|
27
|
+
|
|
28
|
+
this.#signal = signal ?? null;
|
|
29
|
+
this.#onAbort = () => this.cancel();
|
|
30
|
+
|
|
31
|
+
this.#attachAbort();
|
|
32
|
+
|
|
33
|
+
// If already aborted, #attachAbort() will have canceled us.
|
|
34
|
+
if (this.isFinished) return;
|
|
35
|
+
|
|
36
|
+
this.resume(() => {
|
|
37
|
+
this.#cleanupAbortListener();
|
|
38
|
+
fn?.();
|
|
39
|
+
onFinish?.();
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
#attachAbort() {
|
|
44
|
+
if (!this.#signal || !this.#onAbort) return;
|
|
45
|
+
|
|
46
|
+
// If already aborted, terminate immediately.
|
|
47
|
+
if (this.#signal.aborted) {
|
|
48
|
+
super.cancel();
|
|
49
|
+
this.#cleanupAbortListener();
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
this.#cleanupAbort = attachAbort(this.#signal, this.#onAbort);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
#cleanupAbortListener() {
|
|
57
|
+
this.#cleanupAbort();
|
|
58
|
+
this.#cleanupAbort = () => { };
|
|
59
|
+
|
|
60
|
+
// Keep signal reference so reset() can reattach.
|
|
61
|
+
this.#onAbort = null;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
cancel() {
|
|
65
|
+
if (this.isFinished) return;
|
|
66
|
+
|
|
67
|
+
// Remove listener first to avoid keeping instance alive.
|
|
68
|
+
this.#cleanupAbortListener();
|
|
69
|
+
|
|
70
|
+
super.cancel();
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
reset() {
|
|
74
|
+
// Preserve AbortSignal semantics.
|
|
75
|
+
this.#cleanupAbort();
|
|
76
|
+
this.#cleanupAbort = () => { };
|
|
77
|
+
this.#onAbort = () => this.cancel();
|
|
78
|
+
|
|
79
|
+
super.reset();
|
|
80
|
+
this.#attachAbort();
|
|
81
|
+
}
|
|
82
82
|
}
|