UNPKG

@loaders.gl/core

Version:

Framework-independent loaders for 3D graphics formats

53 lines (44 loc) 1.26 kB
import WorkerPool from './worker-pool'; const DEFAULT_MAX_CONCURRENCY = 5; export default class WorkerFarm { constructor({ maxConcurrency = DEFAULT_MAX_CONCURRENCY, onMessage, onDebug = () => {} }) { this.maxConcurrency = maxConcurrency; this.onMessage = onMessage; this.onDebug = onDebug; this.workerPools = new Map(); } setProps(props) { if ('maxConcurrency' in props) { this.maxConcurrency = props.maxConcurrency; } if ('onDebug' in props) { this.onDebug = props.onDebug; } } destroy() { this.workerPools.forEach(workerPool => workerPool.destroy()); } async process(workerSource, workerName, data) { const workerPool = this._getWorkerPool(workerSource, workerName); return workerPool.process(data); } _getWorkerPool(workerSource, workerName) { let workerPool = this.workerPools.get(workerName); if (!workerPool) { workerPool = new WorkerPool({ source: workerSource, name: workerName, onMessage: this.onMessage, maxConcurrency: this.maxConcurrency, onDebug: this.onDebug }); this.workerPools.set(workerName, workerPool); } return workerPool; } } //# sourceMappingURL=worker-farm.js.map