- if (worker) {
- // A worker is free, use it
- return worker
- } else {
- if (this.workers.length === this.max) {
- this.emitter.emit('FullPool')
- return super.chooseWorker()
- }
- // All workers are busy, create a new worker
- const worker = this.createAndSetupWorker()
- worker.on('message', (message: MessageValue<Data>) => {
- if (message.kill) {
- this.sendToWorker(worker, { kill: 1 })
- void this.destroyWorker(worker)
- this.removeWorker(worker)
- }
- })
- return worker
- }