timeout-flow 0.0.10 → 0.0.12

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.
@@ -1,2 +1,7 @@
1
1
  // TimeoutFlow - Fluent timer + RAF control library
2
- var Q=e=>{throw TypeError(e)};var H=(e,t,n)=>t.has(e)||Q("Cannot "+n);var u=(e,t,n)=>(H(e,t,"read from private field"),n?n.call(e):t.get(e)),d=(e,t,n)=>t.has(e)?Q("Cannot add the same private member more than once"):t instanceof WeakSet?t.add(e):t.set(e,n),m=(e,t,n,l)=>(H(e,t,"write to private field"),l?l.call(e,n):t.set(e,n),n),j=(e,t,n)=>(H(e,t,"access private method"),n);var U=(e,t,n,l)=>({set _(o){m(e,t,o,n)},get _(){return u(e,t,l)}});var V={ms:1,s:1e3,m:6e4,h:36e5};function p(e){if(typeof e=="number"&&Number.isFinite(e))return e;if(typeof e!="string")throw new TypeError(`Expected a duration string or number, got ${typeof e}`);e=e.toLowerCase().trim();let t=e.match(/^(\d+(?:\.\d+)?)\s*(ms|s|m|h)$/);if(!t)throw new Error(`Invalid duration format: "${e}". Expected formats like "500ms", "2s", "1.5m", or "1h".`);let[,n,l]=t;return parseFloat(n)*V[l]}var k,W,R,I,D,C=class{constructor(t){d(this,k);d(this,W,null);d(this,R);d(this,I,null);d(this,D,!1);m(this,k,p(t)),m(this,R,u(this,k))}get isRunning(){return u(this,D)}pause(){u(this,D)&&(clearTimeout(u(this,I)),m(this,R,u(this,R)-(Date.now()-u(this,W))),m(this,D,!1))}resume(t,n=null){!u(this,D)&&u(this,R)>0&&(m(this,W,Date.now()),m(this,D,!0),m(this,I,setTimeout(()=>{m(this,D,!1),t?.()},n??u(this,R))))}cancel(){clearTimeout(u(this,I)),m(this,D,!1),m(this,I,null)}reset(){this.cancel(),m(this,R,u(this,k))}};k=new WeakMap,W=new WeakMap,R=new WeakMap,I=new WeakMap,D=new WeakMap;var S=class extends C{constructor(t,n,l){super(t),this.resume(()=>{n?.(),l?.()})}};function J(e,t,n){return new S(e,t,n)}var $,L,q,v,T,b,E,M,B,z=class{constructor(t,n,l=1/0,o=!1){d(this,M);d(this,$);d(this,L);d(this,q);d(this,v,0);d(this,T,!1);d(this,b,null);d(this,E,()=>{var t;if(u(this,v)>=u(this,q)){this.cancel();return}(t=u(this,L))==null||t.call(this,U(this,v)._++),j(this,M,B).call(this)});m(this,$,p(t)),m(this,L,n),m(this,q,l),o?u(this,E).call(this):j(this,M,B).call(this)}get count(){return u(this,v)}get isRunning(){return u(this,T)}pause(){u(this,b)?.pause(),m(this,T,!1)}resume(){!u(this,T)&&u(this,v)<u(this,q)&&(u(this,b)?.resume(u(this,E)),m(this,T,!0))}cancel(){u(this,b)?.cancel(),m(this,T,!1)}reset(t=!1){this.cancel(),m(this,v,0),t&&j(this,M,B).call(this)}};$=new WeakMap,L=new WeakMap,q=new WeakMap,v=new WeakMap,T=new WeakMap,b=new WeakMap,E=new WeakMap,M=new WeakSet,B=function(){m(this,b,new C(u(this,$))),m(this,T,!0),u(this,b).resume(u(this,E))};function K(e,t,n=1/0,l=!1){return new z(e,t,n,l)}function X(){let e=[],t=0,n=!1,l=!1,o=!1,i=1/0,s=0,f=null,a=new Map,c=null,y=[],x=!1,F=null,A=null,w={after(r,h){return e.push({type:"after",duration:r,fn:h}),w},every(r,h,_=1/0){return e.push({type:"every",duration:r,fn:h,times:_,whileCondition:F,doWhileCondition:A}),F=null,A=null,w},loop(r=!0){return o=!0,i=r===!0?1/0:r,w},onFinish(r){return f=r,w},start(){return l=!1,n=!1,t=0,s=0,P(),w},pause(){n||l||(n=!0,e[t]?.controller?.pause?.())},resume(){!n||l||(n=!1,e[t]?.controller?.resume?.())},cancel(){l=!0;let r=e[t];r?.controller?.cancel?.(),r.controller=null},reset(){return w.cancel(),e.length=0,t=0,s=0,f=null,w},get isPaused(){return n},label(r){return a.set(r,e.length),w},jumpTo(r){return c=r,w},if(r){return y.push(()=>typeof r=="function"?r():!!r),w},unless(r){return y.push(()=>!(typeof r=="function"?r():r)),w},while(r){return F=()=>typeof r=="function"?r():!!r,w},doWhile(r){return A=()=>typeof r=="function"?r():!!r,w}};function g(r=null){r?.controller&&(r.controller=null),t++,P()}function P(){if(l||n)return;if(c){let h=a.get(c);if(typeof h=="number")t=h;else throw new Error(`Label "${c}" not found`);c=null}if(y.length>0&&(y.shift()()||(x=!0)),x){let h=e[t];if(!h||h.type==="label")x=!1;else return g(r)}let r=e[t];if(!r||t>=e.length){o&&++s<i?(t=0,P()):f?.();return}if(r.type==="after"){r.controller=J(r.duration,r.fn,()=>g(r));return}if(r.type==="every"){let h=0,_=!0,N=K(r.duration,()=>{if(!(n||l)&&(r.whileCondition&&!r.whileCondition()||(r.fn(h++),_=!1,!_&&r.doWhileCondition&&!r.doWhileCondition())||h>=r.times))return N.cancel(),g(r)},r.times);r.controller=N}if(r.type==="label")return g(r)}return w}function G(e,t){let n,l;if(typeof e=="function"?(n=e,l=t):(n=t,l=e),typeof n!="function")throw new TypeError(`Expected a function as one argument, got: ${typeof n}`);let o=p(l);return{fn:n,delay:o}}function Y(e,t,n=!0){let{fn:l,delay:o}=G(e,t),i=0,s=null,f=function(...a){let c=this,y=Date.now(),x=y-i;x>=o?(i=y,l.apply(c,a)):n&&!s&&(s=setTimeout(()=>{i=Date.now(),s=null,l.apply(c,a)},o-x))};return f.cancel=()=>{clearTimeout(s),s=null},f}function Z(e,t){let{fn:n,delay:l}=G(e,t),o=null,i=function(...s){clearTimeout(o),o=setTimeout(()=>n.apply(this,s),l)};return i.cancel=()=>{clearTimeout(o),o=null},i}function O(e,{interval:t="250ms",timeout:n}={}){return new Promise((l,o)=>{let i=Date.now(),s=typeof t=="string"?p(t):t,f=n?p(n):null,c=setInterval(()=>{e()?(clearInterval(c),l()):f&&Date.now()-i>=f&&(clearInterval(c),o(new Error("waitFor timed out")))},s)})}async function ee(e,{attempts:t=3,delay:n="500ms",backoff:l=!1}={}){let o=p(n);for(let i=0;i<t;i++)try{return await e()}catch(s){if(i===t-1)throw s;let f=l?o*Math.pow(2,i):o;await new Promise(a=>setTimeout(a,f))}}function te(e,t,n){let l=typeof e=="string"?p(e):e,o=null,i=null,s=null,f=0,a=!0,c=A=>{if(!a)return;i==null&&(i=A),A-i+f>=l?(a=!1,t?.(),n?.()):o=requestAnimationFrame(c)},y=()=>{a&&o!==null&&(cancelAnimationFrame(o),s=performance.now(),a=!1)},x=()=>{!a&&s!==null&&(f+=performance.now()-s,a=!0,o=requestAnimationFrame(c))},F=()=>{o!==null&&(cancelAnimationFrame(o),o=null),a=!1};return o=requestAnimationFrame(c),{pause:y,resume:x,cancel:F,get isRunning(){return a}}}function ne(e,t,n=1/0,l=!1){let o=typeof e=="string"?p(e):e,i=null,s=null,f=null,a=!1,c=!1,y=0,x=0,F=h=>{if(!a||c)return;if(s==null&&(s=h,f=h),h-f+y>=o){if(x>=n){g();return}t?.(),x++,f=h,y=0}i=requestAnimationFrame(F)},A=()=>{a&&!c&&(c=!0,y+=performance.now()-f,cancelAnimationFrame(i))},w=()=>{a&&c&&(c=!1,f=performance.now(),i=requestAnimationFrame(F))},g=()=>{i!==null&&(cancelAnimationFrame(i),i=null),a=!1,c=!1,s=null,f=null,y=0},P=(h=!1)=>{g(),x=0,h&&r()},r=()=>{l&&(t?.(),x++),a=!0,c=!1,f=performance.now(),i=requestAnimationFrame(F)};return r(),{pause:A,resume:w,cancel:g,reset:P,get isRunning(){return a&&!c},get count(){return x}}}function re(e,t){let[n,l]=typeof e=="function"?[0,e]:[p(e),t],o=null,i=null,s=0,f=a=>{a-s>=n?(l(...i),o=null):o=requestAnimationFrame(f)};return(...a)=>{i=a,s=performance.now(),o!==null&&cancelAnimationFrame(o),o=requestAnimationFrame(f)}}function oe(e,t=0){let n=!1,l=null,o=0;return(...i)=>{l=i,n||(n=!0,requestAnimationFrame(function s(){o>=t?(e(...l),o=0,n=!1):(o++,requestAnimationFrame(s))}))}}function le(e,{timeout:t}={}){return new Promise((n,l)=>{let o=performance.now(),i=t?p(t):null,s=f=>{if(e())return n();if(i&&f-o>=i)return l(new Error("waitForRaf timed out"));requestAnimationFrame(s)};requestAnimationFrame(s)})}export{J as after,te as afterRaf,Z as debounce,re as debounceRaf,K as every,ne as everyRaf,X as flow,p as parseDuration,ee as retry,Y as throttle,oe as throttleRaf,O as waitFor,le as waitForRaf};
2
+ var O=e=>{throw TypeError(e)};var G=(e,t,n)=>t.has(e)||O("Cannot "+n);var u=(e,t,n)=>(G(e,t,"read from private field"),n?n.call(e):t.get(e)),d=(e,t,n)=>t.has(e)?O("Cannot add the same private member more than once"):t instanceof WeakSet?t.add(e):t.set(e,n),m=(e,t,n,l)=>(G(e,t,"write to private field"),l?l.call(e,n):t.set(e,n),n),S=(e,t,n)=>(G(e,t,"access private method"),n);var Q=(e,t,n,l)=>({set _(o){m(e,t,o,n)},get _(){return u(e,t,l)}});var U=Object.freeze(JSON.parse(`{
3
+ "ms": 1,
4
+ "s": 1000,
5
+ "m": 60000,
6
+ "h": 3600000
7
+ }`));function p(e){if(typeof e=="number"&&Number.isFinite(e))return e;if(typeof e!="string")throw new TypeError(`Expected a duration string or number, got ${typeof e}`);e=e.toLowerCase().trim();let t=e.match(/^(\d+(?:\.\d+)?)\s*(ms|s|m|h)$/);if(!t)throw new Error(`Invalid duration format: "${e}". Expected formats like "500ms", "2s", "1.5m", or "1h".`);let[,n,l]=t;return parseFloat(n)*U[l]}var k,$,R,I,D,C=class{constructor(t){d(this,k);d(this,$,null);d(this,R);d(this,I,null);d(this,D,!1);m(this,k,p(t)),m(this,R,u(this,k))}get isRunning(){return u(this,D)}pause(){u(this,D)&&(clearTimeout(u(this,I)),m(this,R,u(this,R)-(Date.now()-u(this,$))),m(this,D,!1))}resume(t,n=null){!u(this,D)&&u(this,R)>0&&(m(this,$,Date.now()),m(this,D,!0),m(this,I,setTimeout(()=>{m(this,D,!1),t?.()},n??u(this,R))))}cancel(){clearTimeout(u(this,I)),m(this,D,!1),m(this,I,null)}reset(){this.cancel(),m(this,R,u(this,k))}};k=new WeakMap,$=new WeakMap,R=new WeakMap,I=new WeakMap,D=new WeakMap;var _=class extends C{constructor(t,n,l){super(t),this.resume(()=>{n?.(),l?.()})}};function H(e,t,n){return new _(e,t,n)}var L,N,q,b,T,v,E,M,z,B=class{constructor(t,n,l=1/0,o=!1){d(this,M);d(this,L);d(this,N);d(this,q);d(this,b,0);d(this,T,!1);d(this,v,null);d(this,E,()=>{var t;if(u(this,b)>=u(this,q)){this.cancel();return}(t=u(this,N))==null||t.call(this,Q(this,b)._++),S(this,M,z).call(this)});m(this,L,p(t)),m(this,N,n),m(this,q,l),o?u(this,E).call(this):S(this,M,z).call(this)}get count(){return u(this,b)}get isRunning(){return u(this,T)}pause(){u(this,v)?.pause(),m(this,T,!1)}resume(){!u(this,T)&&u(this,b)<u(this,q)&&(u(this,v)?.resume(u(this,E)),m(this,T,!0))}cancel(){u(this,v)?.cancel(),m(this,T,!1)}reset(t=!1){this.cancel(),m(this,b,0),t&&S(this,M,z).call(this)}};L=new WeakMap,N=new WeakMap,q=new WeakMap,b=new WeakMap,T=new WeakMap,v=new WeakMap,E=new WeakMap,M=new WeakSet,z=function(){m(this,v,new C(u(this,L))),m(this,T,!0),u(this,v).resume(u(this,E))};function K(e,t,n=1/0,l=!1){return new B(e,t,n,l)}function V(){let e=[],t=0,n=!1,l=!1,o=!1,i=1/0,s=0,f=null,a=new Map,c=null,y=[],x=!1,F=null,A=null,w={after(r,h){return e.push({type:"after",duration:r,fn:h}),w},every(r,h,W=1/0){return e.push({type:"every",duration:r,fn:h,times:W,whileCondition:F,doWhileCondition:A}),F=null,A=null,w},loop(r=!0){return o=!0,i=r===!0?1/0:r,w},onFinish(r){return f=r,w},start(){return l=!1,n=!1,t=0,s=0,P(),w},pause(){n||l||(n=!0,e[t]?.controller?.pause?.())},resume(){!n||l||(n=!1,e[t]?.controller?.resume?.())},cancel(){l=!0;let r=e[t];r?.controller?.cancel?.(),r.controller=null},reset(){return w.cancel(),e.length=0,t=0,s=0,f=null,w},get isPaused(){return n},label(r){return a.set(r,e.length),w},jumpTo(r){return c=r,w},if(r){return y.push(()=>typeof r=="function"?r():!!r),w},unless(r){return y.push(()=>!(typeof r=="function"?r():r)),w},while(r){return F=()=>typeof r=="function"?r():!!r,w},doWhile(r){return A=()=>typeof r=="function"?r():!!r,w}};function g(r=null){r?.controller&&(r.controller=null),t++,P()}function P(){if(l||n)return;if(c){let h=a.get(c);if(typeof h=="number")t=h;else throw new Error(`Label "${c}" not found`);c=null}if(y.length>0&&(y.shift()()||(x=!0)),x){let h=e[t];if(!h||h.type==="label")x=!1;else return g(r)}let r=e[t];if(!r||t>=e.length){o&&++s<i?(t=0,P()):f?.();return}if(r.type==="after"){r.controller=H(r.duration,r.fn,()=>g(r));return}if(r.type==="every"){let h=0,W=!0,j=K(r.duration,()=>{if(!(n||l)&&(r.whileCondition&&!r.whileCondition()||(r.fn(h++),W=!1,!W&&r.doWhileCondition&&!r.doWhileCondition())||h>=r.times))return j.cancel(),g(r)},r.times);r.controller=j}if(r.type==="label")return g(r)}return w}function J(e,t){let n,l;if(typeof e=="function"?(n=e,l=t):(n=t,l=e),typeof n!="function")throw new TypeError(`Expected a function as one argument, got: ${typeof n}`);let o=p(l);return{fn:n,delay:o}}function X(e,t,n=!0){let{fn:l,delay:o}=J(e,t),i=0,s=null,f=function(...a){let c=this,y=Date.now(),x=y-i;x>=o?(i=y,l.apply(c,a)):n&&!s&&(s=setTimeout(()=>{i=Date.now(),s=null,l.apply(c,a)},o-x))};return f.cancel=()=>{clearTimeout(s),s=null},f}function Y(e,t){let{fn:n,delay:l}=J(e,t),o=null,i=function(...s){clearTimeout(o),o=setTimeout(()=>n.apply(this,s),l)};return i.cancel=()=>{clearTimeout(o),o=null},i}function Z(e,{interval:t="250ms",timeout:n}={}){return new Promise((l,o)=>{let i=Date.now(),s=typeof t=="string"?p(t):t,f=n?p(n):null,c=setInterval(()=>{e()?(clearInterval(c),l()):f&&Date.now()-i>=f&&(clearInterval(c),o(new Error("waitFor timed out")))},s)})}async function ee(e,{attempts:t=3,delay:n="500ms",backoff:l=!1}={}){let o=p(n);for(let i=0;i<t;i++)try{return await e()}catch(s){if(i===t-1)throw s;let f=l?o*Math.pow(2,i):o;await new Promise(a=>setTimeout(a,f))}}function te(e,t,n){let l=typeof e=="string"?p(e):e,o=null,i=null,s=null,f=0,a=!0,c=A=>{if(!a)return;i==null&&(i=A),A-i+f>=l?(a=!1,t?.(),n?.()):o=requestAnimationFrame(c)},y=()=>{a&&o!==null&&(cancelAnimationFrame(o),s=performance.now(),a=!1)},x=()=>{!a&&s!==null&&(f+=performance.now()-s,a=!0,o=requestAnimationFrame(c))},F=()=>{o!==null&&(cancelAnimationFrame(o),o=null),a=!1};return o=requestAnimationFrame(c),{pause:y,resume:x,cancel:F,get isRunning(){return a}}}function ne(e,t,n=1/0,l=!1){let o=typeof e=="string"?p(e):e,i=null,s=null,f=null,a=!1,c=!1,y=0,x=0,F=h=>{if(!a||c)return;if(s==null&&(s=h,f=h),h-f+y>=o){if(x>=n){g();return}t?.(),x++,f=h,y=0}i=requestAnimationFrame(F)},A=()=>{a&&!c&&(c=!0,y+=performance.now()-f,cancelAnimationFrame(i))},w=()=>{a&&c&&(c=!1,f=performance.now(),i=requestAnimationFrame(F))},g=()=>{i!==null&&(cancelAnimationFrame(i),i=null),a=!1,c=!1,s=null,f=null,y=0},P=(h=!1)=>{g(),x=0,h&&r()},r=()=>{l&&(t?.(),x++),a=!0,c=!1,f=performance.now(),i=requestAnimationFrame(F)};return r(),{pause:A,resume:w,cancel:g,reset:P,get isRunning(){return a&&!c},get count(){return x}}}function re(e,t){let[n,l]=typeof e=="function"?[0,e]:[p(e),t],o=null,i=null,s=0,f=a=>{a-s>=n?(l(...i),o=null):o=requestAnimationFrame(f)};return(...a)=>{i=a,s=performance.now(),o!==null&&cancelAnimationFrame(o),o=requestAnimationFrame(f)}}function oe(e,t=0){let n=!1,l=null,o=0;return(...i)=>{l=i,n||(n=!0,requestAnimationFrame(function s(){o>=t?(e(...l),o=0,n=!1):(o++,requestAnimationFrame(s))}))}}function le(e,{timeout:t}={}){return new Promise((n,l)=>{let o=performance.now(),i=t?p(t):null,s=f=>{if(e())return n();if(i&&f-o>=i)return l(new Error("waitForRaf timed out"));requestAnimationFrame(s)};requestAnimationFrame(s)})}export{H as after,te as afterRaf,Y as debounce,re as debounceRaf,K as every,ne as everyRaf,V as flow,p as parseDuration,ee as retry,X as throttle,oe as throttleRaf,Z as waitFor,le as waitForRaf};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "timeout-flow",
3
- "version": "0.0.10",
3
+ "version": "0.0.12",
4
4
  "type": "module",
