UNPKG

@lzptec/concurrency

Version:

A Lightweight concurrency manager

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