"dev": true
},
"uglify-js": {
- "version": "3.12.7",
- "resolved": "https://registry.npmjs.org/uglify-js/-/uglify-js-3.12.7.tgz",
- "integrity": "sha512-SIZhkoh+U/wjW+BHGhVwE9nt8tWJspncloBcFapkpGRwNPqcH8pzX36BXe3TPBjzHWPMUZotpCigak/udWNr1Q==",
+ "version": "3.12.8",
+ "resolved": "https://registry.npmjs.org/uglify-js/-/uglify-js-3.12.8.tgz",
+ "integrity": "sha512-fvBeuXOsvqjecUtF/l1dwsrrf5y2BCUk9AOJGzGcm6tE7vegku5u/YvqjyDaAGr422PLoLnrxg3EnRvTqsdC1w==",
"dev": true,
"optional": true
},
message: MessageValue<Data>
): void
- protected abstract registerWorkerMessageListener (
- port: Worker,
- listener: (message: MessageValue<Response>) => void
- ): void
+ protected abstract registerWorkerMessageListener<
+ Message extends Data | Response
+ > (worker: Worker, listener: (message: MessageValue<Message>) => void): void
- protected abstract unregisterWorkerMessageListener (
- port: Worker,
- listener: (message: MessageValue<Response>) => void
- ): void
+ protected abstract unregisterWorkerMessageListener<
+ Message extends Data | Response
+ > (worker: Worker, listener: (message: MessageValue<Message>) => void): void
protected internalExecute (
worker: Worker,
import type { Worker } from 'cluster'
-import type { JSONValue, MessageValue } from '../../utility-types'
+import type { JSONValue } from '../../utility-types'
import type { ClusterPoolOptions } from './fixed'
import { FixedClusterPool } from './fixed'
* @returns Cluster worker.
*/
protected chooseWorker (): Worker {
- let worker: Worker | undefined
- for (const entry of this.tasks) {
- if (entry[1] === 0) {
- worker = entry[0]
- break
+ for (const [worker, numberOfTasks] of this.tasks) {
+ if (numberOfTasks === 0) {
+ // A worker is free, use it
+ return worker
}
}
- 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)
- }
- })
- return worker
+ 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()
+ this.registerWorkerMessageListener<Data>(worker, message => {
+ if (message.kill) {
+ this.sendToWorker(worker, { kill: 1 })
+ void this.destroyWorker(worker)
+ }
+ })
+ return worker
}
}
worker.send(message)
}
- protected registerWorkerMessageListener (
- port: Worker,
- listener: (message: MessageValue<Response>) => void
+ protected registerWorkerMessageListener<Message extends Data | Response> (
+ worker: Worker,
+ listener: (message: MessageValue<Message>) => void
): void {
- port.on('message', listener)
+ worker.on('message', listener)
}
- protected unregisterWorkerMessageListener (
- port: Worker,
- listener: (message: MessageValue<Response>) => void
+ protected unregisterWorkerMessageListener<Message extends Data | Response> (
+ worker: Worker,
+ listener: (message: MessageValue<Message>) => void
): void {
- port.removeListener('message', listener)
+ worker.removeListener('message', listener)
}
protected createWorker (): Worker {
-import type { JSONValue, MessageValue } from '../../utility-types'
+import type { JSONValue } from '../../utility-types'
import type { PoolOptions } from '../abstract-pool'
import type { ThreadWorkerWithMessageChannel } from './fixed'
import { FixedThreadPool } from './fixed'
* @returns Thread worker.
*/
protected chooseWorker (): ThreadWorkerWithMessageChannel {
- let worker: ThreadWorkerWithMessageChannel | undefined
- for (const entry of this.tasks) {
- if (entry[1] === 0) {
- worker = entry[0]
- break
+ for (const [worker, numberOfTasks] of this.tasks) {
+ if (numberOfTasks === 0) {
+ // A worker is free, use it
+ return worker
}
}
- 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.port2?.on('message', (message: MessageValue<Data>) => {
- if (message.kill) {
- this.sendToWorker(worker, { kill: 1 })
- void this.destroyWorker(worker)
- }
- })
- return worker
+ 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()
+ this.registerWorkerMessageListener<Data>(worker, message => {
+ if (message.kill) {
+ this.sendToWorker(worker, { kill: 1 })
+ void this.destroyWorker(worker)
+ }
+ })
+ return worker
}
}
worker.postMessage(message)
}
- protected registerWorkerMessageListener (
- port: ThreadWorkerWithMessageChannel,
- listener: (message: MessageValue<Response>) => void
+ protected registerWorkerMessageListener<Message extends Data | Response> (
+ messageChannel: ThreadWorkerWithMessageChannel,
+ listener: (message: MessageValue<Message>) => void
): void {
- port.port2?.on('message', listener)
+ messageChannel.port2?.on('message', listener)
}
- protected unregisterWorkerMessageListener (
- port: ThreadWorkerWithMessageChannel,
- listener: (message: MessageValue<Response>) => void
+ protected unregisterWorkerMessageListener<Message extends Data | Response> (
+ messageChannel: ThreadWorkerWithMessageChannel,
+ listener: (message: MessageValue<Message>) => void
): void {
- port.port2?.removeListener('message', listener)
+ messageChannel.port2?.removeListener('message', listener)
}
protected createWorker (): ThreadWorkerWithMessageChannel {