type MessageChannel,
type MessagePort,
SHARE_ENV,
+ type TransferListItem,
Worker,
type WorkerOptions,
isMainThread
// FIXME: wait for tasks to be finished
const workerNode = this.workerNodes[workerNodeKey]
const worker = workerNode.worker
- const workerExitPromise = new Promise<void>(resolve => {
+ const waitWorkerExit = new Promise<void>(resolve => {
worker.on('exit', () => {
resolve()
})
})
- this.sendToWorker(workerNodeKey, { kill: true, workerId: worker.threadId })
+ await this.sendKillMessageToWorker(
+ workerNodeKey,
+ workerNode.info.id as number
+ )
workerNode.closeChannel()
await worker.terminate()
- await workerExitPromise
+ await waitWorkerExit
}
/** @inheritDoc */
protected sendToWorker (
workerNodeKey: number,
- message: MessageValue<Data>
+ message: MessageValue<Data>,
+ transferList?: TransferListItem[]
): void {
(
- this.getWorkerInfo(workerNodeKey).messageChannel as MessageChannel
- ).port1.postMessage(message)
+ this.workerNodes[workerNodeKey].messageChannel as MessageChannel
+ ).port1.postMessage(message, transferList)
}
/** @inheritDoc */
protected sendStartupMessageToWorker (workerNodeKey: number): void {
- const worker = this.workerNodes[workerNodeKey].worker
- const port2: MessagePort = (
- this.getWorkerInfo(workerNodeKey).messageChannel as MessageChannel
- ).port2
+ const workerNode = this.workerNodes[workerNodeKey]
+ const worker = workerNode.worker
+ const port2: MessagePort = (workerNode.messageChannel as MessageChannel)
+ .port2
worker.postMessage(
{
ready: false,
- workerId: worker.threadId,
+ workerId: workerNode.info.id,
port: port2
},
[port2]
listener: (message: MessageValue<Message>) => void
): void {
(
- this.getWorkerInfo(workerNodeKey).messageChannel as MessageChannel
+ this.workerNodes[workerNodeKey].messageChannel as MessageChannel
).port1.on('message', listener)
}
return WorkerTypes.thread
}
- /** @inheritDoc */
- protected get minSize (): number {
- return this.numberOfWorkers
- }
-
- /** @inheritDoc */
- protected get maxSize (): number {
- return this.numberOfWorkers
- }
-
/** @inheritDoc */
protected get busy (): boolean {
return this.internalBusy()