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'
WorkerChoiceStrategyContext
} from './selection-strategies'
-/**
- * An intentional empty function.
- */
-const EMPTY_FUNCTION: () => void = () => {
- /* Intentionally empty */
-}
-
/**
* Callback invoked if the worker raised an error.
*/
Worker extends IWorker,
Data = unknown,
Response = unknown
-> implements IPoolInternal<Worker, Data, Response> {
+> implements IPoolInternal<Worker, Data, Response>
+{
/** @inheritdoc */
public readonly workers: Worker[] = []
/**
* 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
},
- opts.workerChoiceStrategy ?? WorkerChoiceStrategies.ROUND_ROBIN
+ this.opts.workerChoiceStrategy
)
}
}
private checkPoolOptions (opts: PoolOptions<Worker>): void {
+ this.opts.workerChoiceStrategy =
+ opts.workerChoiceStrategy ?? WorkerChoiceStrategies.ROUND_ROBIN
this.opts.enableEvents = opts.enableEvents ?? true
}
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
}
worker: Worker,
messageId: number
): Promise<Response> {
+ this.increaseWorkersTask(worker)
return new Promise<Response>((resolve, reject) => {
this.promiseMap.set(messageId, { resolve, reject, worker })
})