build: build fix package publishing on JSR
[poolifier.git] / src / pools / worker.ts
index f439c882a66a703e23cffafc6d02b5f0fcb7fef1..12a2e321b11c3724e739d0e7d9fd4fe708eb9a12 100644 (file)
@@ -1,5 +1,6 @@
-import type { MessageChannel, WorkerOptions } from 'node:worker_threads'
 import type { EventEmitter } from 'node:events'
+import type { MessageChannel, WorkerOptions } from 'node:worker_threads'
+
 import type { CircularArray } from '../circular-array.js'
 import type { Task } from '../utility-types.js'
 
@@ -133,10 +134,11 @@ export interface TaskStatistics {
 /**
  * Enumeration of worker types.
  */
-export const WorkerTypes = Object.freeze({
-  thread: 'thread',
-  cluster: 'cluster'
-} as const)
+export const WorkerTypes: Readonly<{ thread: 'thread', cluster: 'cluster' }> =
+  Object.freeze({
+    thread: 'thread',
+    cluster: 'cluster'
+  } as const)
 
 /**
  * Worker type.
@@ -212,7 +214,7 @@ export interface StrategyData {
 /**
  * Worker interface.
  */
-export interface IWorker {
+export interface IWorker extends EventEmitter {
   /**
    * Cluster worker id.
    */
@@ -227,14 +229,20 @@ export interface IWorker {
    * @param event - The event.
    * @param handler - The event handler.
    */
-  readonly on: (event: string, handler: EventHandler<this>) => void
+  readonly on: (event: string, handler: EventHandler<this>) => this
   /**
    * Registers once an event handler.
    *
    * @param event - The event.
    * @param handler - The event handler.
    */
-  readonly once: (event: string, handler: EventHandler<this>) => void
+  readonly once: (event: string, handler: EventHandler<this>) => this
+  /**
+   * Calling `unref()` on a worker allows the thread to exit if this is the only
+   * active handle in the event system. If the worker is already `unref()`ed calling`unref()` again has no effect.
+   * @since v10.5.0
+   */
+  readonly unref?: () => void
   /**
    * Stop all JavaScript execution in the worker thread as soon as possible.
    * Returns a Promise for the exit code that is fulfilled when the `'exit' event` is emitted.
@@ -258,7 +266,7 @@ export interface IWorker {
 export interface WorkerNodeOptions {
   workerOptions?: WorkerOptions
   env?: Record<string, unknown>
-  tasksQueueBackPressureSize: number
+  tasksQueueBackPressureSize: number | undefined
 }
 
 /**
@@ -388,6 +396,6 @@ export interface IWorkerNode<Worker extends IWorker, Data = unknown>
  * @internal
  */
 export interface WorkerNodeEventDetail {
-  workerId: number
+  workerId?: number
   workerNodeKey?: number
 }