import { AsyncResource } from 'node:async_hooks'
import type { Worker } from 'node:cluster'
import type { MessagePort } from 'node:worker_threads'
-import type { MessageValue } from '../utility-types'
+import type {
+ MessageValue,
+ WorkerAsyncFunction,
+ WorkerFunction,
+ WorkerSyncFunction
+} from '../utility-types'
import { EMPTY_FUNCTION } from '../utils'
import type { KillBehavior, WorkerOptions } from './worker-options'
import { KillBehaviors } from './worker-options'
public constructor (
type: string,
protected readonly isMain: boolean,
- fn: (data: Data) => Response,
+ fn: WorkerFunction<Data, Response>,
protected mainWorker: MainWorker | undefined | null,
protected readonly opts: WorkerOptions = {
/**
}
) {
super(type)
- this.checkFunctionInput(fn)
this.checkWorkerOptions(this.opts)
+ this.checkFunctionInput(fn)
if (!this.isMain) {
- this.lastTaskTimestamp = Date.now()
+ this.lastTaskTimestamp = performance.now()
this.aliveInterval = setInterval(
this.checkAlive.bind(this),
(this.opts.maxInactiveTime ?? DEFAULT_MAX_INACTIVE_TIME) / 2
)
}
+ private checkWorkerOptions (opts: WorkerOptions): void {
+ this.opts.killBehavior = opts.killBehavior ?? DEFAULT_KILL_BEHAVIOR
+ this.opts.maxInactiveTime =
+ opts.maxInactiveTime ?? DEFAULT_MAX_INACTIVE_TIME
+ this.opts.async = opts.async ?? false
+ }
+
+ /**
+ * Checks if the `fn` parameter is passed to the constructor.
+ *
+ * @param fn - The function that should be defined.
+ */
+ private checkFunctionInput (fn: WorkerFunction<Data, Response>): 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'
+ )
+ }
+ }
+
/**
* Worker message listener.
*
*/
protected messageListener (
message: MessageValue<Data, MainWorker>,
- fn: (data: Data) => Response
+ fn: WorkerFunction<Data, Response>
): void {
- if (message.data != null && message.id != null) {
+ if (message.id != null && message.data != null) {
// Task message received
if (this.opts.async === true) {
this.runInAsyncScope(this.runAsync.bind(this), this, fn, message)
}
}
- private checkWorkerOptions (opts: WorkerOptions): void {
- this.opts.killBehavior = opts.killBehavior ?? DEFAULT_KILL_BEHAVIOR
- this.opts.maxInactiveTime =
- opts.maxInactiveTime ?? DEFAULT_MAX_INACTIVE_TIME
- this.opts.async = opts.async ?? false
- }
-
- /**
- * Checks 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 == null) throw new Error('fn parameter is mandatory')
- if (typeof fn !== 'function') {
- throw new TypeError('fn parameter is not a function')
- }
- }
-
/**
* Returns the main worker.
*
*/
protected checkAlive (): void {
if (
- Date.now() - this.lastTaskTimestamp >
+ performance.now() - this.lastTaskTimestamp >
(this.opts.maxInactiveTime ?? DEFAULT_MAX_INACTIVE_TIME)
) {
this.sendToMainWorker({ kill: this.opts.killBehavior })
* @param message - Input data for the given function.
*/
protected run (
- fn: (data?: Data) => Response,
+ fn: WorkerSyncFunction<Data, Response>,
message: MessageValue<Data>
): void {
try {
- const startTimestamp = Date.now()
+ const startTimestamp = performance.now()
const res = fn(message.data)
- const runTime = Date.now() - startTimestamp
- this.sendToMainWorker({ data: res, id: message.id, runTime })
+ const runTime = performance.now() - startTimestamp
+ this.sendToMainWorker({
+ data: res,
+ id: message.id,
+ runTime
+ })
} catch (e) {
const err = this.handleError(e as Error)
this.sendToMainWorker({ error: err, id: message.id })
} finally {
- !this.isMain && (this.lastTaskTimestamp = Date.now())
+ !this.isMain && (this.lastTaskTimestamp = performance.now())
}
}
* @param message - Input data for the given function.
*/
protected runAsync (
- fn: (data?: Data) => Promise<Response>,
+ fn: WorkerAsyncFunction<Data, Response>,
message: MessageValue<Data>
): void {
- const startTimestamp = Date.now()
+ const startTimestamp = performance.now()
fn(message.data)
.then(res => {
- const runTime = Date.now() - startTimestamp
- this.sendToMainWorker({ data: res, id: message.id, runTime })
+ const runTime = performance.now() - startTimestamp
+ this.sendToMainWorker({
+ data: res,
+ id: message.id,
+ runTime
+ })
return null
})
.catch(e => {
this.sendToMainWorker({ error: err, id: message.id })
})
.finally(() => {
- !this.isMain && (this.lastTaskTimestamp = Date.now())
+ !this.isMain && (this.lastTaskTimestamp = performance.now())
})
.catch(EMPTY_FUNCTION)
}