Data = unknown,
Response = unknown
> extends AbstractWorker<MessagePort, Data, Response> {
+ /**
+ * Message port used to communicate with the main thread.
+ */
+ private port!: MessagePort
/**
* Constructs a new poolifier thread worker.
*
super(
'worker-thread-pool:poolifier',
isMainThread,
- taskFunctions,
parentPort as MessagePort,
+ taskFunctions,
opts
)
}
+ /** @inheritDoc */
+ protected handleReadyMessage (message: MessageValue<Data>): void {
+ if (
+ !this.isMain &&
+ message.workerId === this.id &&
+ message.ready != null &&
+ message.port != null
+ ) {
+ this.port = message.port
+ this.port.on('message', this.messageListener.bind(this))
+ this.sendToMainWorker({ ready: true, workerId: this.id })
+ }
+ }
+
+ /** @inheritDoc */
+ protected handleKillMessage (message: MessageValue<Data, unknown>): void {
+ super.handleKillMessage(message)
+ this.port?.unref()
+ this.port?.close()
+ }
+
+ /** @inheritDoc */
protected get id (): number {
return threadId
}
/** @inheritDoc */
protected sendToMainWorker (message: MessageValue<Response>): void {
- this.getMainWorker().postMessage(message)
+ this.port.postMessage(message)
}
/** @inheritDoc */