From 6c711ec211556d045f6c1c0c4ddc0a97c686841b Mon Sep 17 00:00:00 2001 From: j433866 Date: Tue, 28 May 2019 14:11:05 +0100 Subject: [PATCH] Add and remove chefworkers when adding or removing inputs. This increases the responsiveness of baking as it means we don't have to... spin up workers when bake is clicked --- src/web/InputWaiter.mjs | 17 ++++++++++++++++- src/web/InputWorker.mjs | 17 ++++++++++++----- src/web/WorkerWaiter.mjs | 4 ++-- 3 files changed, 30 insertions(+), 8 deletions(-) diff --git a/src/web/InputWaiter.mjs b/src/web/InputWaiter.mjs index c9d5f8f6..6c19af97 100644 --- a/src/web/InputWaiter.mjs +++ b/src/web/InputWaiter.mjs @@ -284,6 +284,9 @@ class InputWaiter { case "getInput": this.callbacks[r.data.id](r.data); break; + case "removeChefWorker": + this.removeChefWorker(); + break; default: log.error(`Unknown action ${r.action}.`); } @@ -1200,6 +1203,17 @@ class InputWaiter { inputAdded(changeTab, inputNum) { this.addTab(inputNum, changeTab); this.manager.output.addOutput(inputNum, changeTab); + + this.manager.worker.addChefWorker(); + } + + /** + * Remove a chefWorker from the workerWaiter if we remove an input + */ + removeChefWorker() { + const workerIdx = this.manager.worker.getInactiveChefWorker(true); + const worker = this.manager.worker.chefWorkers[workerIdx]; + this.manager.worker.removeChefWorker(worker); } /** @@ -1258,7 +1272,8 @@ class InputWaiter { action: "removeInput", data: { inputNum: inputNum, - refreshTabs: refresh + refreshTabs: refresh, + removeChefWorker: true } }); diff --git a/src/web/InputWorker.mjs b/src/web/InputWorker.mjs index 37de69db..bf9cb340 100644 --- a/src/web/InputWorker.mjs +++ b/src/web/InputWorker.mjs @@ -716,7 +716,11 @@ self.loadFiles = function(filesData) { const inputNums = []; for (let i = 0; i < files.length; i++) { if (i === 0 && self.getInputValue(activeTab) === "") { - self.removeInput(activeTab); + self.removeInput({ + inputNum: activeTab, + refreshTabs: false, + removeChefWorker: false + }); lastInputNum = self.addInput(false, "file", { name: files[i].name, size: files[i].size.toLocaleString(), @@ -805,6 +809,7 @@ self.addInput = function(changeTab=false, type, fileData={name: "unknown", size: * @param {object} removeInputData * @param {number} removeInputData.inputNum - The number of the input to be removed * @param {boolean} removeInputData.refreshTabs - If true, refresh the tabs after removing the input + * @param {boolean} removeInputData.removeChefWorker - If true, remove a chefWorker from the WorkerWaiter */ self.removeInput = function(removeInputData) { const inputNum = removeInputData.inputNum; @@ -830,13 +835,15 @@ self.removeInput = function(removeInputData) { delete self.inputs[inputNum]; - if (Object.keys(self.inputs).length === 0) { - self.addInput(true, "string"); - } - if (refreshTabs) { self.refreshTabs(inputNum, "left"); } + + if (self.numInputs < self.maxWorkers && removeInputData.removeChefWorker) { + self.postMessage({ + action: "removeChefWorker" + }); + } }; /** diff --git a/src/web/WorkerWaiter.mjs b/src/web/WorkerWaiter.mjs index e57521b2..09d4e3ba 100644 --- a/src/web/WorkerWaiter.mjs +++ b/src/web/WorkerWaiter.mjs @@ -106,7 +106,7 @@ class WorkerWaiter { const newWorkerObj = { worker: newWorker, active: false, - inputNum: this.manager.input.getActiveTab() + inputNum: -1 }; this.chefWorkers.push(newWorkerObj); @@ -141,6 +141,7 @@ class WorkerWaiter { } if (this.chefWorkers.length > 1 || this.chefWorkers[index].active) { + log.debug(`Removing ChefWorker at index ${index}`); this.chefWorkers[index].worker.terminate(); this.chefWorkers.splice(index, 1); } @@ -230,7 +231,6 @@ class WorkerWaiter { this.app.options[r.data.option] = r.data.value; break; case "setRegisters": - // Should this update with the tabs? this.manager.recipe.setRegisters(r.data.opIndex, r.data.numPrevRegisters, r.data.registers); break; case "highlightsCalculated":