1import{b9 as o,eZ as _,ba as p,e_ as f}from"./index-BdMpjrZC.js";import{QueueProcessor as b}from"./QueueProcessor-DkVB7fRQ.js";function v(a,e,t,s){return new w(a,e,t,s)}class m{constructor(e,t,s){this._executor=e,this._data=t,this._impl=s,this._closed=!1,this._resolver=o(),this._impl.open(this)}assertOpen(){if(this._closed)throw new Error("Batch closed.")}get data(){return this.assertOpen(),this._data}get results(){return this._resolver.promise}send(){try{this.assertOpen(),this._closed=!0,this._executor.resume(this),this._impl.close(this),_(this._resolver,this._impl.execute(this._data))}catch(e){this._resolver.reject(e)}}}class w{constructor(e,t,s,r){if(this._runJob=t,this._maxRunning=s,this._abortSignal=r,this._jobIdSeq=0,this._running=new Set,this._blocked=new Set,this._openBatches=new Map,this._state="ready",this._runResolver=null,s<1)throw new Error(`_maxRunning=${s} but cannot be < 1`);this._todo=e[Symbol.iterator](),this._queue=new b({concurrency:1,process:async i=>{if(i.type==="start"){const{id:n,args:d}=i,h=o(),u=o(),l={id:n,interrupt:h,finished:u.promise};return _(u,this._runJob(d,{id:n,yieldFor:c=>this.yieldFor(l,c)}).then(()=>{this._blocked.has(n)&&this._fail(new Error(`job ${n} completed before resuming`)),this._running.delete(n)||this._fail(new Error(`job ${n} not running?`))})),await Promise.race([h.promise,l.finished]),void this._tryQueue()}if(i.type==="continue")return i.job.interrupt=o(),i.data.status==="fulfilled"?i.continuation.resolve(i.data.value):i.continuation.reject(i.data.reason),await Promise.race([i.job.interrupt.promise,i.job.finished]),void this._tryQueue()}})}openBatch(e,t){return new m(this,e,t)}_fail(e,t=!1){if(this._runResolver?.reject(e),this._state="stopped",!t)throw e}_schedule(e){this._queue.push(e).catch(t=>this._fail(t,!0))}_tryQueue(){if(this._state==="running")if(this._abortSignal?.aborted)this._fail(p(),!0);else{if(this._running.size<this._maxRunning){const e=this._todo.next();if(!e.done){const t=this._jobIdSeq++;return this._running.add(t),void this._schedule({type:"start",id:t,args:e.value})}}if(this._running.size===0)return this._runResolver?.resolve(),this._runResolver=null,void(this._state="stopped");if(this._blocked.size===this._running.size){let e=null,t=-1/0;for(const[s,r]of this._openBatches)r.size>t&&(e=s,t=r.size);return e==null&&this._fail(new Error("deadlock")),void e.send()}}}resume(e){const t=this._openBatches.get(e);if(this._openBatches.delete(e),t!=null)for(const s of t)this._blocked.delete(s)||this._fail(new Error(`job ${s} not suspended`))}async yieldFor(e,t){t.assertOpen(),this._running.has(e.id)||this._fail(new Error(`job ${e.id} not running`)),this._blocked.has(e.id)&&this._fail(new Error(`job ${e.id} already suspended`)),this._blocked.add(e.id),f(this._openBatches,t,()=>new Set).add(e.id),e.interrupt==null&&this._fail(new Error(`job ${e.id} hasn't resumed yet`)),e.interrupt.resolve(),e.interrupt=null;const s=o();let r;try{r={status:"fulfilled",value:await t.results}}catch(i){r={status:"rejected",reason:i}}return this._schedule({type:"continue",continuation:s,data:r,job:e}),s.promise}run(){this._state!=="ready"&&this._fail(new Error(`executor not ready to start. state=${this._state}`));const e=o();return this._state="running",this._runResolver=e,this._tryQueue(),e.promise}}export{v as createBatchExecutor};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.