Benchmarks and performance enhancements (#209)
[poolifier.git] / src / utility-types.ts
CommitLineData
838898f1
S
1import type { Worker } from 'cluster'
2import type { MessagePort } from 'worker_threads'
be0676b3 3import type { IWorker } from './pools/abstract-pool'
1a81f8af 4import type { KillBehavior } from './worker/worker-options'
838898f1 5
729c563d 6/**
3832ad95 7 * Make all properties in T non-readonly.
729c563d 8 */
325f50bc
S
9export type Draft<T> = { -readonly [P in keyof T]?: T[P] }
10
729c563d
S
11/**
12 * Message object that is passed between worker and main worker.
13 */
838898f1
S
14export interface MessageValue<
15 Data = unknown,
16 MainWorker extends Worker | MessagePort | unknown = unknown
17> {
729c563d
S
18 /**
19 * Input data that will be passed to the worker.
20 */
325f50bc 21 readonly data?: Data
729c563d
S
22 /**
23 * ID of the message.
24 */
325f50bc 25 readonly id?: number
729c563d
S
26 /**
27 * Kill code.
28 */
1a81f8af 29 readonly kill?: KillBehavior | 1
729c563d
S
30 /**
31 * Error.
32 */
325f50bc 33 readonly error?: string
729c563d
S
34 /**
35 * Reference to main worker.
36 *
37 * _Only for internal use_
38 */
838898f1 39 readonly parent?: MainWorker
325f50bc 40}
be0676b3
APA
41
42/**
43 * An object holding the worker that will be used to resolve/rejects the promise later on.
44 *
45 * @template Worker Type of worker.
46 * @template Response Type of response of execution. This can only be serializable data.
47 */
48export interface PromiseWorkerResponseWrapper<
49 Worker extends IWorker,
50 Response = unknown
51> {
52 /**
53 * Resolve callback to fulfill the promise.
54 */
55 readonly resolve: (value: Response) => void
56 /**
57 * Reject callback to reject the promise.
58 */
59 readonly reject: (reason?: string) => void
60 /**
61 * The worker that has the assigned task.
62 */
63 readonly worker: Worker
64}