De-duplicate code for workers (#154)
[poolifier.git] / src / utility-types.ts
1 import type { Worker } from 'cluster'
2 import type { MessagePort } from 'worker_threads'
3
4 /**
5 * Make all properties in T non-readonly
6 */
7 export type Draft<T> = { -readonly [P in keyof T]?: T[P] }
8
9 /**
10 * Serializable primitive JSON value.
11 */
12 export type JSONPrimitive = number | boolean | string | null
13 /**
14 * Serializable JSON value.
15 */
16 // eslint-disable-next-line no-use-before-define
17 export type JSONValue = JSONPrimitive | JSONArray | JSONObject
18 /**
19 * Serializable JSON object.
20 */
21 export type JSONObject = { [k: string]: JSONValue }
22 /**
23 * Serializable JSON array.
24 */
25 export type JSONArray = Array<JSONValue>
26
27 /**
28 * Message object that is passed between worker and main worker.
29 */
30 export interface MessageValue<
31 Data = unknown,
32 MainWorker extends Worker | MessagePort | unknown = unknown
33 > {
34 /**
35 * Input data that will be passed to the worker.
36 */
37 readonly data?: Data
38 /**
39 * ID of the message.
40 */
41 readonly id?: number
42 /**
43 * Kill code.
44 */
45 readonly kill?: number
46 /**
47 * Error.
48 */
49 readonly error?: string
50 /**
51 * Reference to main worker.
52 *
53 * _Only for internal use_
54 */
55 readonly parent?: MainWorker
56 }