import type { Worker } from 'node:cluster'
import type { MessagePort } from 'node:worker_threads'
-import { EventEmitter } from 'node:events'
import { performance } from 'node:perf_hooks'
import type {
TaskFunctions,
TaskSyncFunction,
} from './task-functions.js'
-import type { AbortTaskEventDetail } from './worker-types.js'
import {
buildTaskFunctionProperties,
MainWorker extends MessagePort | Worker,
Data = unknown,
Response = unknown
-> extends EventEmitter {
+> {
/**
* Handler id of the `activeInterval` worker activity check.
*/
taskFunctions: TaskFunction<Data, Response> | TaskFunctions<Data, Response>,
protected opts: WorkerOptions = DEFAULT_WORKER_OPTIONS
) {
- super()
if (this.isMain == null) {
throw new Error('isMain parameter is mandatory')
}
`${string}-${string}-${string}-${string}-${string}`,
() => void
>()
- this.on('abortTask', (eventDetail: AbortTaskEventDetail) => {
- const { taskId } = eventDetail
- if (this.taskAbortFunctions.has(taskId)) {
- this.taskAbortFunctions.get(taskId)?.()
- }
- })
this.checkWorkerOptions(this.opts)
if (!this.isMain) {
// Should be once() but Node.js on windows has a bug that prevents it from working
this.sendToMainWorker({ kill: 'failure' })
}
}
- this.removeAllListeners()
}
/**
this.run(message)
} else if (taskOperation === 'abort' && taskId != null) {
// Abort task operation message received
- this.emit('abortTask', { taskId })
+ if (this.taskAbortFunctions.has(taskId)) {
+ this.taskAbortFunctions.get(taskId)?.()
+ }
} else if (kill === true) {
// Kill message received
this.handleKillMessage(message)