5
5
  "description": "Fluent, composable, pauseable JavaScript timers and time control flows — plus RAF utilities for frame-based logic.",
6
6
  "keywords": [
@@ -25,14 +25,16 @@
25
25
  "url": "git+https://github.com/iWhatty/TimeoutFlow-js.git"
26
26
  },
27
27
  "main": "./dist/timeout-flow.min.js",
28
- "module": "./dist/timeout-flow.min.js",
28
+ "module": "./src/index.js",
29
29
  "browser": "./dist/timeout-flow.min.js",
30
30
  "exports": {
31
31
  ".": {
32
- "development": "./src/index.js",
33
- "import": "./dist/timeout-flow.min.js",
34
- "require": "./dist/timeout-flow.min.js",
35
- "default": "./dist/timeout-flow.min.js"
32
+ "browser": {
33
+ "import": "./dist/timeout-flow.min.js",
34
+ "require": "./dist/timeout-flow.min.js"
35
+ },
36
+ "import": "./src/index.js",
37
+ "require": "./dist/timeout-flow.min.js"
36
38
  }
37
39
  },
38
40
  "files": [
@@ -51,4 +53,4 @@
51
53
  "esbuild": "^0.25.3",
52
54
  "serve": "^14.2.4"
53
55
  }
54
- }
56
+ }
@@ -1,37 +1,44 @@
1
1
  // ./src/parseDuration.js
