fix: fix sonarcloud analysis
[poolifier.git] / src / pools / abstract-pool.ts
index 3bc00a6fffd36c987dd015fdeecd6b62f1e6a662..b299bc0172e839fdc57e165bd8e1391200a17885 100644 (file)
@@ -69,7 +69,7 @@ export abstract class AbstractPool<
    * Constructs a new poolifier pool.
    *
    * @param numberOfWorkers - Number of workers that this pool should manage.
-   * @param filePath - Path to the worker-file.
+   * @param filePath - Path to the worker file.
    * @param opts - Options for the pool.
    */
   public constructor (
@@ -84,10 +84,10 @@ export abstract class AbstractPool<
     this.checkFilePath(this.filePath)
     this.checkPoolOptions(this.opts)
 
-    this.chooseWorkerNode.bind(this)
-    this.executeTask.bind(this)
-    this.enqueueTask.bind(this)
-    this.checkAndEmitEvents.bind(this)
+    this.chooseWorkerNode = this.chooseWorkerNode.bind(this)
+    this.executeTask = this.executeTask.bind(this)
+    this.enqueueTask = this.enqueueTask.bind(this)
+    this.checkAndEmitEvents = this.checkAndEmitEvents.bind(this)
 
     this.setupHook()
 
@@ -256,9 +256,7 @@ export abstract class AbstractPool<
     tasksQueueOptions?: TasksQueueOptions
   ): void {
     if (this.opts.enableTasksQueue === true && !enable) {
-      for (const [workerNodeKey] of this.workerNodes.entries()) {
-        this.flushTasksQueue(workerNodeKey)
-      }
+      this.flushTasksQueues()
     }
     this.opts.enableTasksQueue = enable
     this.setTasksQueueOptions(tasksQueueOptions as TasksQueueOptions)
@@ -298,18 +296,15 @@ export abstract class AbstractPool<
   protected abstract get busy (): boolean
 
   protected internalBusy (): boolean {
-    return this.findFreeWorkerNodeKey() === -1
-  }
-
-  /** @inheritDoc */
-  public findFreeWorkerNodeKey (): number {
-    return this.workerNodes.findIndex(workerNode => {
-      return workerNode.tasksUsage?.running === 0
-    })
+    return (
+      this.workerNodes.findIndex(workerNode => {
+        return workerNode.tasksUsage?.running === 0
+      }) === -1
+    )
   }
 
   /** @inheritDoc */
-  public async execute (data: Data): Promise<Response> {
+  public async execute (data?: Data): Promise<Response> {
     const [workerNodeKey, workerNode] = this.chooseWorkerNode()
     const submittedTask: Task<Data> = {
       // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
@@ -656,4 +651,10 @@ export abstract class AbstractPool<
     const workerNodeKey = this.getWorkerNodeKey(worker)
     this.flushTasksQueue(workerNodeKey)
   }
+
+  private flushTasksQueues (): void {
+    for (const [workerNodeKey] of this.workerNodes.entries()) {
+      this.flushTasksQueue(workerNodeKey)
+    }
+  }
 }