refactor: merge dynamic pool events emission code
authorJérôme Benoit <jerome.benoit@piment-noir.org>
Wed, 28 Aug 2024 13:39:23 +0000 (15:39 +0200)
committerJérôme Benoit <jerome.benoit@piment-noir.org>
Wed, 28 Aug 2024 13:39:23 +0000 (15:39 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
src/pools/abstract-pool.ts
src/pools/cluster/dynamic.ts
src/pools/thread/dynamic.ts

index 8d8a750cda17e5cdf86d1775a519bdb1b8905196..0503e8913937df42c2b6f64a73029c572ef59c43 100644 (file)
@@ -934,12 +934,6 @@ export abstract class AbstractPool<
     return this.workerNodes.length <= 1 || this.info.queuedTasks === 0
   }
 
-  private checkAndEmitEmptyEvent (): void {
-    if (this.emitter != null && this.empty) {
-      this.emitter.emit(PoolEvents.empty, this.info)
-    }
-  }
-
   private checkAndEmitReadyEvent (): void {
     if (this.emitter != null && !this.readyEventEmitted && this.ready) {
       this.emitter.emit(PoolEvents.ready, this.info)
@@ -1402,7 +1396,6 @@ export abstract class AbstractPool<
       this.workerChoiceStrategiesContext?.remove(workerNodeKey)
       workerNode.info.dynamic &&
         this.checkAndEmitDynamicWorkerDestructionEvents()
-      this.checkAndEmitEmptyEvent()
     }
   }
 
index acca81ba8c77c54ada5fd810ef17c2b660b66f99..9561fc80b1c5302add47d11687ff9cac06125421 100644 (file)
@@ -52,9 +52,14 @@ export class DynamicClusterPool<
 
   /** @inheritDoc */
   protected checkAndEmitDynamicWorkerDestructionEvents (): void {
-    if (this.emitter != null && this.fullEventEmitted && !this.full) {
-      this.emitter.emit(PoolEvents.fullEnd, this.info)
-      this.fullEventEmitted = false
+    if (this.emitter != null) {
+      if (this.fullEventEmitted && !this.full) {
+        this.emitter.emit(PoolEvents.fullEnd, this.info)
+        this.fullEventEmitted = false
+      }
+      if (this.empty) {
+        this.emitter.emit(PoolEvents.empty, this.info)
+      }
     }
   }
 
index 8fd4b675854f218da49a9d0f071c202a36572a3f..9cb068a2690abf0255344a495fec46714a202458 100644 (file)
@@ -52,9 +52,14 @@ export class DynamicThreadPool<
 
   /** @inheritDoc */
   protected checkAndEmitDynamicWorkerDestructionEvents (): void {
-    if (this.emitter != null && this.fullEventEmitted && !this.full) {
-      this.emitter.emit(PoolEvents.fullEnd, this.info)
-      this.fullEventEmitted = false
+    if (this.emitter != null) {
+      if (this.fullEventEmitted && !this.full) {
+        this.emitter.emit(PoolEvents.fullEnd, this.info)
+        this.fullEventEmitted = false
+      }
+      if (this.empty) {
+        this.emitter.emit(PoolEvents.empty, this.info)
+      }
     }
   }