+ /**
+ * Worker message listener.
+ *
+ * @param message - Message received.
+ * @param fn - Function processed by the worker when the pool's `execution` function is invoked.
+ */
+ protected messageListener (
+ message: MessageValue<Data, MainWorker>,
+ fn: WorkerFunction<Data, Response>
+ ): void {
+ if (message.id != null && message.data != null) {
+ // Task message received
+ if (this.opts.async === true) {
+ this.runInAsyncScope(this.runAsync.bind(this), this, fn, message)
+ } else {
+ this.runInAsyncScope(this.run.bind(this), this, fn, message)
+ }
+ } else if (message.parent != null) {
+ // Main worker reference message received
+ this.mainWorker = message.parent
+ } else if (message.kill != null) {
+ // Kill message received
+ this.aliveInterval != null && clearInterval(this.aliveInterval)
+ this.emitDestroy()
+ }
+ }
+
+ /**
+ * Returns the main worker.
+ *
+ * @returns Reference to the main worker.
+ */
+ protected getMainWorker (): MainWorker {
+ if (this.mainWorker == null) {
+ throw new Error('Main worker was not set')
+ }
+ return this.mainWorker
+ }
+
+ /**
+ * Sends a message to the main worker.
+ *
+ * @param message - The response message.
+ */