- protected abstract registerWorkerMessageListener (
- port: Worker,
- listener: (message: MessageValue<Response>) => void
- ): void
+ /**
+ * Removes the given worker from the pool.
+ *
+ * @param worker Worker that will be removed.
+ */
+ protected removeWorker (worker: Worker): void {
+ // Clean worker from data structure
+ const workerIndex = this.workers.indexOf(worker)
+ this.workers.splice(workerIndex, 1)
+ this.tasks.delete(worker)
+ }
+
+ /**
+ * Choose a worker for the next task.
+ *
+ * The default implementation uses a round robin algorithm to distribute the load.
+ *
+ * @returns Worker.
+ */
+ protected chooseWorker (): Worker {
+ const chosenWorker = this.workers[this.nextWorkerIndex]
+ this.nextWorkerIndex =
+ this.workers.length - 1 === this.nextWorkerIndex
+ ? 0
+ : this.nextWorkerIndex + 1
+ return chosenWorker
+ }