UNPKG

@lzptec/concurrency

Version:

A Lightweight concurrency manager

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