+ this.mainWorker?.on('message', this.messageListener.bind(this))
+ }
+
+ 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 `taskFunctions` parameter is passed to the constructor.
+ *
+ * @param taskFunctions - The task function(s) parameter that should be checked.
+ */
+ private checkTaskFunctions (
+ taskFunctions:
+ | WorkerFunction<Data, Response>
+ | TaskFunctions<Data, Response>
+ ): void {
+ if (taskFunctions == null) {
+ throw new Error('taskFunctions parameter is mandatory')
+ }
+ if (
+ typeof taskFunctions !== 'function' &&
+ typeof taskFunctions !== 'object'
+ ) {
+ throw new Error('taskFunctions parameter is not a function or an object')
+ }
+ if (
+ typeof taskFunctions === 'object' &&
+ taskFunctions.constructor !== Object &&
+ Object.prototype.toString.call(taskFunctions) !== '[object Object]'
+ ) {
+ throw new Error('taskFunctions parameter is not an object literal')
+ }
+ this.taskFunctions = new Map<string, WorkerFunction<Data, Response>>()
+ if (typeof taskFunctions !== 'function') {
+ let firstEntry = true
+ for (const [name, fn] of Object.entries(taskFunctions)) {
+ if (typeof fn !== 'function') {
+ throw new Error(
+ 'A taskFunctions parameter object value is not a function'
+ )
+ }
+ this.taskFunctions.set(name, fn.bind(this))
+ if (firstEntry) {
+ this.taskFunctions.set(DEFAULT_FUNCTION_NAME, fn.bind(this))
+ firstEntry = false
+ }