+import type { MessageChannel } from 'node:worker_threads'
import type { CircularArray } from '../circular-array'
+import type { Task } from '../utility-types'
/**
* Callback invoked if the worker has received a message.
exitCode: number
) => void
-/**
- * Message object that is passed as a task between main worker and worker.
- *
- * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
- * @internal
- */
-export interface Task<Data = unknown> {
- /**
- * Worker id.
- */
- readonly workerId: number
- /**
- * Task name.
- */
- readonly name?: string
- /**
- * Task input data that will be passed to the worker.
- */
- readonly data?: Data
- /**
- * Timestamp.
- */
- readonly timestamp?: number
- /**
- * Message UUID.
- */
- readonly id?: string
-}
-
/**
* Measurement statistics.
*
* Enumeration of worker types.
*/
export const WorkerTypes = Object.freeze({
- cluster: 'cluster',
- thread: 'thread'
+ thread: 'thread',
+ cluster: 'cluster'
} as const)
/**
* Ready flag.
*/
ready: boolean
+ /**
+ * Message channel.
+ */
+ messageChannel?: MessageChannel
}
/**
* @param event - The event.
* @param handler - The event handler.
*/
- readonly on: ((event: 'message', handler: MessageHandler<this>) => void) &
+ readonly on: ((event: 'online', handler: OnlineHandler<this>) => void) &
+ ((event: 'message', handler: MessageHandler<this>) => void) &
((event: 'error', handler: ErrorHandler<this>) => void) &
- ((event: 'online', handler: OnlineHandler<this>) => void) &
((event: 'exit', handler: ExitHandler<this>) => void)
/**
* Registers a listener to the exit event that will only be performed once.
*/
readonly resetUsage: () => void
/**
- * Gets task usage statistics.
+ * Close communication channel.
+ */
+ readonly closeChannel: () => void
+ /**
+ * Gets task worker usage statistics.
*/
readonly getTaskWorkerUsage: (name: string) => WorkerUsage | undefined
}