refactor: refine worker options scope
[poolifier.git] / src / worker / abstract-worker.ts
index c71ff297e2485151a89f470c3c9eb4712122a83a..475595eccf5f2e2c85b4eac8ba251f306f472c5d 100644 (file)
@@ -1,13 +1,9 @@
-import { AsyncResource } from 'async_hooks'
-import type { Worker } from 'cluster'
-import type { MessagePort } from 'worker_threads'
+import { AsyncResource } from 'node:async_hooks'
+import type { Worker } from 'node:cluster'
+import type { MessagePort } from 'node:worker_threads'
 import type { MessageValue } from '../utility-types'
 import { EMPTY_FUNCTION } from '../utils'
-import {
-  isKillBehavior,
-  type KillBehavior,
-  type WorkerOptions
-} from './worker-options'
+import type { KillBehavior, WorkerOptions } from './worker-options'
 import { KillBehaviors } from './worker-options'
 
 const DEFAULT_MAX_INACTIVE_TIME = 60000
@@ -33,10 +29,6 @@ export abstract class AbstractWorker<
    * Handler Id of the `aliveInterval` worker alive check.
    */
   protected readonly aliveInterval?: NodeJS.Timeout
-  /**
-   * Options for the worker.
-   */
-  public readonly opts: WorkerOptions
   /**
    * Constructs a new poolifier worker.
    *
@@ -48,10 +40,10 @@ export abstract class AbstractWorker<
    */
   public constructor (
     type: string,
-    isMain: boolean,
+    protected readonly isMain: boolean,
     fn: (data: Data) => Response,
     protected mainWorker: MainWorker | undefined | null,
-    opts: WorkerOptions = {
+    protected readonly opts: WorkerOptions = {
       /**
        * The kill behavior option on this Worker or its default value.
        */
@@ -64,10 +56,9 @@ export abstract class AbstractWorker<
     }
   ) {
     super(type)
-    this.opts = opts
     this.checkFunctionInput(fn)
     this.checkWorkerOptions(this.opts)
-    if (!isMain && isKillBehavior(KillBehaviors.HARD, this.opts.killBehavior)) {
+    if (!this.isMain) {
       this.lastTaskTimestamp = Date.now()
       this.aliveInterval = setInterval(
         this.checkAlive.bind(this),
@@ -85,20 +76,20 @@ export abstract class AbstractWorker<
     value: MessageValue<Data, MainWorker>,
     fn: (data: Data) => Response
   ): void {
-    if (value.data !== undefined && value.id !== undefined) {
+    if (value.data != null && value.id != null) {
       // Here you will receive messages
       if (this.opts.async === true) {
         this.runInAsyncScope(this.runAsync.bind(this), this, fn, value)
       } else {
         this.runInAsyncScope(this.run.bind(this), this, fn, value)
       }
-    } else if (value.parent !== undefined) {
+    } else if (value.parent != null) {
       // Save a reference of the main worker to communicate with it
       // This will be received once
       this.mainWorker = value.parent
-    } else if (value.kill !== undefined) {
+    } else if (value.kill != null) {
       // Here is time to kill this worker, just clearing the interval
-      if (this.aliveInterval != null) clearInterval(this.aliveInterval)
+      this.aliveInterval != null && clearInterval(this.aliveInterval)
       this.emitDestroy()
     }
   }
@@ -182,8 +173,7 @@ export abstract class AbstractWorker<
       const err = this.handleError(e as Error)
       this.sendToMainWorker({ error: err, id: value.id })
     } finally {
-      isKillBehavior(KillBehaviors.HARD, this.opts.killBehavior) &&
-        (this.lastTaskTimestamp = Date.now())
+      !this.isMain && (this.lastTaskTimestamp = Date.now())
     }
   }
 
@@ -209,8 +199,7 @@ export abstract class AbstractWorker<
         this.sendToMainWorker({ error: err, id: value.id })
       })
       .finally(() => {
-        isKillBehavior(KillBehaviors.HARD, this.opts.killBehavior) &&
-          (this.lastTaskTimestamp = Date.now())
+        !this.isMain && (this.lastTaskTimestamp = Date.now())
       })
       .catch(EMPTY_FUNCTION)
   }