From a9f8dac656dc9bfac950d07bc74907ec2a7dbd4b Mon Sep 17 00:00:00 2001 From: j433866 Date: Tue, 2 Jul 2019 12:23:46 +0100 Subject: [PATCH] Fix stepping. Move stepping logic into App. Change toggleBakeButtonFunction to use a str instead of booleans. Tidy up handleLoaderMessage in InputWaiter. --- src/web/App.mjs | 28 ++++++++++++++ src/web/waiters/ControlsWaiter.mjs | 61 ++++++++++++------------------ src/web/waiters/InputWaiter.mjs | 20 ++++------ src/web/waiters/OutputWaiter.mjs | 4 +- src/web/waiters/WorkerWaiter.mjs | 19 +++++++--- 5 files changed, 77 insertions(+), 55 deletions(-) diff --git a/src/web/App.mjs b/src/web/App.mjs index 4e8ea06a..db530cf0 100755 --- a/src/web/App.mjs +++ b/src/web/App.mjs @@ -166,6 +166,34 @@ class App { } + /** + * Executes the next step of the recipe. + */ + step() { + if (this.baking) return; + + // Reset status using cancelBake + this.manager.worker.cancelBake(true, false); + + const activeTab = this.manager.tabs.getActiveInputTab(); + if (activeTab === -1) return; + + let progress = 0; + if (this.manager.output.outputs[activeTab].progress !== false) { + log.error(this.manager.output.outputs[activeTab]); + progress = this.manager.output.outputs[activeTab].progress; + } + + this.manager.input.inputWorker.postMessage({ + action: "step", + data: { + activeTab: activeTab, + progress: progress + 1 + } + }); + } + + /** * Runs a silent bake, forcing the browser to load and cache all the relevant JavaScript code needed * to do a real bake. diff --git a/src/web/waiters/ControlsWaiter.mjs b/src/web/waiters/ControlsWaiter.mjs index 83ebc63b..1a7b0684 100755 --- a/src/web/waiters/ControlsWaiter.mjs +++ b/src/web/waiters/ControlsWaiter.mjs @@ -68,23 +68,8 @@ class ControlsWaiter { /** * Handler for the 'Step through' command. Executes the next step of the recipe. */ - async stepClick() { - if (this.app.baking) return; - // Reset status using cancelBake - this.manager.worker.cancelBake(true, false); - const activeTab = this.manager.tabs.getActiveInputTab(); - let progress = 0; - if (this.manager.output.outputs[activeTab].progress !== false) { - progress = this.manager.output.outputs[activeTab].progress; - } - - this.manager.input.inputWorker.postMessage({ - action: "step", - data: { - activeTab: activeTab, - progress: progress + 1 - } - }); + stepClick() { + this.app.step(); } @@ -394,30 +379,32 @@ ${navigator.userAgent} /** * Switches the Bake button between 'Bake', 'Cancel' and 'Loading' functions. * - * @param {boolean} cancel - Whether to change to cancel or not - * @param {boolean} loading - Whether to change to loading or not + * @param {string} func - The function to change to. Either "cancel", "loading" or "bake" */ - toggleBakeButtonFunction(cancel, loading) { + toggleBakeButtonFunction(func) { const bakeButton = document.getElementById("bake"), btnText = bakeButton.querySelector("span"); - if (cancel) { - btnText.innerText = "Cancel"; - bakeButton.classList.remove("btn-success"); - bakeButton.classList.remove("btn-warning"); - bakeButton.classList.add("btn-danger"); - } else if (loading) { - bakeButton.style.background = ""; - btnText.innerText = "Loading..."; - bakeButton.classList.remove("btn-success"); - bakeButton.classList.remove("btn-danger"); - bakeButton.classList.add("btn-warning"); - } else { - bakeButton.style.background = ""; - btnText.innerText = "Bake!"; - bakeButton.classList.remove("btn-danger"); - bakeButton.classList.remove("btn-warning"); - bakeButton.classList.add("btn-success"); + switch (func) { + case "cancel": + btnText.innerText = "Cancel"; + bakeButton.classList.remove("btn-success"); + bakeButton.classList.remove("btn-warning"); + bakeButton.classList.add("btn-danger"); + break; + case "loading": + bakeButton.style.background = ""; + btnText.innerText = "Loading..."; + bakeButton.classList.remove("btn-success"); + bakeButton.classList.remove("btn-danger"); + bakeButton.classList.add("btn-warning"); + break; + default: + bakeButton.style.background = ""; + btnText.innerText = "Bake!"; + bakeButton.classList.remove("btn-danger"); + bakeButton.classList.remove("btn-warning"); + bakeButton.classList.add("btn-success"); } } diff --git a/src/web/waiters/InputWaiter.mjs b/src/web/waiters/InputWaiter.mjs index d2e97254..cf3aa03e 100644 --- a/src/web/waiters/InputWaiter.mjs +++ b/src/web/waiters/InputWaiter.mjs @@ -55,6 +55,8 @@ class InputWaiter { this.maxWorkers = 1; if (navigator.hardwareConcurrency !== undefined && navigator.hardwareConcurrency > 1) { + // Subtract 1 from hardwareConcurrency value to avoid using + // the entire available resources this.maxWorkers = navigator.hardwareConcurrency - 1; } } @@ -223,17 +225,11 @@ class InputWaiter { this.manager.tabs.updateInputTabProgress(r.inputNum, 100, 100); } - if (r.hasOwnProperty("fileBuffer")) { - this.inputWorker.postMessage({ - action: "loaderWorkerMessage", - data: r - }, [r.fileBuffer]); - } else { - this.inputWorker.postMessage({ - action: "loaderWorkerMessage", - data: r - }); - } + const transferable = r.hasOwnProperty("fileBuffer") ? [r.fileBuffer] : undefined; + this.inputWorker.postMessage({ + action: "loaderWorkerMessage", + data: r + }, transferable); } @@ -318,7 +314,7 @@ class InputWaiter { */ bakeAll() { this.app.progress = 0; - this.manager.controls.toggleBakeButtonFunction(false, true); + this.manager.controls.toggleBakeButtonFunction("loading"); this.inputWorker.postMessage({ action: "bakeAll" }); diff --git a/src/web/waiters/OutputWaiter.mjs b/src/web/waiters/OutputWaiter.mjs index 21e12d8a..26e790bf 100755 --- a/src/web/waiters/OutputWaiter.mjs +++ b/src/web/waiters/OutputWaiter.mjs @@ -233,7 +233,9 @@ class OutputWaiter { if (!this.outputExists(inputNum)) return; this.outputs[inputNum].progress = progress; - this.manager.tabs.updateOutputTabProgress(inputNum, progress, total); + if (progress !== false) { + this.manager.tabs.updateOutputTabProgress(inputNum, progress, total); + } } diff --git a/src/web/waiters/WorkerWaiter.mjs b/src/web/waiters/WorkerWaiter.mjs index 5414292e..ab279fca 100644 --- a/src/web/waiters/WorkerWaiter.mjs +++ b/src/web/waiters/WorkerWaiter.mjs @@ -194,6 +194,7 @@ class WorkerWaiter { } else { this.updateOutput(r.data, r.data.inputNum, r.data.bakeId, r.data.progress); } + this.app.progress = r.data.progress; if (r.data.progress === this.recipeConfig.length) { @@ -258,11 +259,16 @@ class WorkerWaiter { if (progress === this.recipeConfig.length) { progress = false; } - this.manager.output.updateOutputProgress(progress, inputNum); + this.manager.output.updateOutputProgress(progress, this.recipeConfig.length, inputNum); this.manager.output.updateOutputValue(data, inputNum, false); if (progress !== false) { this.manager.output.updateOutputStatus("error", inputNum); + + if (inputNum === this.manager.tabs.getActiveInputTab()) { + this.manager.recipe.updateBreakpointIndicator(progress); + } + } else { this.manager.output.updateOutputStatus("baked", inputNum); } @@ -275,7 +281,7 @@ class WorkerWaiter { */ setBakingStatus(bakingStatus) { this.app.baking = bakingStatus; - this.manager.controls.toggleBakeButtonFunction(bakingStatus); + this.manager.controls.toggleBakeButtonFunction(bakingStatus ? "cancel" : "bake"); if (bakingStatus) this.manager.output.hideMagicButton(); } @@ -403,6 +409,7 @@ class WorkerWaiter { } else { document.getElementById("bake-info").style.display = "none"; } + document.getElementById("bake").style.background = ""; this.totalOutputs = 0; // Reset for next time log.debug("--- Bake complete ---"); @@ -431,9 +438,11 @@ class WorkerWaiter { if (this.step) { // Remove all breakpoints from the recipe up to progress - for (let i = 0; i < this.app.progress; i++) { - if (recipeConfig[i].hasOwnProperty("breakpoint")) { - delete recipeConfig[i].breakpoint; + if (nextInput.progress !== false) { + for (let i = 0; i < nextInput.progress; i++) { + if (recipeConfig[i].hasOwnProperty("breakpoint")) { + delete recipeConfig[i].breakpoint; + } } }