delete this.opts.async
}
+ private checkValidTaskFunction (
+ name: string,
+ fn: TaskFunction<Data, Response>
+ ): void {
+ if (typeof name !== 'string') {
+ throw new TypeError(
+ 'A taskFunctions parameter object key is not a string'
+ )
+ }
+ if (typeof name === 'string' && name.trim().length === 0) {
+ throw new TypeError(
+ 'A taskFunctions parameter object key is an empty string'
+ )
+ }
+ if (typeof fn !== 'function') {
+ throw new TypeError(
+ 'A taskFunctions parameter object value is not a function'
+ )
+ }
+ }
+
/**
* Checks if the `taskFunctions` parameter is passed to the constructor.
*
} else if (isPlainObject(taskFunctions)) {
let firstEntry = true
for (const [name, fn] of Object.entries(taskFunctions)) {
- if (typeof name !== 'string') {
- throw new TypeError(
- 'A taskFunctions parameter object key is not a string'
- )
- }
- if (typeof name === 'string' && name.trim().length === 0) {
- throw new TypeError(
- 'A taskFunctions parameter object key is an empty string'
- )
- }
- if (typeof fn !== 'function') {
- throw new TypeError(
- 'A taskFunctions parameter object value is not a function'
- )
- }
+ this.checkValidTaskFunction(name, fn)
const boundFn = fn.bind(this)
if (firstEntry) {
this.taskFunctions.set(DEFAULT_TASK_NAME, boundFn)
names[names.indexOf(DEFAULT_TASK_NAME)],
defaultTaskFunctionName,
...names.filter(
- (name) => name !== DEFAULT_TASK_NAME && name !== defaultTaskFunctionName
+ name => name !== DEFAULT_TASK_NAME && name !== defaultTaskFunctionName
)
]
}
* Returns the main worker.
*
* @returns Reference to the main worker.
+ * @throws {@link https://nodejs.org/api/errors.html#class-error} If the main worker is not set.
*/
protected getMainWorker (): MainWorker {
if (this.mainWorker == null) {
const { name, taskId, data } = task
let taskPerformance = this.beginTaskPerformance(name)
fn(data)
- .then((res) => {
+ .then(res => {
taskPerformance = this.endTaskPerformance(taskPerformance)
this.sendToMainWorker({
data: res,
})
return null
})
- .catch((e) => {
+ .catch(e => {
const errorMessage = this.handleError(e as Error | string)
this.sendToMainWorker({
taskError: {