UNPKG

@lzptec/concurrency

Version:

A Lightweight concurrency manager

2 lines 4.43 kB
'use strict';var chunkCA3T2TT7_cjs=require('./chunk-CA3T2TT7.cjs'),chunkYQZ4NCQG_cjs=require('./chunk-YQZ4NCQG.cjs'),chunkZFKFOLTS_cjs=require('./chunk-ZFKFOLTS.cjs');function W(b){if(!Number.isInteger(b.maxConcurrency))throw new Error("Parameter `maxConcurrency` must be a integer!");if(!Number.isInteger(b.interval)||b.interval<0)throw new Error("Parameter `interval` must be a positive integer greater than 0!")}var u,y,g,w,k,f,a,m,x,X,C=class extends chunkCA3T2TT7_cjs.n{constructor(t){super();chunkZFKFOLTS_cjs.d(this,x);chunkZFKFOLTS_cjs.d(this,u);chunkZFKFOLTS_cjs.d(this,y,new chunkYQZ4NCQG_cjs.a);chunkZFKFOLTS_cjs.d(this,g,Promise.resolve());chunkZFKFOLTS_cjs.d(this,w,[]);chunkZFKFOLTS_cjs.d(this,k,0);chunkZFKFOLTS_cjs.d(this,f);this.options=t;}static async forEach(t){chunkCA3T2TT7_cjs.l(t.task);let{task:e}=chunkCA3T2TT7_cjs.c(t.task);await chunkZFKFOLTS_cjs.f(this,a,m).call(this,chunkZFKFOLTS_cjs.b(chunkZFKFOLTS_cjs.a({},t),{task:e}));}static async map(t){chunkCA3T2TT7_cjs.l(t.task);let{task:e,results:r}=chunkCA3T2TT7_cjs.d(t.task);return await chunkZFKFOLTS_cjs.f(this,a,m).call(this,chunkZFKFOLTS_cjs.b(chunkZFKFOLTS_cjs.a({},t),{task:e})),r}static async mapSettled(t){chunkCA3T2TT7_cjs.l(t.task);let{task:e,results:r}=chunkCA3T2TT7_cjs.e(t.task);return await chunkZFKFOLTS_cjs.f(this,a,m).call(this,chunkZFKFOLTS_cjs.b(chunkZFKFOLTS_cjs.a({},t),{task:e})),r}static async filter(t){chunkCA3T2TT7_cjs.m(t.predicate);let{task:e,results:r}=chunkCA3T2TT7_cjs.f(t.predicate);return await chunkZFKFOLTS_cjs.f(this,a,m).call(this,chunkZFKFOLTS_cjs.b(chunkZFKFOLTS_cjs.a({},t),{task:e})),r}static async some(t){chunkCA3T2TT7_cjs.m(t.predicate);let{task:e,results:r}=chunkCA3T2TT7_cjs.g(t.predicate);return await chunkZFKFOLTS_cjs.f(this,a,m).call(this,chunkZFKFOLTS_cjs.b(chunkZFKFOLTS_cjs.a({},t),{task:e})),r[0]}static async find(t){chunkCA3T2TT7_cjs.m(t.predicate);let{task:e,results:r}=chunkCA3T2TT7_cjs.h(t.predicate);return await chunkZFKFOLTS_cjs.f(this,a,m).call(this,chunkZFKFOLTS_cjs.b(chunkZFKFOLTS_cjs.a({},t),{task:e})),r[0]}static async every(t){chunkCA3T2TT7_cjs.m(t.predicate);let{task:e,results:r}=chunkCA3T2TT7_cjs.i(t.predicate);return await chunkZFKFOLTS_cjs.f(this,a,m).call(this,chunkZFKFOLTS_cjs.b(chunkZFKFOLTS_cjs.a({},t),{task:e})),r[0]}static async group(t){chunkCA3T2TT7_cjs.l(t.task);let{task:e,results:r}=chunkCA3T2TT7_cjs.j(t.task);return await chunkZFKFOLTS_cjs.f(this,a,m).call(this,chunkZFKFOLTS_cjs.b(chunkZFKFOLTS_cjs.a({},t),{task:e})),Object.fromEntries(r[0])}async run(t,...e){let{interval:r}=chunkZFKFOLTS_cjs.c(this,u),l=await chunkZFKFOLTS_cjs.c(this,y).acquire();chunkZFKFOLTS_cjs.c(this,w).push(l);let n=performance.now();return n-chunkZFKFOLTS_cjs.c(this,k)>=r&&chunkZFKFOLTS_cjs.e(this,k,n),chunkZFKFOLTS_cjs.c(this,f)||chunkZFKFOLTS_cjs.e(this,f,setTimeout(chunkZFKFOLTS_cjs.f(this,x,X).bind(this),r)),chunkZFKFOLTS_cjs.c(this,g).then(()=>t(...e))}async[chunkCA3T2TT7_cjs.b](t,e){let r=chunkCA3T2TT7_cjs.k(t),l=false,{maxConcurrency:n}=chunkZFKFOLTS_cjs.c(this,u),B=d=>{throw l=true,d};await new Promise((d,E)=>{let A=n;for(let S=0;S<n;S++)Promise.resolve(r.next()).then(async T=>{for(;!l&&!T.done;)await this.run(e,await T.value),T=await r.next();}).then(()=>--A===0?d():void 0).catch(B).catch(E);});}set options(t){W(t),chunkZFKFOLTS_cjs.e(this,u,chunkZFKFOLTS_cjs.a(chunkZFKFOLTS_cjs.a({},chunkZFKFOLTS_cjs.c(this,u)),t)),chunkZFKFOLTS_cjs.c(this,y).options=chunkZFKFOLTS_cjs.c(this,u);}get options(){return chunkZFKFOLTS_cjs.a({},chunkZFKFOLTS_cjs.c(this,u))}};u=new WeakMap,y=new WeakMap,g=new WeakMap,w=new WeakMap,k=new WeakMap,f=new WeakMap,a=new WeakSet,m=async function(t){W(t);let e=chunkCA3T2TT7_cjs.k(t.input),r=false,l=A=>{throw r=true,A},n=performance.now(),{interval:B,maxConcurrency:d,task:E}=t;await new Promise((A,S)=>{let T=d;for(let L=0;L<d;L++)(async()=>{var N;for(;!r;){let R=await e.next();if(R.done||r)break;if(await E(await R.value)===chunkCA3T2TT7_cjs.a){r=true,(N=e.return)==null||N.call(e);break}let I=performance.now();I-n>=B&&(n=I);let G=n+B-I;G>1&&await new Promise(Y=>setTimeout(()=>Y(),G));}})().then(()=>--T===0?A():void 0).catch(l).catch(S);});},x=new WeakSet,X=function(){chunkZFKFOLTS_cjs.c(this,w).forEach(t=>t.release()),chunkZFKFOLTS_cjs.e(this,w,[]),chunkZFKFOLTS_cjs.e(this,f,void 0);},chunkZFKFOLTS_cjs.d(C,a);Object.freeze(C);exports.a=C;//# sourceMappingURL=chunk-Q4UQKLMH.cjs.map //# sourceMappingURL=chunk-Q4UQKLMH.cjs.map