2
2
 
3
- const timeMultipliers = {
4
- ms: 1,
5
- s: 1000,
6
- m: 60_000,
7
- h: 3_600_000,
8
- };
9
-
10
- /**
11
- * Parses a duration string like "2s", "1.5m", "500ms" into milliseconds.
12
- * Also accepts raw number values (treated as ms).
13
- * @param {string|number} input
14
- * @returns {number}
15
- */
16
- export function parseDuration(input) {
17
- if (typeof input === 'number' && Number.isFinite(input)) {
18
- return input;
19
- }
20
-
21
- if (typeof input !== 'string') {
22
- throw new TypeError(`Expected a duration string or number, got ${typeof input}`);
23
- }
24
-
25
- input = input.toLowerCase().trim();
26
-
27
- const match = input.match(/^(\d+(?:\.\d+)?)\s*(ms|s|m|h)$/);
28
-
29
- if (!match) {
30
- throw new Error(
31
- `Invalid duration format: "${input}". Expected formats like "500ms", "2s", "1.5m", or "1h".`
32
- );
33
- }
34
-
35
- const [, value, unit] = match;
36
- return parseFloat(value) * timeMultipliers[unit];
37
- }
3
+ // const timeMultipliers = Object.freeze({
4
+ // "ms": 1,
5
+ // "s": 1000,
6
+ // "m": 60_000,
7
+ // "h": 3_600_000,
8
+ // });
9
+
10
+ const timeMultipliers = Object.freeze(JSON.parse(`{
11
+ "ms": 1,
12
+ "s": 1000,
13
+ "m": 60000,
14
+ "h": 3600000
15
+ }`));
16
+
17
+ /**
18
+ * Parses a duration string like "2s", "1.5m", "500ms" into milliseconds.
19
+ * Also accepts raw number values (treated as ms).
20
+ * @param {string|number} input
21
+ * @returns {number}
22
+ */
23
+ export function parseDuration(input) {
24
+ if (typeof input === 'number' && Number.isFinite(input)) {
25
+ return input;
26
+ }
27
+
28
+ if (typeof input !== 'string') {
29
+ throw new TypeError(`Expected a duration string or number, got ${typeof input}`);
30
+ }
31
+
32
+ input = input.toLowerCase().trim();
33
+
34
+ const match = input.match(/^(\d+(?:\.\d+)?)\s*(ms|s|m|h)$/);
35
+
36
+ if (!match) {
37
+ throw new Error(
38
+ `Invalid duration format: "${input}". Expected formats like "500ms", "2s", "1.5m", or "1h".`
39
+ );
40
+ }
41
+
42
+ const [, value, unit] = match;
43
+ return parseFloat(value) * timeMultipliers[unit];
44
+ }