UNPKG

push-it-to-the-limit

Version:

Delay function wrappers for common purposes. Throttle, debounce and ratelimit with promises

3 lines (2 loc) 4.38 kB
var z=Object.defineProperty;var P=(t,e,r)=>e in t?z(t,e,{enumerable:!0,configurable:!0,writable:!0,value:r}):t[e]=r;var R=(t,e,r)=>(P(t,typeof e!="symbol"?e+"":e,r),r);function L(t,e,r,o){t(e.call(o,...r))}var w="Rejected";function v(t,e=w){t(new Error(e))}var N="Rejected on cancel";function b(t){v(t,N)}var S=0,_=(t={})=>({delay:S,...t});function p(t){return(e,r,o)=>{H(e);let i=_(o);return typeof r=="number"?i={...i,delay:r}:Array.isArray(r)?i={...i,limit:r}:typeof r=="object"&&(i=typeof r.period=="number"&&typeof r.count=="number"?{...i,delay:r}:{...i,...r}),t(e,i)}}function F(t,e="Assertion error"){if(!t)throw new Error(e)}function H(t){F(typeof t=="function","Target must be a function")}function l(t){t&&clearTimeout(t)}function O(t){return t===void 0?[]:(Array.isArray(t)?t:[t]).map(r=>typeof r=="number"?{period:r,count:1}:r)}var T=class t{constructor(e){R(this,"limits");return this.limits=e.reduce((r,o)=>o instanceof t?r.concat(o.limits):[...r,{...o,rest:o.count,ttl:0}],[]),this}decrease(){this.limits.forEach(e=>{e.rest+=-1})}reset(){this.limits.forEach(t.refreshLimit)}resetTtl(){this.limits.forEach(t.refreshTtl)}isAllowed(){return!this.limits.some(({rest:e})=>e<1)}getNextDelay(){let e=0,r=this.limits;for(let o of r)o.rest<1&&o.ttl>e&&(e=o.ttl);return e-Date.now()}getNextQueueSize(){return Math.min(...this.limits.map(({rest:e})=>e))}static refreshLimit(e){return(e.ttl===void 0||e.ttl<Date.now())&&(e.rest=e.count,e.ttl=Date.now()+e.period),e}static refreshTtl(e){e.ttl=Date.now()+e.period}};var J={leading:!1,trailing:!0},h=p((t,e)=>{let{delay:r,limit:o,context:i,rejectOnCancel:s,maxWait:d,leading:f,order:u,limiter:m}={...J,...e},a=m||new T(O(o||r)),I=[],n=[],c=()=>{n.forEach(E=>E.complete()),n.length=0},D,C,y,x,g=(...E)=>{l(D),I.push(E),x===void 0&&(a.reset(),x=a.getNextQueueSize());let k=f&&x>0;if((x>0||y===void 0)&&(a.decrease(),x-=1,y=new Promise((A,j)=>{n.push({complete:()=>L(A,t,u==="fifo"?I.shift():I.pop(),i),fail:b.bind(void 0,j)})})),a.resetTtl(),D=setTimeout(g.flush,a.getNextDelay()),k){let A=y;return y=void 0,c(),A}return d&&!C&&(C=setTimeout(g.flush,d)),y};return g.flush=()=>{a.reset(),y=void 0,x=void 0,c(),g.cancel()},g.cancel=()=>{l(D),l(C),x=void 0,y=void 0,D=void 0,C=void 0,I.length=0,s&&n.forEach(E=>E.fail()),n.length=0},g});var M=p((t,e)=>{let r=typeof e.delay=="number"?{period:e.delay,count:Number.POSITIVE_INFINITY}:e.delay,o=typeof e.delay=="number"?e.delay:void 0;return h(t,{maxWait:o,...e,delay:r,order:"fifo"})});var U=p((t,e)=>{let r,{delay:o,limit:i,context:s,rejectOnCancel:d,limiter:f}=e,u=f||new T(O(i||o)),m=[],a=(n,c)=>{l(r),c.reset(),Y(n,c),r=Q(n,c,a)},I=(...n)=>new Promise((c,D)=>{m.push({complete:L.bind(void 0,c,t,n,s),fail:b.bind(void 0,D)}),a(m,u)});return I.flush=()=>{m.forEach(n=>n.complete()),I.cancel()},I.cancel=()=>{d&&m.forEach(n=>n.fail()),m.length=0,l(r)},I});function Y(t,e){for(;t.length>0&&e.isAllowed();)e.decrease(),t.shift()?.complete()}function B(t,e,r){return setTimeout(()=>r(t,e),e.getNextDelay())}function Q(t,e,r){return t.length>0?B(t,e,r):void 0}function W(t,e,r,o){if(typeof t=="object"){let{target:s,delay:d,context:f,limit:u}=t;return W(s,d,f,u)}q(t,e);let i=(...s)=>{let{timeout:d,target:f,limit:u,context:m}=i,a=V(u);return clearTimeout(d),(a===void 0||a>0)&&(i.limit=a,i.args=s,i.timeout=setTimeout(i.bind(i,...s),e)),f.call(m,...s)};return i.target=t,i.delay=e,i.limit=o,i.context=r,i}function q(t,e){if(typeof t!="function")throw new TypeError("repeater: target must be callable");if(!e)throw new TypeError("repeater: delay must not be empty")}function V(t){return typeof t=="number"?t-1:void 0}var G=p((t,e)=>{let r={...e,target:t,delay:typeof e.delay=="number"?e.delay:e.delay.period,limit:Array.isArray(e.limit)?e.limit[0].period:e.limit?.period},o=(W?.default||W)(r);return o.cancel=()=>{l(o.timeout)},o.flush=()=>{t.call(o.context,o.args),l(o.timeout)},o});var K=p((t,e)=>{let r=typeof e.delay=="number"?e.delay:void 0;return h(t,{maxWait:r,...e})});var X=p((t,e)=>{let r=typeof e.delay=="number"?e.delay:void 0;return h(t,{leading:!0,maxWait:r,...e})});export{S as DEFAULT_DELAY,T as Limiter,w as REJECTED,N as REJECTED_ON_CANCEL,p as adapter,F as assert,H as assertFn,L as complete,h as debounce,M as delay,l as dropTimeout,v as fail,b as failOnCancel,O as normalizeDelay,U as ratelimit,G as repeat,K as stabilize,X as throttle}; //# sourceMappingURL=index.mjs.map