* @param message - The received message.
*/
protected messageListener (message: MessageValue<Data>): void {
- if (message.workerId != null && message.workerId !== this.id) {
+ if (this.isMain) {
+ throw new Error('Cannot handle message to worker in main worker')
+ } else if (message.workerId != null && message.workerId !== this.id) {
throw new Error('Message worker id does not match worker id')
} else if (message.workerId === this.id) {
if (message.statistics != null) {
this.statistics = message.statistics
} else if (message.checkActive != null) {
// Check active message received
- !this.isMain && message.checkActive
- ? this.startCheckActive()
- : this.stopCheckActive()
+ message.checkActive ? this.startCheckActive() : this.stopCheckActive()
} else if (message.taskId != null && message.data != null) {
// Task message received
this.run(message)
* @param message - The kill message.
*/
protected handleKillMessage (message: MessageValue<Data>): void {
- if (!this.isMain) {
- this.stopCheckActive()
- this.opts.killHandler?.()
- this.emitDestroy()
- }
+ this.stopCheckActive()
+ this.opts.killHandler?.()
+ this.emitDestroy()
}
/**
* @throws {@link https://nodejs.org/api/errors.html#class-error} If the task function is not found.
*/
protected run (task: Task<Data>): void {
- if (this.isMain) {
- throw new Error('Cannot run a task in the main worker')
- }
const fn = this.getTaskFunction(task.name)
if (isAsyncFunction(fn)) {
this.runInAsyncScope(this.runAsync.bind(this), this, fn, task)
}
private updateLastTaskTimestamp (): void {
- if (!this.isMain && this.activeInterval != null) {
+ if (this.activeInterval != null) {
this.lastTaskTimestamp = performance.now()
}
}
/** @inheritDoc */
protected handleReadyMessage (message: MessageValue<Data>): void {
- if (!this.isMain && message.workerId === this.id && message.ready != null) {
+ if (message.workerId === this.id && message.ready != null) {
this.getMainWorker()?.on('message', this.messageListener.bind(this))
this.sendToMainWorker({ ready: true, workerId: this.id })
}