+
+ this.mainWorker?.on('message', (value: MessageValue<Data, MainWorker>) => {
+ this.messageListener(value, fn)
+ })
+ }
+
+ protected messageListener (
+ value: MessageValue<Data, MainWorker>,
+ fn: (data: Data) => Response
+ ): void {
+ if (value.data != null && value.id != null) {
+ // Here you will receive messages
+ if (this.opts.async === true) {
+ this.runInAsyncScope(this.runAsync.bind(this), this, fn, value)
+ } else {
+ this.runInAsyncScope(this.run.bind(this), this, fn, value)
+ }
+ } else if (value.parent != null) {
+ // Save a reference of the main worker to communicate with it
+ // This will be received once
+ this.mainWorker = value.parent
+ } else if (value.kill != null) {
+ // Here is time to kill this worker, just clearing the interval
+ this.aliveInterval != null && clearInterval(this.aliveInterval)
+ this.emitDestroy()
+ }
+ }
+
+ private checkWorkerOptions (opts: WorkerOptions): void {
+ this.opts.killBehavior = opts.killBehavior ?? DEFAULT_KILL_BEHAVIOR
+ this.opts.maxInactiveTime =
+ opts.maxInactiveTime ?? DEFAULT_MAX_INACTIVE_TIME
+ this.opts.async = opts.async ?? false
+ }
+
+ /**
+ * Checks if the `fn` parameter is passed to the constructor.
+ *
+ * @param fn - The function that should be defined.
+ */
+ private checkFunctionInput (fn: (data: Data) => Response): void {
+ if (fn == null) throw new Error('fn parameter is mandatory')
+ if (typeof fn !== 'function') {
+ throw new TypeError('fn parameter is not a function')
+ }