X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fpools%2Fabstract-pool.ts;h=c951c6f23792ea631b8804e11c7ff25719ce5fa5;hb=90bd5e4759bd31cc25b6f42e88d07ed05b969cec;hp=4f467bf4ebe869ac8215d8c5cc6aa1842edb7aad;hpb=f416c0987c6fe2d452de7e854b0d20f094448dd4;p=poolifier.git diff --git a/src/pools/abstract-pool.ts b/src/pools/abstract-pool.ts index 4f467bf4..c951c6f2 100644 --- a/src/pools/abstract-pool.ts +++ b/src/pools/abstract-pool.ts @@ -2,6 +2,13 @@ import EventEmitter from 'events' import type { MessageValue } from '../utility-types' import type { IPool } from './pool' +/** + * An intentional empty function. + */ +function emptyFunction () { + // intentionally left blank +} + /** * Callback invoked if the worker raised an error. */ @@ -142,10 +149,7 @@ export abstract class AbstractPool< if (!this.isMain()) { throw new Error('Cannot start a pool from a worker!') } - // TODO christopher 2021-02-07: Improve this check e.g. with a pattern or blank check - if (!this.filePath) { - throw new Error('Please specify a file with a worker implementation') - } + this.checkFilePath(this.filePath) this.setupHook() for (let i = 1; i <= this.numberOfWorkers; i++) { @@ -155,6 +159,12 @@ export abstract class AbstractPool< this.emitter = new PoolEmitter() } + private checkFilePath (filePath: string) { + if (!filePath) { + throw new Error('Please specify a file with a worker implementation') + } + } + /** * Perform the task specified in the constructor with the data parameter. * @@ -317,9 +327,9 @@ export abstract class AbstractPool< protected createAndSetupWorker (): Worker { const worker: Worker = this.createWorker() - worker.on('error', this.opts.errorHandler ?? (() => {})) - worker.on('online', this.opts.onlineHandler ?? (() => {})) - worker.on('exit', this.opts.exitHandler ?? (() => {})) + worker.on('error', this.opts.errorHandler ?? emptyFunction) + worker.on('online', this.opts.onlineHandler ?? emptyFunction) + worker.on('exit', this.opts.exitHandler ?? emptyFunction) worker.once('exit', () => this.removeWorker(worker)) this.workers.push(worker)