Improve JSDoc comments (#130)
[poolifier.git] / src / worker / abstract-worker.ts
index 4b68d8975a3cc21306a1d4ff19ab9592b5c48297..fd163547a78eb08c7ca0c0a93519374530ed5c8f 100644 (file)
@@ -2,22 +2,42 @@ import { AsyncResource } from 'async_hooks'
 import type { MessageValue } from '../utility-types'
 import type { WorkerOptions } from './worker-options'
 
+/**
+ * Base class containing some shared logic for all poolifier workers.
+ *
+ * @template MainWorker Type of main worker.
+ * @template Data Type of data this worker receives from pool's execution.
+ * @template Response Type of response the worker sends back to the main worker.
+ */
 export abstract class AbstractWorker<
   MainWorker,
   Data = unknown,
   Response = unknown
 > extends AsyncResource {
+  /**
+   * The maximum time to keep this worker alive while idle. The pool automatically checks and terminates this worker when the time expires.
+   */
   protected readonly maxInactiveTime: number
+  /**
+   * Whether the worker is working asynchronously or not.
+   */
   protected readonly async: boolean
+  /**
+   * Timestamp of the last task processed by this worker.
+   */
   protected lastTask: number
+  /**
+   * Handler ID of the `interval` alive check.
+   */
   protected readonly interval?: NodeJS.Timeout
 
   /**
+   * Constructs a new poolifier worker.
    *
    * @param type The type of async event.
-   * @param isMain
-   * @param fn
-   * @param opts
+   * @param isMain Whether this is the main worker or not.
+   * @param fn Function processed by the worker when the pool's `execution` function is invoked.
+   * @param opts Options for the worker.
    */
   public constructor (
     type: string,
@@ -30,7 +50,7 @@ export abstract class AbstractWorker<
     this.maxInactiveTime = this.opts.maxInactiveTime ?? 1000 * 60
     this.async = !!this.opts.async
     this.lastTask = Date.now()
-    if (!fn) throw new Error('Fn parameter is mandatory')
+    if (!fn) throw new Error('fn parameter is mandatory')
     // keep the worker active
     if (!isMain) {
       this.interval = setInterval(
@@ -41,20 +61,42 @@ export abstract class AbstractWorker<
     }
   }
 
+  /**
+   * Returns the main worker.
+   */
   protected abstract getMainWorker (): MainWorker
 
+  /**
+   * Send a message to the main worker.
+   *
+   * @param message The response message.
+   */
   protected abstract sendToMainWorker (message: MessageValue<Response>): void
 
+  /**
+   * Check to see if the worker should be terminated, because its living too long.
+   */
   protected checkAlive (): void {
     if (Date.now() - this.lastTask > this.maxInactiveTime) {
       this.sendToMainWorker({ kill: 1 })
     }
   }
 
+  /**
+   * Handle an error and convert it to a string so it can be sent back to the main worker.
+   *
+   * @param e The error raised by the worker.
+   */
   protected handleError (e: Error | string): string {
     return (e as unknown) as string
   }
 
+  /**
+   * Run the given function synchronously.
+   *
+   * @param fn Function that will be executed.
+   * @param value Input data for the given function.
+   */
   protected run (
     fn: (data?: Data) => Response,
     value: MessageValue<Data>
@@ -70,6 +112,12 @@ export abstract class AbstractWorker<
     }
   }
 
+  /**
+   * Run the given function asynchronously.
+   *
+   * @param fn Function that will be executed.
+   * @param value Input data for the given function.
+   */
   protected runAsync (
     fn: (data?: Data) => Promise<Response>,
     value: MessageValue<Data>