timeout-flow 0.0.4 → 0.0.6
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/README.md +3 -0
- package/dist/timeout-flow.min.js +1 -1
- package/package.json +1 -1
- package/src/debounce.js +22 -11
- package/src/resolveDelayAndFn.js +28 -0
- package/src/throttle.js +35 -17
- package/src/waitFor.js +5 -0
package/README.md
CHANGED
|
@@ -9,6 +9,9 @@
|
|
|
9
9
|
|
|
10
10
|
TimeoutFlow makes working with time-based logic intuitive — think of it as a modern, composable upgrade to `setTimeout` and `setInterval`, with added powers like chaining, conditional logic, pause/resume, retries, and more.
|
|
11
11
|
|
|
12
|
+
* Minified: 6.64 KB
|
|
13
|
+
* Gzipped: 2.6 KB
|
|
14
|
+
|
|
12
15
|
---
|
|
13
16
|
|
|
14
17
|
## Philosophy
|
package/dist/timeout-flow.min.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
// TimeoutFlow - Fluent timer + RAF control library
|
|
2
|
-
var
|
|
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)),y=(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,i)=>(H(e,t,"write to private field"),i?i.call(e,n):t.set(e,n),n),S=(e,t,n)=>(H(e,t,"access private method"),n);var U=(e,t,n,i)=>({set _(o){m(e,t,o,n)},get _(){return u(e,t,i)}});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,i]=t;return parseFloat(n)*V[i]}var $,L,k,R,F,v,C,q,B,W=class{constructor(t,n,i=1/0,o=!1){y(this,q);y(this,$);y(this,L);y(this,k);y(this,R,0);y(this,F,!1);y(this,v,null);y(this,C,()=>{var t;if(u(this,R)>=u(this,k)){this.cancel();return}(t=u(this,L))==null||t.call(this,U(this,R)._++),S(this,q,B).call(this)});m(this,$,p(t)),m(this,L,n),m(this,k,i),o?u(this,C).call(this):S(this,q,B).call(this)}get count(){return u(this,R)}get isRunning(){return u(this,F)}pause(){u(this,v)?.pause(),m(this,F,!1)}resume(){!u(this,F)&&u(this,R)<u(this,k)&&(u(this,v)?.resume(u(this,C)),m(this,F,!0))}cancel(){u(this,v)?.cancel(),m(this,F,!1)}reset(t=!1){this.cancel(),m(this,R,0),t&&S(this,q,B).call(this)}};$=new WeakMap,L=new WeakMap,k=new WeakMap,R=new WeakMap,F=new WeakMap,v=new WeakMap,C=new WeakMap,q=new WeakSet,B=function(){m(this,v,new E(u(this,$))),m(this,F,!0),u(this,v).resume(u(this,C))};var M,N,b,I,D,E=class{constructor(t){y(this,M);y(this,N,null);y(this,b);y(this,I,null);y(this,D,!1);m(this,M,p(t)),m(this,b,u(this,M))}get isRunning(){return u(this,D)}pause(){u(this,D)&&(clearTimeout(u(this,I)),m(this,b,u(this,b)-(Date.now()-u(this,N))),m(this,D,!1))}resume(t,n=null){!u(this,D)&&u(this,b)>0&&(m(this,N,Date.now()),m(this,D,!0),m(this,I,setTimeout(()=>{m(this,D,!1),t?.()},n??u(this,b))))}cancel(){clearTimeout(u(this,I)),m(this,D,!1),m(this,I,null)}reset(){this.cancel(),m(this,b,u(this,M))}};M=new WeakMap,N=new WeakMap,b=new WeakMap,I=new WeakMap,D=new WeakMap;var z=class extends E{constructor(t,n,i){super(t),this.resume(()=>{n?.(),i?.()})}};function J(e,t,n){return new z(e,t,n)}function K(e,t,n=1/0,i=!1){return new W(e,t,n,i)}function X(){let e=[],t=0,n=!1,i=!1,o=!1,l=1/0,s=0,f=null,a=new Map,c=null,d=[],x=!1,T=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:T,doWhileCondition:A}),T=null,A=null,w},loop(r=!0){return o=!0,l=r===!0?1/0:r,w},onFinish(r){return f=r,w},start(){return i=!1,n=!1,t=0,s=0,P(),w},pause(){n||i||(n=!0,e[t]?.controller?.pause?.())},resume(){!n||i||(n=!1,e[t]?.controller?.resume?.())},cancel(){i=!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 d.push(()=>typeof r=="function"?r():!!r),w},unless(r){return d.push(()=>!(typeof r=="function"?r():r)),w},while(r){return T=()=>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(i||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(d.length>0&&(d.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<l?(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,j=K(r.duration,()=>{if(!(n||i)&&(r.whileCondition&&!r.whileCondition()||(r.fn(h++),_=!1,!_&&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 G(e,t){let n,i;if(typeof e=="function"?(n=e,i=t):(n=t,i=e),typeof n!="function")throw new TypeError(`Expected a function as one argument, got: ${typeof n}`);let o=p(i);return{fn:n,delay:o}}function Y(e,t,n=!0){let{fn:i,delay:o}=G(e,t),l=0,s=null,f=function(...a){let c=this,d=Date.now(),x=d-l;x>=o?(l=d,i.apply(c,a)):n&&!s&&(s=setTimeout(()=>{l=Date.now(),s=null,i.apply(c,a)},o-x))};return f.cancel=()=>{clearTimeout(s),s=null},f}function Z(e,t){let{fn:n,delay:i}=G(e,t),o=null,l=function(...s){clearTimeout(o),o=setTimeout(()=>n.apply(this,s),i)};return l.cancel=()=>{clearTimeout(o),o=null},l}function O(e,{interval:t="250ms",timeout:n}={}){return new Promise((i,o)=>{let l=Date.now(),s=typeof t=="string"?p(t):t,f=n?p(n):null,c=setInterval(()=>{e()?(clearInterval(c),i()):f&&Date.now()-l>=f&&(clearInterval(c),o(new Error("waitFor timed out")))},s)})}async function ee(e,{attempts:t=3,delay:n="500ms",backoff:i=!1}={}){let o=p(n);for(let l=0;l<t;l++)try{return await e()}catch(s){if(l===t-1)throw s;let f=i?o*Math.pow(2,l):o;await new Promise(a=>setTimeout(a,f))}}function te(e,t,n){let i=typeof e=="string"?p(e):e,o=null,l=null,s=null,f=0,a=!0,c=A=>{if(!a)return;l==null&&(l=A),A-l+f>=i?(a=!1,t?.(),n?.()):o=requestAnimationFrame(c)},d=()=>{a&&o!==null&&(cancelAnimationFrame(o),s=performance.now(),a=!1)},x=()=>{!a&&s!==null&&(f+=performance.now()-s,a=!0,o=requestAnimationFrame(c))},T=()=>{o!==null&&(cancelAnimationFrame(o),o=null),a=!1};return o=requestAnimationFrame(c),{pause:d,resume:x,cancel:T,get isRunning(){return a}}}function ne(e,t,n=1/0,i=!1){let o=typeof e=="string"?p(e):e,l=null,s=null,f=null,a=!1,c=!1,d=0,x=0,T=h=>{if(!a||c)return;if(s==null&&(s=h,f=h),h-f+d>=o){if(x>=n){g();return}t?.(),x++,f=h,d=0}l=requestAnimationFrame(T)},A=()=>{a&&!c&&(c=!0,d+=performance.now()-f,cancelAnimationFrame(l))},w=()=>{a&&c&&(c=!1,f=performance.now(),l=requestAnimationFrame(T))},g=()=>{l!==null&&(cancelAnimationFrame(l),l=null),a=!1,c=!1,s=null,f=null,d=0},P=(h=!1)=>{g(),x=0,h&&r()},r=()=>{i&&(t?.(),x++),a=!0,c=!1,f=performance.now(),l=requestAnimationFrame(T)};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,i]=typeof e=="function"?[0,e]:[p(e),t],o=null,l=null,s=0,f=a=>{a-s>=n?(i(...l),o=null):o=requestAnimationFrame(f)};return(...a)=>{l=a,s=performance.now(),o!==null&&cancelAnimationFrame(o),o=requestAnimationFrame(f)}}function oe(e,t=0){let n=!1,i=null,o=0;return(...l)=>{i=l,n||(n=!0,requestAnimationFrame(function s(){o>=t?(e(...i),o=0,n=!1):(o++,requestAnimationFrame(s))}))}}function ie(e,{timeout:t}={}){return new Promise((n,i)=>{let o=performance.now(),l=t?p(t):null,s=f=>{if(e())return n();if(l&&f-o>=l)return i(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,ie as waitForRaf};
|
package/package.json
CHANGED
package/src/debounce.js
CHANGED
|
@@ -1,17 +1,28 @@
|
|
|
1
|
-
|
|
1
|
+
|
|
2
|
+
// ./src/debounce.js
|
|
3
|
+
|
|
4
|
+
import { resolveDelayAndFn } from './resolveDelayAndFn.js';
|
|
2
5
|
|
|
3
6
|
/**
|
|
4
|
-
*
|
|
5
|
-
* @param {string}
|
|
6
|
-
* @param {Function}
|
|
7
|
-
* @returns {Function}
|
|
7
|
+
* Creates a debounced function.
|
|
8
|
+
* @param {Function|string|number} a
|
|
9
|
+
* @param {Function|string|number} b
|
|
10
|
+
* @returns {Function}
|
|
8
11
|
*/
|
|
9
|
-
export function debounce(
|
|
10
|
-
const
|
|
11
|
-
let
|
|
12
|
+
export function debounce(a, b) {
|
|
13
|
+
const { fn, delay } = resolveDelayAndFn(a, b);
|
|
14
|
+
let timeoutId = null;
|
|
15
|
+
|
|
16
|
+
const debounced = function (...args) {
|
|
17
|
+
clearTimeout(timeoutId);
|
|
18
|
+
timeoutId = setTimeout(() => fn.apply(this, args), delay);
|
|
19
|
+
};
|
|
12
20
|
|
|
13
|
-
|
|
14
|
-
clearTimeout(
|
|
15
|
-
|
|
21
|
+
debounced.cancel = () => {
|
|
22
|
+
clearTimeout(timeoutId);
|
|
23
|
+
timeoutId = null;
|
|
16
24
|
};
|
|
25
|
+
|
|
26
|
+
return debounced;
|
|
17
27
|
}
|
|
28
|
+
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { parseDuration } from './parseDuration.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Resolves flexible (fn, delay) or (delay, fn) inputs.
|
|
5
|
+
*
|
|
6
|
+
* @param {Function|string|number} a - function or duration
|
|
7
|
+
* @param {Function|string|number} b - function or duration
|
|
8
|
+
* @returns {{ fn: Function, delay: number }}
|
|
9
|
+
*/
|
|
10
|
+
export function resolveDelayAndFn(a, b) {
|
|
11
|
+
let fn, delayRaw;
|
|
12
|
+
|
|
13
|
+
if (typeof a === 'function') {
|
|
14
|
+
fn = a;
|
|
15
|
+
delayRaw = b;
|
|
16
|
+
} else {
|
|
17
|
+
fn = b;
|
|
18
|
+
delayRaw = a;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
if (typeof fn !== 'function') {
|
|
22
|
+
throw new TypeError(`Expected a function as one argument, got: ${typeof fn}`);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const delay = parseDuration(delayRaw);
|
|
26
|
+
|
|
27
|
+
return { fn, delay };
|
|
28
|
+
}
|
package/src/throttle.js
CHANGED
|
@@ -1,25 +1,43 @@
|
|
|
1
|
-
|
|
1
|
+
// ./src/throttle.js
|
|
2
|
+
|
|
3
|
+
import { resolveDelayAndFn } from './resolveDelayAndFn.js';
|
|
4
|
+
|
|
2
5
|
|
|
3
6
|
/**
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
* @
|
|
7
|
+
* Creates a throttled version of a function.
|
|
8
|
+
* Executes immediately, throttles further calls, optional trailing execution.
|
|
9
|
+
*
|
|
10
|
+
* @param {Function|string|number} a - Function or delay
|
|
11
|
+
* @param {Function|string|number} b - The other parameter
|
|
12
|
+
* @param {boolean} [trailing=true] - If true, fire once at trailing edge
|
|
13
|
+
* @returns {Function} Throttled function with a .cancel() method
|
|
8
14
|
*/
|
|
9
|
-
export function throttle(
|
|
10
|
-
const
|
|
11
|
-
let
|
|
15
|
+
export function throttle(a, b, trailing = true) {
|
|
16
|
+
const { fn, delay } = resolveDelayAndFn(a, b);
|
|
17
|
+
let lastCall = 0;
|
|
18
|
+
let timeoutId = null;
|
|
12
19
|
|
|
13
|
-
|
|
20
|
+
const throttled = function (...args) {
|
|
21
|
+
const context = this;
|
|
14
22
|
const now = Date.now();
|
|
15
|
-
|
|
16
|
-
lastTime = now;
|
|
17
|
-
return fn(...args);
|
|
18
|
-
}
|
|
23
|
+
const timeSinceLast = now - lastCall;
|
|
19
24
|
|
|
20
|
-
if (
|
|
21
|
-
|
|
22
|
-
fn(
|
|
25
|
+
if (timeSinceLast >= delay) {
|
|
26
|
+
lastCall = now;
|
|
27
|
+
fn.apply(context, args);
|
|
28
|
+
} else if (trailing && !timeoutId) {
|
|
29
|
+
timeoutId = setTimeout(() => {
|
|
30
|
+
lastCall = Date.now();
|
|
31
|
+
timeoutId = null;
|
|
32
|
+
fn.apply(context, args);
|
|
33
|
+
}, delay - timeSinceLast);
|
|
23
34
|
}
|
|
24
35
|
};
|
|
25
|
-
|
|
36
|
+
|
|
37
|
+
throttled.cancel = () => {
|
|
38
|
+
clearTimeout(timeoutId);
|
|
39
|
+
timeoutId = null;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
return throttled;
|
|
43
|
+
}
|