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.
*/
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++) {
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.
*
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)
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(
})
}
+ /**
+ * Check if the `fn` parameter is passed to the constructor.
+ *
+ * @param fn The function that should be defined.
+ */
+ private checkFunctionInput (fn: (data: Data) => Response) {
+ if (!fn) throw new Error('fn parameter is mandatory')
+ }
+
/**
* Returns the main worker.
*
--- /dev/null
+const expect = require('expect')
+const { ThreadWorker } = require('../../lib')
+
+describe('Abstract worker test suite', () => {
+ it('Verify that fn function is mandatory', () => {
+ expect(() => {
+ const worker = new ThreadWorker()
+ }).toThrowError()
+ })
+})