import type { KillBehavior, WorkerOptions } from './worker-options'
import { KillBehaviors } from './worker-options'
+const DEFAULT_FUNCTION_NAME = 'default'
const DEFAULT_MAX_INACTIVE_TIME = 60000
const DEFAULT_KILL_BEHAVIOR: KillBehavior = KillBehaviors.SOFT
| WorkerFunction<Data, Response>
| TaskFunctions<Data, Response>
): void {
- if (taskFunctions == null) { throw new Error('taskFunctions parameter is mandatory') }
+ if (taskFunctions == null) {
+ throw new Error('taskFunctions parameter is mandatory')
+ }
if (
typeof taskFunctions !== 'function' &&
typeof taskFunctions !== 'object'
this.taskFunctions.set(name, fn.bind(this))
}
} else {
- this.taskFunctions.set('default', taskFunctions.bind(this))
+ this.taskFunctions.set(DEFAULT_FUNCTION_NAME, taskFunctions.bind(this))
}
}
*/
protected messageListener (message: MessageValue<Data, MainWorker>): void {
if (message.id != null && message.data != null) {
- let fn: WorkerFunction<Data, Response> | undefined
- if (message.name == null) {
- fn = this.taskFunctions.get('default')
- } else {
- fn = this.taskFunctions.get(message.name)
- }
+ const fn = this.getTaskFunction(message.name)
// Task message received
if (fn?.constructor.name === 'AsyncFunction') {
this.runInAsyncScope(this.runAsync.bind(this), this, fn, message)
})
.catch(EMPTY_FUNCTION)
}
+
+ private getTaskFunction (name?: string): WorkerFunction<Data, Response> {
+ name = name ?? DEFAULT_FUNCTION_NAME
+ const fn = this.taskFunctions.get(name)
+ if (fn == null) {
+ throw new Error(`Task function "${name}" not found`)
+ }
+ return fn
+ }
}