fix: fix dynamic import syntax
[poolifier.git] / src / worker / abstract-worker.ts
index a84bb0691fc588d3a8e51b0fe1696d7c85bb7dc9..1e1d3088fb7cce28ad22f1961899786e6edd5fdd 100644 (file)
@@ -45,7 +45,7 @@ export abstract class AbstractWorker<
    */
   protected lastTaskTimestamp!: number
   /**
-   * Performance statistics computation.
+   * Performance statistics computation requirements.
    */
   protected statistics!: WorkerStatistics
   /**
@@ -160,13 +160,13 @@ export abstract class AbstractWorker<
     } else if (message.parent != null) {
       // Main worker reference message received
       this.mainWorker = message.parent
+    } else if (message.statistics != null) {
+      // Statistics message received
+      this.statistics = message.statistics
     } else if (message.kill != null) {
       // Kill message received
       this.aliveInterval != null && clearInterval(this.aliveInterval)
       this.emitDestroy()
-    } else if (message.statistics != null) {
-      // Statistics message received
-      this.statistics = message.statistics
     }
   }
 
@@ -298,6 +298,7 @@ export abstract class AbstractWorker<
   }
 
   private beginTaskPerformance (): TaskPerformance {
+    this.checkStatistics()
     return {
       timestamp: performance.now(),
       ...(this.statistics.elu && { elu: performance.eventLoopUtilization() })
@@ -307,6 +308,7 @@ export abstract class AbstractWorker<
   private endTaskPerformance (
     taskPerformance: TaskPerformance
   ): TaskPerformance {
+    this.checkStatistics()
     return {
       ...taskPerformance,
       ...(this.statistics.runTime && {
@@ -317,4 +319,10 @@ export abstract class AbstractWorker<
       })
     }
   }
+
+  private checkStatistics (): void {
+    if (this.statistics == null) {
+      throw new Error('Performance statistics computation requirements not set')
+    }
+  }
 }