MessageValue,
PromiseWorkerResponseWrapper
} from '../utility-types'
+import { EMPTY_FUNCTION } from '../utils'
import { isKillBehavior, KillBehaviors } from '../worker/worker-options'
import type { IPoolInternal } from './pool-internal'
import { PoolEmitter, PoolType } from './pool-internal'
} from './selection-strategies'
/**
- * An intentional empty function.
+ * Callback invoked if the worker has received a message.
*/
-const EMPTY_FUNCTION: () => void = () => {
- /* Intentionally empty */
-}
+export type MessageHandler<Worker> = (this: Worker, m: unknown) => void
/**
* Callback invoked if the worker raised an error.
* Basic interface that describes the minimum required implementation of listener events for a pool-worker.
*/
export interface IWorker {
+ /**
+ * Register a listener to the message event.
+ *
+ * @param event `'message'`.
+ * @param handler The message handler.
+ */
+ on(event: 'message', handler: MessageHandler<this>): void
/**
* Register a listener to the error event.
*
* Options for a poolifier pool.
*/
export interface PoolOptions<Worker> {
+ /**
+ * A function that will listen for message event on each worker.
+ */
+ messageHandler?: MessageHandler<Worker>
/**
* A function that will listen for error event on each worker.
*/
/**
* Pool events emission.
*
- * Default to true.
+ * @default true
*/
enableEvents?: boolean
}
/**
* The promise map.
*
- * - `key`: This is the message ID of each submitted task.
+ * - `key`: This is the message Id of each submitted task.
* - `value`: An object that contains the worker, the resolve function and the reject function.
*
* When we receive a message from the worker we get a map entry and resolve/reject the promise based on the message.
> = new Map<number, PromiseWorkerResponseWrapper<Worker, Response>>()
/**
- * ID of the next message.
+ * Id of the next message.
*/
protected nextMessageId: number = 0
this,
() => {
const workerCreated = this.createAndSetupWorker()
- this.registerWorkerMessageListener(workerCreated, message => {
+ this.registerWorkerMessageListener(workerCreated, async message => {
const tasksInProgress = this.tasks.get(workerCreated)
if (
isKillBehavior(KillBehaviors.HARD, message.kill) ||
tasksInProgress === 0
) {
// Kill received from the worker, means that no new tasks are submitted to that worker for a while ( > maxInactiveTime)
- void this.destroyWorker(workerCreated)
+ await this.destroyWorker(workerCreated)
}
})
return workerCreated
public execute (data: Data): Promise<Response> {
// Configure worker to handle message with the specified task
const worker = this.chooseWorker()
- this.increaseWorkersTask(worker)
- this.checkAndEmitBusy()
const messageId = ++this.nextMessageId
const res = this.internalExecute(worker, messageId)
+ this.checkAndEmitBusy()
this.sendToWorker(worker, { data: data || ({} as Data), id: messageId })
return res
}
protected abstract isMain (): boolean
/**
- * Increase the number of tasks that the given workers has applied.
+ * Increase the number of tasks that the given worker has applied.
*
* @param worker Worker whose tasks are increased.
*/
}
/**
- * Decrease the number of tasks that the given workers has applied.
+ * Decrease the number of tasks that the given worker has applied.
*
* @param worker Worker whose tasks are decreased.
*/
}
/**
- * Step the number of tasks that the given workers has applied.
+ * Step the number of tasks that the given worker has applied.
*
* @param worker Worker whose tasks are set.
* @param step Worker number of tasks step.
worker: Worker,
messageId: number
): Promise<Response> {
+ this.increaseWorkersTask(worker)
return new Promise<Response>((resolve, reject) => {
this.promiseMap.set(messageId, { resolve, reject, worker })
})
protected createAndSetupWorker (): Worker {
const worker: Worker = this.createWorker()
+ worker.on('message', this.opts.messageHandler ?? EMPTY_FUNCTION)
worker.on('error', this.opts.errorHandler ?? EMPTY_FUNCTION)
worker.on('online', this.opts.onlineHandler ?? EMPTY_FUNCTION)
worker.on('exit', this.opts.exitHandler ?? EMPTY_FUNCTION)