X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fworker%2Fabstract-worker.ts;h=75a8d784f87d4cd18126d2f81ce9ba54fae65323;hb=8735b4e51c0cfabc9612d57417834d42042cab4e;hp=f42fae1751e0381cd4cf8505a6a689cf7adf3e19;hpb=d38d0e30f12595f00a74ecd0aa89d7e568af57b7;p=poolifier.git diff --git a/src/worker/abstract-worker.ts b/src/worker/abstract-worker.ts index f42fae17..75a8d784 100644 --- a/src/worker/abstract-worker.ts +++ b/src/worker/abstract-worker.ts @@ -165,12 +165,7 @@ export abstract class AbstractWorker< * @throws {@link https://nodejs.org/api/errors.html#class-typeerror} If the `name` parameter is not a string or an empty string. */ public hasTaskFunction (name: string): boolean { - if (typeof name !== 'string') { - throw new TypeError('name parameter is not a string') - } - if (typeof name === 'string' && name.trim().length === 0) { - throw new TypeError('name parameter is an empty string') - } + this.checkTaskFunctionName(name) return this.taskFunctions.has(name) } @@ -189,12 +184,7 @@ export abstract class AbstractWorker< name: string, fn: TaskFunction ): boolean { - if (typeof name !== 'string') { - throw new TypeError('name parameter is not a string') - } - if (typeof name === 'string' && name.trim().length === 0) { - throw new TypeError('name parameter is an empty string') - } + this.checkTaskFunctionName(name) if (name === DEFAULT_TASK_NAME) { throw new Error( 'Cannot add a task function with the default reserved name' @@ -229,12 +219,7 @@ export abstract class AbstractWorker< * @throws {@link https://nodejs.org/api/errors.html#class-error} If the `name` parameter is the task function used as default task function. */ public removeTaskFunction (name: string): boolean { - if (typeof name !== 'string') { - throw new TypeError('name parameter is not a string') - } - if (typeof name === 'string' && name.trim().length === 0) { - throw new TypeError('name parameter is an empty string') - } + this.checkTaskFunctionName(name) if (name === DEFAULT_TASK_NAME) { throw new Error( 'Cannot remove the task function with the default reserved name' @@ -288,12 +273,7 @@ export abstract class AbstractWorker< * @throws {@link https://nodejs.org/api/errors.html#class-error} If the `name` parameter is a non-existing task function. */ public setDefaultTaskFunction (name: string): boolean { - if (typeof name !== 'string') { - throw new TypeError('name parameter is not a string') - } - if (typeof name === 'string' && name.trim().length === 0) { - throw new TypeError('name parameter is an empty string') - } + this.checkTaskFunctionName(name) if (name === DEFAULT_TASK_NAME) { throw new Error( 'Cannot set the default task function reserved name as the default task function' @@ -315,6 +295,15 @@ export abstract class AbstractWorker< } } + private checkTaskFunctionName (name: string): void { + if (typeof name !== 'string') { + throw new TypeError('name parameter is not a string') + } + if (typeof name === 'string' && name.trim().length === 0) { + throw new TypeError('name parameter is an empty string') + } + } + /** * Handles the ready message sent by the main worker. *