From: Jérôme Benoit Date: Sat, 15 Apr 2023 15:11:53 +0000 (+0200) Subject: refactor: add type aliases for worker function X-Git-Tag: v2.4.10~14 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=48ef910747c7eafb18ebfff83d8eb24e5f5be26c;p=poolifier.git refactor: add type aliases for worker function Signed-off-by: Jérôme Benoit --- diff --git a/src/index.ts b/src/index.ts index dce00380..d1482c83 100644 --- a/src/index.ts +++ b/src/index.ts @@ -40,6 +40,9 @@ export type { KillBehavior, WorkerOptions } from './worker/worker-options' export type { Draft, PromiseResponseWrapper, - MessageValue + MessageValue, + WorkerAsyncFunction, + WorkerFunction, + WorkerSyncFunction } from './utility-types' export type { CircularArray } from './circular-array' diff --git a/src/pools/worker.ts b/src/pools/worker.ts index 8d62712b..65b3a430 100644 --- a/src/pools/worker.ts +++ b/src/pools/worker.ts @@ -90,7 +90,7 @@ export interface IWorker { * Register an event listener. * * @param event - The event. - * @param handler - The event listener. + * @param handler - The event handler. */ on: ((event: 'message', handler: MessageHandler) => void) & ((event: 'error', handler: ErrorHandler) => void) & diff --git a/src/utility-types.ts b/src/utility-types.ts index fb0e435a..81a57698 100644 --- a/src/utility-types.ts +++ b/src/utility-types.ts @@ -39,6 +39,19 @@ export interface MessageValue< readonly parent?: MainWorker } +/** + * Worker function that can be executed types. + */ +export type WorkerSyncFunction = ( + data?: Data +) => Response +export type WorkerAsyncFunction = ( + data?: Data +) => Promise +export type WorkerFunction = + | WorkerSyncFunction + | WorkerAsyncFunction + /** * An object holding the execution response promise resolve/reject callbacks. * diff --git a/src/worker/abstract-worker.ts b/src/worker/abstract-worker.ts index 73eec1c6..5390ec02 100644 --- a/src/worker/abstract-worker.ts +++ b/src/worker/abstract-worker.ts @@ -1,7 +1,12 @@ 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' @@ -41,7 +46,7 @@ export abstract class AbstractWorker< public constructor ( type: string, protected readonly isMain: boolean, - fn: (data: Data) => Response | Promise, + fn: WorkerFunction, protected mainWorker: MainWorker | undefined | null, protected readonly opts: WorkerOptions = { /** @@ -83,7 +88,7 @@ export abstract class AbstractWorker< */ protected messageListener ( message: MessageValue, - fn: (data: Data) => Response | Promise + fn: WorkerFunction ): void { if (message.id != null && message.data != null) { // Task message received @@ -114,9 +119,7 @@ export abstract class AbstractWorker< * * @param fn - The function that should be defined. */ - private checkFunctionInput ( - fn: (data: Data) => Response | Promise - ): void { + private checkFunctionInput (fn: WorkerFunction): void { if (fn == null) throw new Error('fn parameter is mandatory') if (typeof fn !== 'function') { throw new TypeError('fn parameter is not a function') @@ -176,7 +179,7 @@ export abstract class AbstractWorker< * @param message - Input data for the given function. */ protected run ( - fn: (data?: Data) => Response, + fn: WorkerSyncFunction, message: MessageValue ): void { try { @@ -203,7 +206,7 @@ export abstract class AbstractWorker< * @param message - Input data for the given function. */ protected runAsync ( - fn: (data?: Data) => Promise, + fn: WorkerAsyncFunction, message: MessageValue ): void { const startTimestamp = performance.now() diff --git a/src/worker/cluster-worker.ts b/src/worker/cluster-worker.ts index 1d272981..8725dc89 100644 --- a/src/worker/cluster-worker.ts +++ b/src/worker/cluster-worker.ts @@ -1,6 +1,6 @@ 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' @@ -29,7 +29,7 @@ export class ClusterWorker< * @param opts - Options for the worker. */ public constructor ( - fn: (data: Data) => Response | Promise, + fn: WorkerFunction, opts: WorkerOptions = {} ) { super( diff --git a/src/worker/thread-worker.ts b/src/worker/thread-worker.ts index ad2bc644..03566a4c 100644 --- a/src/worker/thread-worker.ts +++ b/src/worker/thread-worker.ts @@ -1,6 +1,6 @@ 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' @@ -29,7 +29,7 @@ export class ThreadWorker< * @param opts - Options for the worker. */ public constructor ( - fn: (data: Data) => Response | Promise, + fn: WorkerFunction, opts: WorkerOptions = {} ) { super('worker-thread-pool:poolifier', isMainThread, fn, parentPort, opts)