Refine error messages.
[e-mobility-charging-stations-simulator.git] / src / charging-station / Worker.js
index 5de059d00c6229153937b3d690f7bb7fa444c410..cc8358559c5ce29d4bbda526b8de402309203de6 100644 (file)
@@ -1,21 +1,38 @@
-const Configuration = require('../utils/Configuration');
-const EventEmitter = require('events');
-const {Worker} = require('worker_threads');
-const Pool = require('worker-threads-pool');
+import Configuration from '../utils/Configuration.js';
+import EventEmitter from 'events';
+import Pool from 'worker-threads-pool';
+import {Worker} from 'worker_threads';
 
-class Wrk {
+export default class Wrk {
   /**
    * Create a new `Wrk`.
    *
    * @param {String} workerScript
    * @param {Object} workerData
+   * @param {Number} numConcurrentWorkers
    */
-  constructor(workerScript, workerData) {
+  constructor(workerScript, workerData, numConcurrentWorkers) {
+    this._workerData = workerData;
+    this._workerScript = workerScript;
+    this._numConcurrentWorkers = numConcurrentWorkers;
     if (Configuration.useWorkerPool()) {
       this._pool = new Pool({max: Configuration.getWorkerPoolSize()});
     }
-    this._workerData = workerData;
-    this._workerScript = workerScript;
+  }
+
+  /**
+   * @param {Number} numConcurrentWorkers
+   * @private
+   */
+  set _numConcurrentWorkers(numConcurrentWorkers) {
+    if (numConcurrentWorkers > 10) {
+      EventEmitter.defaultMaxListeners = numConcurrentWorkers + 1;
+    }
+    this._concurrentWorkers = numConcurrentWorkers;
+  }
+
+  get _numConcurrentWorkers() {
+    return this._concurrentWorkers;
   }
 
   /**
@@ -60,13 +77,8 @@ class Wrk {
    */
   start() {
     if (Configuration.useWorkerPool()) {
-      if (Configuration.getWorkerPoolSize() > 10) {
-        EventEmitter.defaultMaxListeners = Configuration.getWorkerPoolSize() + 1;
-      }
       return this._startWorkerWithPool();
     }
     return this._startWorker();
   }
 }
-
-module.exports = Wrk;