import type {
TaskAsyncFunction,
TaskFunction,
- TaskFunctionOperationReturnType,
+ TaskFunctionOperationResult,
TaskFunctions,
TaskSyncFunction
} from './task-functions'
}
private checkWorkerOptions (opts: WorkerOptions): void {
+ if (opts != null && !isPlainObject(opts)) {
+ throw new TypeError('opts worker options parameter is not a plain object')
+ }
+ if (
+ opts?.killBehavior != null &&
+ !Object.values(KillBehaviors).includes(opts.killBehavior)
+ ) {
+ throw new TypeError(
+ `killBehavior option '${opts.killBehavior}' is not valid`
+ )
+ }
+ if (
+ opts?.maxInactiveTime != null &&
+ !Number.isSafeInteger(opts.maxInactiveTime)
+ ) {
+ throw new TypeError('maxInactiveTime option is not an integer')
+ }
+ if (opts?.maxInactiveTime != null && opts.maxInactiveTime < 5) {
+ throw new TypeError(
+ 'maxInactiveTime option is not a positive integer greater or equal than 5'
+ )
+ }
+ if (opts?.killHandler != null && typeof opts.killHandler !== 'function') {
+ throw new TypeError('killHandler option is not a function')
+ }
+ if (opts?.async != null) {
+ throw new Error('async option is deprecated')
+ }
this.opts = { ...DEFAULT_WORKER_OPTIONS, ...opts }
- delete this.opts.async
}
- private checkValidTaskFunction (
+ private checkValidTaskFunctionEntry (
name: string,
fn: TaskFunction<Data, Response>
): void {
}
/**
- * Checks if the `taskFunctions` parameter is passed to the constructor.
+ * Checks if the `taskFunctions` parameter is passed to the constructor and valid.
*
* @param taskFunctions - The task function(s) parameter that should be checked.
*/
} else if (isPlainObject(taskFunctions)) {
let firstEntry = true
for (const [name, fn] of Object.entries(taskFunctions)) {
- this.checkValidTaskFunction(name, fn)
+ this.checkValidTaskFunctionEntry(name, fn)
const boundFn = fn.bind(this)
if (firstEntry) {
this.taskFunctions.set(DEFAULT_TASK_NAME, boundFn)
* @param name - The name of the task function to check.
* @returns Whether the worker has a task function with the given name or not.
*/
- public hasTaskFunction (name: string): TaskFunctionOperationReturnType {
+ public hasTaskFunction (name: string): TaskFunctionOperationResult {
try {
this.checkTaskFunctionName(name)
} catch (error) {
public addTaskFunction (
name: string,
fn: TaskFunction<Data, Response>
- ): TaskFunctionOperationReturnType {
+ ): TaskFunctionOperationResult {
try {
this.checkTaskFunctionName(name)
if (name === DEFAULT_TASK_NAME) {
* @param name - The name of the task function to remove.
* @returns Whether the task function existed and was removed or not.
*/
- public removeTaskFunction (name: string): TaskFunctionOperationReturnType {
+ public removeTaskFunction (name: string): TaskFunctionOperationResult {
try {
this.checkTaskFunctionName(name)
if (name === DEFAULT_TASK_NAME) {
* @param name - The name of the task function to use as default task function.
* @returns Whether the default task function was set or not.
*/
- public setDefaultTaskFunction (name: string): TaskFunctionOperationReturnType {
+ public setDefaultTaskFunction (name: string): TaskFunctionOperationResult {
try {
this.checkTaskFunctionName(name)
if (name === DEFAULT_TASK_NAME) {
DEFAULT_TASK_NAME,
this.taskFunctions.get(name) as TaskFunction<Data, Response>
)
+ this.sendTaskFunctionNamesToMainWorker()
return { status: true }
} catch (error) {
return { status: false, error: error as Error }
protected handleTaskFunctionOperationMessage (
message: MessageValue<Data>
): void {
- const { taskFunctionOperation, taskFunction, taskFunctionName } = message
- let response!: TaskFunctionOperationReturnType
+ const { taskFunctionOperation, taskFunctionName, taskFunction } = message
+ let response!: TaskFunctionOperationResult
if (taskFunctionOperation === 'add') {
response = this.addTaskFunction(
taskFunctionName as string,
this.sendToMainWorker({
taskFunctionOperation,
taskFunctionOperationStatus: response.status,
- workerError: {
- name: taskFunctionName as string,
- message: this.handleError(response.error as Error | string)
- },
- workerId: this.id
+ taskFunctionName,
+ ...(!response.status &&
+ response?.error != null && {
+ workerError: {
+ name: taskFunctionName as string,
+ message: this.handleError(response.error as Error | string)
+ }
+ })
})
}
if (isAsyncFunction(this.opts.killHandler)) {
(this.opts.killHandler?.() as Promise<void>)
.then(() => {
- this.sendToMainWorker({ kill: 'success', workerId: this.id })
+ this.sendToMainWorker({ kill: 'success' })
return null
})
.catch(() => {
- this.sendToMainWorker({ kill: 'failure', workerId: this.id })
+ this.sendToMainWorker({ kill: 'failure' })
})
.finally(() => {
this.emitDestroy()
try {
// eslint-disable-next-line @typescript-eslint/no-invalid-void-type
this.opts.killHandler?.() as void
- this.sendToMainWorker({ kill: 'success', workerId: this.id })
+ this.sendToMainWorker({ kill: 'success' })
} catch {
- this.sendToMainWorker({ kill: 'failure', workerId: this.id })
+ this.sendToMainWorker({ kill: 'failure' })
} finally {
this.emitDestroy()
}
performance.now() - this.lastTaskTimestamp >
(this.opts.maxInactiveTime ?? DEFAULT_MAX_INACTIVE_TIME)
) {
- this.sendToMainWorker({ kill: this.opts.killBehavior, workerId: this.id })
+ this.sendToMainWorker({ kill: this.opts.killBehavior })
}
}
*/
protected sendTaskFunctionNamesToMainWorker (): void {
this.sendToMainWorker({
- taskFunctionNames: this.listTaskFunctionNames(),
- workerId: this.id
+ taskFunctionNames: this.listTaskFunctionNames()
})
}
message: `Task function '${name as string}' not found`,
data
},
- workerId: this.id,
taskId
})
return
this.sendToMainWorker({
data: res,
taskPerformance,
- workerId: this.id,
taskId
})
} catch (error) {
message: this.handleError(error as Error | string),
data
},
- workerId: this.id,
taskId
})
} finally {
this.sendToMainWorker({
data: res,
taskPerformance,
- workerId: this.id,
taskId
})
- return null
+ return undefined
})
.catch(error => {
this.sendToMainWorker({
message: this.handleError(error as Error | string),
data
},
- workerId: this.id,
taskId
})
})