X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fworker%2Fabstract-worker.ts;h=4f741b47acf134146069e33d866b8f40a09e0bb1;hb=a35560bac09e829e1e19f88f8fd1d71a64c9d50b;hp=6aa34e8e899fda868bf0c1935b656a21d8b67629;hpb=aad2595fd9c26bcb2f0d7a22d06edf56d5b08bdb;p=poolifier.git diff --git a/src/worker/abstract-worker.ts b/src/worker/abstract-worker.ts index 6aa34e8e..4f741b47 100644 --- a/src/worker/abstract-worker.ts +++ b/src/worker/abstract-worker.ts @@ -12,8 +12,8 @@ const DEFAULT_KILL_BEHAVIOR: KillBehavior = KillBehaviors.SOFT * Base class containing some shared logic for all poolifier workers. * * @template MainWorker Type of main worker. - * @template Data Type of data this worker receives from pool's execution. - * @template Response Type of response the worker sends back to the main worker. + * @template Data Type of data this worker receives from pool's execution. This can only be serializable data. + * @template Response Type of response the worker sends back to the main worker. This can only be serializable data. */ export abstract class AbstractWorker< MainWorker extends Worker | MessagePort, @@ -66,7 +66,7 @@ export abstract class AbstractWorker< this.opts.maxInactiveTime ?? DEFAULT_MAX_INACTIVE_TIME this.async = !!this.opts.async this.lastTask = Date.now() - if (!fn) throw new Error('fn parameter is mandatory') + this.checkFunctionInput(fn) // Keep the worker active if (!isMain) { this.interval = setInterval( @@ -96,6 +96,15 @@ export abstract class AbstractWorker< }) } + /** + * Check if the `fn` parameter is passed to the constructor. + * + * @param fn The function that should be defined. + */ + private checkFunctionInput (fn: (data: Data) => Response): void { + if (!fn) throw new Error('fn parameter is mandatory') + } + /** * Returns the main worker. *