/** @inheritDoc */
public async addTaskFunction (
name: string,
- taskFunction: TaskFunction<Data, Response>
+ fn: TaskFunction<Data, Response>
): Promise<boolean> {
- this.taskFunctions.set(name, taskFunction)
+ if (typeof name !== 'string') {
+ throw new TypeError('name argument must be a string')
+ }
+ if (typeof name === 'string' && name.trim().length === 0) {
+ throw new TypeError('name argument must not be an empty string')
+ }
+ if (typeof fn !== 'function') {
+ throw new TypeError('fn argument must be a function')
+ }
+ this.taskFunctions.set(name, fn)
return await this.sendTaskFunctionOperationToWorkers({
taskFunctionOperation: 'add',
taskFunctionName: name,
- taskFunction: taskFunction.toString()
+ taskFunction: fn.toString()
})
}
* If a task function with the same name already exists, it will be overwritten.
*
* @param name - The name of the task function.
- * @param taskFunction - The task function.
+ * @param fn - The task function.
* @returns `true` if the task function was added, `false` otherwise.
*/
readonly addTaskFunction: (
name: string,
- taskFunction: TaskFunction<Data, Response>
+ fn: TaskFunction<Data, Response>
) => Promise<boolean>
/**
* Removes a task function from this pool.
'./tests/worker-files/thread/testWorker.js'
)
await waitPoolEvents(dynamicThreadPool, PoolEvents.ready, 1)
+ await expect(
+ dynamicThreadPool.addTaskFunction(0, () => {})
+ ).rejects.toThrowError(new TypeError('name argument must be a string'))
+ await expect(
+ dynamicThreadPool.addTaskFunction('', () => {})
+ ).rejects.toThrowError(
+ new TypeError('name argument must not be an empty string')
+ )
+ await expect(
+ dynamicThreadPool.addTaskFunction('test', 0)
+ ).rejects.toThrowError(new TypeError('fn argument must be a function'))
+ await expect(
+ dynamicThreadPool.addTaskFunction('test', '')
+ ).rejects.toThrowError(new TypeError('fn argument must be a function'))
expect(dynamicThreadPool.listTaskFunctionNames()).toStrictEqual([
DEFAULT_TASK_NAME,
'test'