+
+ this.mainWorker?.on('message', (value: MessageValue<Data, MainWorker>) => {
+ if (value?.data && value.id) {
+ // Here you will receive messages
+ if (this.opts.async) {
+ this.runInAsyncScope(this.runAsync.bind(this), this, fn, value)
+ } else {
+ this.runInAsyncScope(this.run.bind(this), this, fn, value)
+ }
+ } else if (value.parent) {
+ // Save a reference of the main worker to communicate with it
+ // This will be received once
+ this.mainWorker = value.parent
+ } else if (value.kill) {
+ // Here is time to kill this worker, just clearing the interval
+ if (this.aliveInterval) clearInterval(this.aliveInterval)
+ this.emitDestroy()
+ }
+ })
+ }
+
+ private checkWorkerOptions (opts: WorkerOptions) {
+ this.opts.killBehavior = opts.killBehavior ?? DEFAULT_KILL_BEHAVIOR
+ this.opts.maxInactiveTime =
+ opts.maxInactiveTime ?? DEFAULT_MAX_INACTIVE_TIME
+ /**
+ * Whether the worker is working asynchronously or not.
+ */
+ this.opts.async = !!opts.async