import type {
MessageValue,
Task,
+ TaskFunctionProperties,
TaskPerformance,
WorkerStatistics
} from '../utility-types.js'
import {
+ buildTaskFunctionProperties,
DEFAULT_TASK_NAME,
EMPTY_FUNCTION,
isAsyncFunction,
import type {
TaskAsyncFunction,
TaskFunction,
+ TaskFunctionObject,
TaskFunctionOperationResult,
TaskFunctions,
TaskSyncFunction
} from './task-functions.js'
import {
checkTaskFunctionName,
- checkValidTaskFunctionEntry,
+ checkValidTaskFunctionObjectEntry,
checkValidWorkerOptions
} from './utils.js'
import { KillBehaviors, type WorkerOptions } from './worker-options.js'
*/
protected abstract id: number
/**
- * Task function(s) processed by the worker when the pool's `execution` function is invoked.
+ * Task function object(s) processed by the worker when the pool's `execution` function is invoked.
*/
- protected taskFunctions!: Map<string, TaskFunction<Data, Response>>
+ protected taskFunctions!: Map<string, TaskFunctionObject<Data, Response>>
/**
* Timestamp of the last task processed by this worker.
*/
if (taskFunctions == null) {
throw new Error('taskFunctions parameter is mandatory')
}
- this.taskFunctions = new Map<string, TaskFunction<Data, Response>>()
+ this.taskFunctions = new Map<string, TaskFunctionObject<Data, Response>>()
if (typeof taskFunctions === 'function') {
- const boundFn = taskFunctions.bind(this)
- this.taskFunctions.set(DEFAULT_TASK_NAME, boundFn)
+ const fnObj = { taskFunction: taskFunctions.bind(this) }
+ this.taskFunctions.set(DEFAULT_TASK_NAME, fnObj)
this.taskFunctions.set(
typeof taskFunctions.name === 'string' &&
taskFunctions.name.trim().length > 0
? taskFunctions.name
: 'fn1',
- boundFn
+ fnObj
)
} else if (isPlainObject(taskFunctions)) {
let firstEntry = true
- for (const [name, fn] of Object.entries(taskFunctions)) {
- checkValidTaskFunctionEntry<Data, Response>(name, fn)
- const boundFn = fn.bind(this)
+ for (let [name, fnObj] of Object.entries(taskFunctions)) {
+ if (typeof fnObj === 'function') {
+ fnObj = { taskFunction: fnObj } satisfies TaskFunctionObject<
+ Data,
+ Response
+ >
+ }
+ checkValidTaskFunctionObjectEntry<Data, Response>(name, fnObj)
+ fnObj.taskFunction = fnObj.taskFunction.bind(this)
if (firstEntry) {
- this.taskFunctions.set(DEFAULT_TASK_NAME, boundFn)
+ this.taskFunctions.set(DEFAULT_TASK_NAME, fnObj)
firstEntry = false
}
- this.taskFunctions.set(name, boundFn)
+ this.taskFunctions.set(name, fnObj)
}
if (firstEntry) {
throw new Error('taskFunctions parameter object is empty')
*/
public addTaskFunction (
name: string,
- fn: TaskFunction<Data, Response>
+ fn: TaskFunction<Data, Response> | TaskFunctionObject<Data, Response>
): TaskFunctionOperationResult {
try {
checkTaskFunctionName(name)
'Cannot add a task function with the default reserved name'
)
}
- if (typeof fn !== 'function') {
- throw new TypeError('fn parameter is not a function')
+ if (typeof fn === 'function') {
+ fn = { taskFunction: fn } satisfies TaskFunctionObject<Data, Response>
}
- const boundFn = fn.bind(this)
+ checkValidTaskFunctionObjectEntry<Data, Response>(name, fn)
+ fn.taskFunction = fn.taskFunction.bind(this)
if (
this.taskFunctions.get(name) ===
this.taskFunctions.get(DEFAULT_TASK_NAME)
) {
- this.taskFunctions.set(DEFAULT_TASK_NAME, boundFn)
+ this.taskFunctions.set(DEFAULT_TASK_NAME, fn)
}
- this.taskFunctions.set(name, boundFn)
- this.sendTaskFunctionNamesToMainWorker()
+ this.taskFunctions.set(name, fn)
+ this.sendTaskFunctionsPropertiesToMainWorker()
return { status: true }
} catch (error) {
return { status: false, error: error as Error }
)
}
const deleteStatus = this.taskFunctions.delete(name)
- this.sendTaskFunctionNamesToMainWorker()
+ this.sendTaskFunctionsPropertiesToMainWorker()
return { status: deleteStatus }
} catch (error) {
return { status: false, error: error as Error }
}
/**
- * Lists the names of the worker's task functions.
+ * Lists the properties of the worker's task functions.
*
- * @returns The names of the worker's task functions.
+ * @returns The properties of the worker's task functions.
*/
- public listTaskFunctionNames (): string[] {
- const names = [...this.taskFunctions.keys()]
+ public listTaskFunctionsProperties (): TaskFunctionProperties[] {
let defaultTaskFunctionName = DEFAULT_TASK_NAME
- for (const [name, fn] of this.taskFunctions) {
+ for (const [name, fnObj] of this.taskFunctions) {
if (
name !== DEFAULT_TASK_NAME &&
- fn === this.taskFunctions.get(DEFAULT_TASK_NAME)
+ fnObj === this.taskFunctions.get(DEFAULT_TASK_NAME)
) {
defaultTaskFunctionName = name
break
}
}
+ const taskFunctionsProperties: TaskFunctionProperties[] = []
+ for (const [name, fnObj] of this.taskFunctions) {
+ if (name === DEFAULT_TASK_NAME || name === defaultTaskFunctionName) {
+ continue
+ }
+ taskFunctionsProperties.push(buildTaskFunctionProperties(name, fnObj))
+ }
return [
- names[names.indexOf(DEFAULT_TASK_NAME)],
- defaultTaskFunctionName,
- ...names.filter(
- name => name !== DEFAULT_TASK_NAME && name !== defaultTaskFunctionName
- )
+ buildTaskFunctionProperties(
+ DEFAULT_TASK_NAME,
+ this.taskFunctions.get(DEFAULT_TASK_NAME)
+ ),
+ buildTaskFunctionProperties(
+ defaultTaskFunctionName,
+ this.taskFunctions.get(defaultTaskFunctionName)
+ ),
+ ...taskFunctionsProperties
]
}
}
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
this.taskFunctions.set(DEFAULT_TASK_NAME, this.taskFunctions.get(name)!)
- this.sendTaskFunctionNamesToMainWorker()
+ this.sendTaskFunctionsPropertiesToMainWorker()
return { status: true }
} catch (error) {
return { status: false, error: error as Error }
*/
protected messageListener (message: MessageValue<Data>): void {
this.checkMessageWorkerId(message)
- if (message.statistics != null) {
+ const {
+ statistics,
+ checkActive,
+ taskFunctionOperation,
+ taskId,
+ data,
+ kill
+ } = message
+ if (statistics != null) {
// Statistics message received
- this.statistics = message.statistics
- } else if (message.checkActive != null) {
+ this.statistics = statistics
+ } else if (checkActive != null) {
// Check active message received
- message.checkActive ? this.startCheckActive() : this.stopCheckActive()
- } else if (message.taskFunctionOperation != null) {
+ checkActive ? this.startCheckActive() : this.stopCheckActive()
+ } else if (taskFunctionOperation != null) {
// Task function operation message received
this.handleTaskFunctionOperationMessage(message)
- } else if (message.taskId != null && message.data != null) {
+ } else if (taskId != null && data != null) {
// Task message received
this.run(message)
- } else if (message.kill === true) {
+ } else if (kill === true) {
// Kill message received
this.handleKillMessage(message)
}
protected handleTaskFunctionOperationMessage (
message: MessageValue<Data>
): void {
- const { taskFunctionOperation, taskFunctionName, taskFunction } = message
- if (taskFunctionName == null) {
+ const { taskFunctionOperation, taskFunctionProperties, taskFunction } =
+ message
+ if (taskFunctionProperties == null) {
throw new Error(
- 'Cannot handle task function operation message without a task function name'
+ 'Cannot handle task function operation message without task function properties'
)
}
let response: TaskFunctionOperationResult
switch (taskFunctionOperation) {
case 'add':
- response = this.addTaskFunction(
- taskFunctionName,
+ response = this.addTaskFunction(taskFunctionProperties.name, {
// eslint-disable-next-line @typescript-eslint/no-implied-eval, no-new-func
- new Function(`return ${taskFunction}`)() as TaskFunction<
- Data,
- Response
- >
- )
+ taskFunction: new Function(
+ `return ${taskFunction}`
+ )() as TaskFunction<Data, Response>,
+ ...(taskFunctionProperties.priority != null && {
+ priority: taskFunctionProperties.priority
+ }),
+ ...(taskFunctionProperties.strategy != null && {
+ strategy: taskFunctionProperties.strategy
+ })
+ })
break
case 'remove':
- response = this.removeTaskFunction(taskFunctionName)
+ response = this.removeTaskFunction(taskFunctionProperties.name)
break
case 'default':
- response = this.setDefaultTaskFunction(taskFunctionName)
+ response = this.setDefaultTaskFunction(taskFunctionProperties.name)
break
default:
response = { status: false, error: new Error('Unknown task operation') }
this.sendToMainWorker({
taskFunctionOperation,
taskFunctionOperationStatus: response.status,
- taskFunctionName,
+ taskFunctionProperties,
...(!response.status &&
response.error != null && {
workerError: {
- name: taskFunctionName,
+ name: taskFunctionProperties.name,
message: this.handleError(response.error as Error | string)
}
})
): void
/**
- * Sends task function names to the main worker.
+ * Sends task functions properties to the main worker.
*/
- protected sendTaskFunctionNamesToMainWorker (): void {
+ protected sendTaskFunctionsPropertiesToMainWorker (): void {
this.sendToMainWorker({
- taskFunctionNames: this.listTaskFunctionNames()
+ taskFunctionsProperties: this.listTaskFunctionsProperties()
})
}
})
return
}
- const fn = this.taskFunctions.get(taskFunctionName)
+ const fn = this.taskFunctions.get(taskFunctionName)?.taskFunction
if (isAsyncFunction(fn)) {
this.runAsync(fn as TaskAsyncFunction<Data, Response>, task)
} else {