UNPKG

@csi-foxbyte/cityjson-to-3d-tiles

Version:

A Node.js library that converts CityJSON files into Cesium 3D Tiles—complete with automatic texture atlas packing, Basis compression, three LOD levels, and customizable threading.

2 lines • 1.42 kB
class l{constructor(e,s=16){this._workers=[];this._busyList=[];this._workQueue=[];this._terminated=false;for(let r=0;r<s;r++)this._workers.push(e()),this._busyList.push(false);}async messageWorkers(e){const s=[];for(const r of this._workers)s.push(new Promise((i,t)=>{r.removeAllListeners(),r.on("message",o=>{i(o);}),r.on("error",o=>t(o)),r.postMessage(e);}));return await Promise.all(s),true}async run(e){if(this._terminated)throw new Error("WorkerPool was terminated, please use new WorkerPool!");let s=()=>{},r=()=>{};const i=new Promise((t,o)=>{s=t,r=o;});return this._workQueue.push({resolve:s,reject:r,args:e}),this._scheduleWork(),i}async _runWorker(e,s,r,i){this._busyList[e]=true;const t=this._workers[e];t.removeAllListeners(),t.on("message",o=>{r(o),this._busyList[e]=false,this._scheduleWork();}),t.on("error",o=>{i(o),this._busyList[e]=false,this._scheduleWork();}),t.postMessage(s);}async _scheduleWork(){if(this._workQueue.length!==0)for(let e=0;e<this._busyList.length;e++){if(this._busyList[e])continue;const{resolve:s,reject:r,args:i}=this._workQueue.shift();return this._runWorker(e,i,s,r)}}terminate(){for(this._terminated=true;this._workQueue.length>0;){const{reject:e}=this._workQueue.pop();e(new Error("WorkerPool terminated before job was processed"));}for(const e of this._workers)e.terminate();}}export{l as WorkerPool};//# sourceMappingURL=workerPool.js.map //# sourceMappingURL=workerPool.js.map