From f416c0987c6fe2d452de7e854b0d20f094448dd4 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Wed, 17 Feb 2021 21:57:30 +0100 Subject: [PATCH] Factor out worker tasks number in/de-crement. (#182) 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 | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/src/pools/abstract-pool.ts b/src/pools/abstract-pool.ts index 4a6ba72f..4f467bf4 100644 --- a/src/pools/abstract-pool.ts +++ b/src/pools/abstract-pool.ts @@ -201,26 +201,31 @@ export abstract class AbstractPool< /** * Increase the number of tasks that the given workers has done. * - * @param worker Workers whose tasks are increased. + * @param worker Worker whose tasks are increased. */ protected increaseWorkersTask (worker: Worker): void { - const numberOfTasksInProgress = this.tasks.get(worker) - if (numberOfTasksInProgress !== undefined) { - this.tasks.set(worker, numberOfTasksInProgress + 1) - } else { - throw Error('Worker could not be found in tasks map') - } + this.stepWorkerNumberOfTasks(worker, 1) } /** * Decrease the number of tasks that the given workers has done. * - * @param worker Workers whose tasks are decreased. + * @param worker Worker whose tasks are decreased. */ protected decreaseWorkersTasks (worker: Worker): void { + this.stepWorkerNumberOfTasks(worker, -1) + } + + /** + * Step the number of tasks that the given workers has done. + * + * @param worker Worker whose tasks are set. + * @param step Worker number of tasks step. + */ + private stepWorkerNumberOfTasks (worker: Worker, step: number) { const numberOfTasksInProgress = this.tasks.get(worker) if (numberOfTasksInProgress !== undefined) { - this.tasks.set(worker, numberOfTasksInProgress - 1) + this.tasks.set(worker, numberOfTasksInProgress + step) } else { throw Error('Worker could not be found in tasks map') } -- 2.34.1