+ private checkFunctionInput (fn: WorkerFunction<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')
+ }
+ if (fn.constructor.name === 'AsyncFunction' && this.opts.async === false) {
+ throw new Error(
+ 'fn parameter is an async function, please set the async option to true'
+ )
+ }
+ }
+
+ /**
+ * 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()
+ }