fix: fix pool ready event emission
[poolifier.git] / src / pools / abstract-pool.ts
index de37c0f9266cc6d6b6ca37fdd7c174ccd94afaec..b14801b69269b43cfe878dee88ef4987151dfb33 100644 (file)
@@ -17,7 +17,6 @@ import {
   max,
   median,
   min,
-  once,
   round
 } from '../utils'
 import { KillBehaviors } from '../worker/worker-options'
@@ -117,6 +116,10 @@ export abstract class AbstractPool<
    * Whether the pool is destroying or not.
    */
   private destroying: boolean
+  /**
+   * Whether the pool ready event has been emitted or not.
+   */
+  private readyEventEmitted: boolean
   /**
    * The start timestamp of the pool.
    */
@@ -167,6 +170,7 @@ export abstract class AbstractPool<
     this.started = false
     this.starting = false
     this.destroying = false
+    this.readyEventEmitted = false
     if (this.opts.startWorkers === true) {
       this.start()
     }
@@ -982,6 +986,7 @@ export abstract class AbstractPool<
     )
     this.emitter?.emit(PoolEvents.destroy, this.info)
     this.emitter?.emitDestroy()
+    this.readyEventEmitted = false
     this.destroying = false
     this.started = false
   }
@@ -1572,12 +1577,9 @@ export abstract class AbstractPool<
     )
     workerInfo.ready = message.ready as boolean
     workerInfo.taskFunctionNames = message.taskFunctionNames
-    if (this.ready) {
-      const emitPoolReadyEventOnce = once(
-        () => this.emitter?.emit(PoolEvents.ready, this.info),
-        this
-      )
-      emitPoolReadyEventOnce()
+    if (!this.readyEventEmitted && this.ready) {
+      this.readyEventEmitted = true
+      this.emitter?.emit(PoolEvents.ready, this.info)
     }
   }