- private checkFunctionInput (fn: (data: Data) => Response): void {
- if (!fn) throw new Error('fn parameter is mandatory')
+ 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()
+ }