readonly parent?: MainWorker
}
+/**
+ * Worker function that can be executed types.
+ */
+export type WorkerSyncFunction<Data = unknown, Response = unknown> = (
+ data?: Data
+) => Response
+export type WorkerAsyncFunction<Data = unknown, Response = unknown> = (
+ data?: Data
+) => Promise<Response>
+export type WorkerFunction<Data = unknown, Response = unknown> =
+ | WorkerSyncFunction<Data, Response>
+ | WorkerAsyncFunction<Data, Response>
+
/**
* An object holding the execution response promise resolve/reject callbacks.
*
import { AsyncResource } from 'node:async_hooks'
import type { Worker } from 'node:cluster'
import type { MessagePort } from 'node:worker_threads'
-import type { MessageValue } from '../utility-types'
+import type {
+ MessageValue,
+ WorkerAsyncFunction,
+ WorkerFunction,
+ WorkerSyncFunction
+} from '../utility-types'
import { EMPTY_FUNCTION } from '../utils'
import type { KillBehavior, WorkerOptions } from './worker-options'
import { KillBehaviors } from './worker-options'
public constructor (
type: string,
protected readonly isMain: boolean,
- fn: (data: Data) => Response | Promise<Response>,
+ fn: WorkerFunction<Data, Response>,
protected mainWorker: MainWorker | undefined | null,
protected readonly opts: WorkerOptions = {
/**
*/
protected messageListener (
message: MessageValue<Data, MainWorker>,
- fn: (data: Data) => Response | Promise<Response>
+ fn: WorkerFunction<Data, Response>
): void {
if (message.id != null && message.data != null) {
// Task message received
*
* @param fn - The function that should be defined.
*/
- private checkFunctionInput (
- fn: (data: Data) => Response | Promise<Response>
- ): void {
+ private checkFunctionInput (fn: WorkerFunction<Data, Response>): void {
if (fn == null) throw new Error('fn parameter is mandatory')
if (typeof fn !== 'function') {
throw new TypeError('fn parameter is not a function')
* @param message - Input data for the given function.
*/
protected run (
- fn: (data?: Data) => Response,
+ fn: WorkerSyncFunction<Data, Response>,
message: MessageValue<Data>
): void {
try {
* @param message - Input data for the given function.
*/
protected runAsync (
- fn: (data?: Data) => Promise<Response>,
+ fn: WorkerAsyncFunction<Data, Response>,
message: MessageValue<Data>
): void {
const startTimestamp = performance.now()
import type { Worker } from 'node:cluster'
import cluster from 'node:cluster'
-import type { MessageValue } from '../utility-types'
+import type { MessageValue, WorkerFunction } from '../utility-types'
import { AbstractWorker } from './abstract-worker'
import type { WorkerOptions } from './worker-options'
* @param opts - Options for the worker.
*/
public constructor (
- fn: (data: Data) => Response | Promise<Response>,
+ fn: WorkerFunction<Data, Response>,
opts: WorkerOptions = {}
) {
super(
import type { MessagePort } from 'node:worker_threads'
import { isMainThread, parentPort } from 'node:worker_threads'
-import type { MessageValue } from '../utility-types'
+import type { MessageValue, WorkerFunction } from '../utility-types'
import { AbstractWorker } from './abstract-worker'
import type { WorkerOptions } from './worker-options'
* @param opts - Options for the worker.
*/
public constructor (
- fn: (data: Data) => Response | Promise<Response>,
+ fn: WorkerFunction<Data, Response>,
opts: WorkerOptions = {}
) {
super('worker-thread-pool:poolifier', isMainThread, fn, parentPort, opts)