@lzptec/concurrency
Version:
A Lightweight concurrency manager
2 lines • 1.35 kB
JavaScript
;var chunkZFKFOLTS_cjs=require('./chunk-ZFKFOLTS.cjs');var o,t,i,u,m,y,d=class{constructor(e){chunkZFKFOLTS_cjs.d(this,m);chunkZFKFOLTS_cjs.d(this,o,new chunkZFKFOLTS_cjs.h);chunkZFKFOLTS_cjs.d(this,t,0);chunkZFKFOLTS_cjs.d(this,i,1);chunkZFKFOLTS_cjs.d(this,u,Promise.resolve());var s;chunkZFKFOLTS_cjs.e(this,i,(s=e==null?void 0:e.maxConcurrency)!=null?s:1);}async run(e,...s){let{release:a}=await this.acquire();try{return await e(s)}finally{a();}}async acquire(){let e,s=new Promise(a=>{e=a;});return new Promise(a=>{chunkZFKFOLTS_cjs.c(this,o).enqueue({promise:s,release:()=>a({release:e})}),chunkZFKFOLTS_cjs.f(this,m,y).call(this);})}get acquired(){return chunkZFKFOLTS_cjs.c(this,t)}get options(){return {maxConcurrency:chunkZFKFOLTS_cjs.c(this,i)}}set options(e){chunkZFKFOLTS_cjs.e(this,i,e.maxConcurrency);}};o=new WeakMap,t=new WeakMap,i=new WeakMap,u=new WeakMap,m=new WeakSet,y=function(){if(!chunkZFKFOLTS_cjs.c(this,o).length||chunkZFKFOLTS_cjs.c(this,t)>=chunkZFKFOLTS_cjs.c(this,i))return;let{release:e,promise:s}=chunkZFKFOLTS_cjs.c(this,o).dequeue();chunkZFKFOLTS_cjs.g(this,t)._++,chunkZFKFOLTS_cjs.c(this,u).then(e).then(()=>s).finally(()=>{chunkZFKFOLTS_cjs.g(this,t)._--,chunkZFKFOLTS_cjs.f(this,m,y).call(this);});};exports.a=d;//# sourceMappingURL=chunk-YQZ4NCQG.cjs.map
//# sourceMappingURL=chunk-YQZ4NCQG.cjs.map