X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fworker%2Fabstract-worker.ts;h=73eec1c6999d2aae78fa7e0a70038d30d6993997;hb=181eb2b40f0493af369dd9f36d1951d1c04d9933;hp=7563e97bf5cadf01b296ed1811def122d6e875dc;hpb=a3f5f781d7f502c040dd2baacd225f308446bff6;p=poolifier.git diff --git a/src/worker/abstract-worker.ts b/src/worker/abstract-worker.ts index 7563e97b..73eec1c6 100644 --- a/src/worker/abstract-worker.ts +++ b/src/worker/abstract-worker.ts @@ -41,7 +41,7 @@ export abstract class AbstractWorker< public constructor ( type: string, protected readonly isMain: boolean, - fn: (data: Data) => Response, + fn: (data: Data) => Response | Promise, protected mainWorker: MainWorker | undefined | null, protected readonly opts: WorkerOptions = { /** @@ -56,8 +56,8 @@ export abstract class AbstractWorker< } ) { super(type) - this.checkFunctionInput(fn) this.checkWorkerOptions(this.opts) + this.checkFunctionInput(fn) if (!this.isMain) { this.lastTaskTimestamp = performance.now() this.aliveInterval = setInterval( @@ -83,7 +83,7 @@ export abstract class AbstractWorker< */ protected messageListener ( message: MessageValue, - fn: (data: Data) => Response + fn: (data: Data) => Response | Promise ): void { if (message.id != null && message.data != null) { // Task message received @@ -114,11 +114,18 @@ export abstract class AbstractWorker< * * @param fn - The function that should be defined. */ - private checkFunctionInput (fn: (data: Data) => Response): void { + private checkFunctionInput ( + fn: (data: Data) => Response | Promise + ): void { if (fn == null) throw new Error('fn parameter is mandatory') if (typeof fn !== 'function') { throw new TypeError('fn parameter is not a function') } + if (fn.constructor.name === 'AsyncFunction' && this.opts.async === false) { + throw new Error( + 'fn parameter is an async function, please set the async option to true' + ) + } } /**