From: Jérôme Benoit Date: Sun, 27 Aug 2023 21:27:47 +0000 (+0200) Subject: refactor: factor out task function validation X-Git-Tag: v2.6.37~19 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=0628df39eb1eb95630d08d9759a83c750a34ff7e;p=poolifier.git refactor: factor out task function validation Signed-off-by: Jérôme Benoit --- diff --git a/src/worker/abstract-worker.ts b/src/worker/abstract-worker.ts index 23c661c9..c00cf083 100644 --- a/src/worker/abstract-worker.ts +++ b/src/worker/abstract-worker.ts @@ -103,6 +103,27 @@ export abstract class AbstractWorker< delete this.opts.async } + private checkValidTaskFunction ( + name: string, + fn: TaskFunction + ): 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. * @@ -128,21 +149,7 @@ export abstract class AbstractWorker< } 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)