X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Futility-types.ts;h=81a57698f5fa6a7dc5e631cbdec844ee68c44c2c;hb=761b0c73be0bf581f50a78123913f933bd405f52;hp=ec5ff44d861baeff4b0779a83e19f2b869ee560b;hpb=ae94ec4d34fe9266f1744eda946e1dc46372bdbe;p=poolifier.git diff --git a/src/utility-types.ts b/src/utility-types.ts index ec5ff44d..81a57698 100644 --- a/src/utility-types.ts +++ b/src/utility-types.ts @@ -1,28 +1,26 @@ import type { Worker as ClusterWorker } from 'node:cluster' import type { MessagePort } from 'node:worker_threads' import type { KillBehavior } from './worker/worker-options' -import type { IPoolWorker } from './pools/pool-worker' +import type { IWorker, Task } from './pools/worker' /** * Make all properties in T non-readonly. + * + * @typeParam T - Type in which properties will be non-readonly. */ export type Draft = { -readonly [P in keyof T]?: T[P] } /** - * Message object that is passed between worker and main worker. + * Message object that is passed between main worker and worker. + * + * @typeParam Data - Type of data sent to the worker. This can only be serializable data. + * @typeParam MainWorker - Type of main worker. + * @internal */ export interface MessageValue< Data = unknown, MainWorker extends ClusterWorker | MessagePort | unknown = unknown -> { - /** - * Input data that will be passed to the worker. - */ - readonly data?: Data - /** - * Id of the message. - */ - readonly id?: string +> extends Task { /** * Kill code. */ @@ -32,25 +30,37 @@ export interface MessageValue< */ readonly error?: string /** - * Task runtime. + * Runtime. */ - readonly taskRunTime?: number + readonly runTime?: number /** * Reference to main worker. - * - * Only for internal use. */ 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. * * @typeParam Worker - Type of worker. * @typeParam Response - Type of execution response. This can only be serializable data. + * @internal */ export interface PromiseResponseWrapper< - Worker extends IPoolWorker, + Worker extends IWorker, Response = unknown > { /** @@ -62,7 +72,7 @@ export interface PromiseResponseWrapper< */ readonly reject: (reason?: string) => void /** - * The worker handling the promise. + * The worker handling the execution. */ readonly worker: Worker }