X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fworker%2Fabstract-worker.ts;h=bc62ade55ef15efac79c8c9e8835a45cb697aa40;hb=059cf20e59d680db96e1d812dd6320ba9af55c1c;hp=11a2263a5b5238ac4f728daa8ed97f6ca013c86f;hpb=01bc762f43e8acaa2420246a4dfa0167c26d2e32;p=poolifier.git diff --git a/src/worker/abstract-worker.ts b/src/worker/abstract-worker.ts index 11a2263a..bc62ade5 100644 --- a/src/worker/abstract-worker.ts +++ b/src/worker/abstract-worker.ts @@ -100,11 +100,38 @@ export abstract class AbstractWorker< } 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 ): void { @@ -126,7 +153,7 @@ export abstract class AbstractWorker< } /** - * 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. */ @@ -150,7 +177,7 @@ export abstract class AbstractWorker< } 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)