transferList?: TransferListItem[]
): void {
(
- this.getWorkerInfo(workerNodeKey).messageChannel as MessageChannel
+ this.workerNodes[workerNodeKey].messageChannel as MessageChannel
).port1.postMessage(message, transferList)
}
protected sendStartupMessageToWorker (workerNodeKey: number): void {
const worker = this.workerNodes[workerNodeKey].worker
const port2: MessagePort = (
- this.getWorkerInfo(workerNodeKey).messageChannel as MessageChannel
+ this.workerNodes[workerNodeKey].messageChannel as MessageChannel
).port2
worker.postMessage(
{
listener: (message: MessageValue<Message>) => void
): void {
(
- this.getWorkerInfo(workerNodeKey).messageChannel as MessageChannel
+ this.workerNodes[workerNodeKey].messageChannel as MessageChannel
).port1.on('message', listener)
}
implements IWorkerNode<Worker, Data> {
public readonly worker: Worker
public readonly info: WorkerInfo
+ public messageChannel?: MessageChannel
public usage: WorkerUsage
private readonly tasksUsage: Map<string, WorkerUsage>
private readonly tasksQueue: Queue<Task<Data>>
constructor (worker: Worker, workerType: WorkerType) {
this.worker = worker
this.info = this.initWorkerInfo(worker, workerType)
+ if (workerType === WorkerTypes.thread) {
+ this.messageChannel = new MessageChannel()
+ }
this.usage = this.initWorkerUsage()
this.tasksUsage = new Map<string, WorkerUsage>()
this.tasksQueue = new Queue<Task<Data>>()
/** @inheritdoc */
public closeChannel (): void {
- if (this.info.messageChannel != null) {
- this.info.messageChannel?.port1.unref()
- this.info.messageChannel?.port2.unref()
- this.info.messageChannel?.port1.close()
- this.info.messageChannel?.port2.close()
- delete this.info.messageChannel
+ if (this.messageChannel != null) {
+ this.messageChannel?.port1.unref()
+ this.messageChannel?.port2.unref()
+ this.messageChannel?.port1.close()
+ this.messageChannel?.port2.close()
+ delete this.messageChannel
}
}
id: this.getWorkerId(worker, workerType),
type: workerType,
dynamic: false,
- ready: false,
- ...(workerType === WorkerTypes.thread && {
- messageChannel: new MessageChannel()
- })
+ ready: false
}
}