From: Jérôme Benoit Date: Wed, 17 Feb 2021 20:57:30 +0000 (+0100) Subject: Factor out worker tasks number in/de-crement. (#182) X-Git-Tag: v2.0.0-beta.2~8 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=f416c0987c6fe2d452de7e854b0d20f094448dd4;p=poolifier.git Factor out worker tasks number in/de-crement. (#182) Signed-off-by: Jérôme Benoit --- 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') }