- } 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.interval) clearInterval(this.interval)
- this.emitDestroy()
+ this.taskFunctions.set(name, fn.bind(this))
+ if (firstEntry) {
+ this.taskFunctions.set(DEFAULT_FUNCTION_NAME, fn.bind(this))
+ firstEntry = false
+ }
+ }
+ if (firstEntry) {
+ throw new Error('taskFunctions parameter object is empty')
+ }
+ } else {
+ throw new TypeError(
+ 'taskFunctions parameter is not a function or a plain object'
+ )
+ }
+ }
+
+ /**
+ * Worker message listener.
+ *
+ * @param message - Message received.
+ */
+ protected messageListener (
+ message: MessageValue<Data, Data, MainWorker>
+ ): void {
+ if (message.id != null && message.data != null) {
+ // Task message received
+ const fn = this.getTaskFunction(message.name)
+ if (fn?.constructor.name === 'AsyncFunction') {
+ this.runInAsyncScope(this.runAsync.bind(this), this, fn, message)
+ } else {
+ this.runInAsyncScope(this.runSync.bind(this), this, fn, message)