From 416fd65cf4573917610e16e3339b2f1303ce8eda Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Sun, 9 Apr 2023 14:16:22 +0200 Subject: [PATCH] refactor: change tasks queue helpers scope MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- src/pools/abstract-pool.ts | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/src/pools/abstract-pool.ts b/src/pools/abstract-pool.ts index 93516fee..34aa634e 100644 --- a/src/pools/abstract-pool.ts +++ b/src/pools/abstract-pool.ts @@ -244,10 +244,12 @@ export abstract class AbstractPool< let currentTask: Task = submittedTask if ( this.opts.enableTasksQueue === true && - (this.busy || this.tasksQueueLength(workerNodeKey) > 0) + (this.busy || this.tasksQueueSize(workerNodeKey) > 0) ) { - this.enqueueTask(workerNodeKey, submittedTask) - currentTask = this.dequeueTask(workerNodeKey) as Task + currentTask = this.enqueueDequeueTask( + workerNodeKey, + submittedTask + ) as Task } this.sendToWorker(workerNode.worker, currentTask) this.checkAndEmitEvents() @@ -442,7 +444,7 @@ export abstract class AbstractPool< const workerNodeKey = this.getWorkerNodeKey(promiseResponse.worker) if ( this.opts.enableTasksQueue === true && - this.tasksQueueLength(workerNodeKey) > 0 + this.tasksQueueSize(workerNodeKey) > 0 ) { this.sendToWorker( promiseResponse.worker, @@ -542,26 +544,34 @@ export abstract class AbstractPool< * * @param worker - The worker. */ - protected removeWorkerNode (worker: Worker): void { + private removeWorkerNode (worker: Worker): void { const workerNodeKey = this.getWorkerNodeKey(worker) this.workerNodes.splice(workerNodeKey, 1) this.workerChoiceStrategyContext.remove(workerNodeKey) } - protected enqueueTask (workerNodeKey: number, task: Task): void { + private enqueueDequeueTask ( + workerNodeKey: number, + task: Task + ): Task | undefined { + this.enqueueTask(workerNodeKey, task) + return this.dequeueTask(workerNodeKey) + } + + private enqueueTask (workerNodeKey: number, task: Task): void { this.workerNodes[workerNodeKey].tasksQueue.push(task) } - protected dequeueTask (workerNodeKey: number): Task | undefined { + private dequeueTask (workerNodeKey: number): Task | undefined { return this.workerNodes[workerNodeKey].tasksQueue.shift() } - protected tasksQueueLength (workerNodeKey: number): number { + private tasksQueueSize (workerNodeKey: number): number { return this.workerNodes[workerNodeKey].tasksQueue.length } - protected flushTasksQueue (workerNodeKey: number): void { - if (this.tasksQueueLength(workerNodeKey) > 0) { + private flushTasksQueue (workerNodeKey: number): void { + if (this.tasksQueueSize(workerNodeKey) > 0) { for (const task of this.workerNodes[workerNodeKey].tasksQueue) { this.sendToWorker(this.workerNodes[workerNodeKey].worker, task) } @@ -569,7 +579,7 @@ export abstract class AbstractPool< } } - protected flushTasksQueueByWorker (worker: Worker): void { + private flushTasksQueueByWorker (worker: Worker): void { const workerNodeKey = this.getWorkerNodeKey(worker) this.flushTasksQueue(workerNodeKey) } -- 2.34.1