De-duplicate code for workers (#154)
[poolifier.git] / src / worker / cluster-worker.ts
index 9402ec1044e51ce9a42c8717e3c6c506dda1ac10..4b1efaca384a92869b67460236aefea72953df9d 100644 (file)
@@ -30,27 +30,7 @@ export class ClusterWorker<
    * @param opts Options for the worker.
    */
   public constructor (fn: (data: Data) => Response, opts: WorkerOptions = {}) {
-    super('worker-cluster-pool:pioardi', isMaster, fn, opts)
-
-    worker.on('message', (value: MessageValue<Data>) => {
-      if (value?.data && value.id) {
-        // here you will receive messages
-        // console.log('This is the main worker ' + isMaster)
-        if (this.async) {
-          this.runInAsyncScope(this.runAsync.bind(this), this, fn, value)
-        } else {
-          this.runInAsyncScope(this.run.bind(this), this, fn, value)
-        }
-      } else if (value.kill) {
-        // here is time to kill this worker, just clearing the interval
-        if (this.interval) clearInterval(this.interval)
-        this.emitDestroy()
-      }
-    })
-  }
-
-  protected getMainWorker (): Worker {
-    return worker
+    super('worker-cluster-pool:pioardi', isMaster, fn, worker, opts)
   }
 
   protected sendToMainWorker (message: MessageValue<Response>): void {