Remove step and progress from Chef.

It's now all handled by the App!
This commit is contained in:
j433866 2019-06-04 14:02:45 +01:00
parent 0e5944e9c6
commit 31a3af1f84
6 changed files with 56 additions and 79 deletions

View File

@ -28,8 +28,6 @@ class Chef {
* @param {Object[]} recipeConfig - The recipe configuration object * @param {Object[]} recipeConfig - The recipe configuration object
* @param {Object} options - The options object storing various user choices * @param {Object} options - The options object storing various user choices
* @param {boolean} options.attempHighlight - Whether or not to attempt highlighting * @param {boolean} options.attempHighlight - Whether or not to attempt highlighting
* @param {number} progress - The position in the recipe to start from
* @param {number} [step] - Whether to only execute one operation in the recipe
* *
* @returns {Object} response * @returns {Object} response
* @returns {string} response.result - The output of the recipe * @returns {string} response.result - The output of the recipe
@ -38,48 +36,20 @@ class Chef {
* @returns {number} response.duration - The number of ms it took to execute the recipe * @returns {number} response.duration - The number of ms it took to execute the recipe
* @returns {number} response.error - The error object thrown by a failed operation (false if no error) * @returns {number} response.error - The error object thrown by a failed operation (false if no error)
*/ */
async bake(input, recipeConfig, options, progress, step) { async bake(input, recipeConfig, options) {
log.debug("Chef baking"); log.debug("Chef baking");
const startTime = new Date().getTime(), const startTime = new Date().getTime(),
recipe = new Recipe(recipeConfig), recipe = new Recipe(recipeConfig),
containsFc = recipe.containsFlowControl(), containsFc = recipe.containsFlowControl(),
notUTF8 = options && options.hasOwnProperty("treatAsUtf8") && !options.treatAsUtf8; notUTF8 = options && options.hasOwnProperty("treatAsUtf8") && !options.treatAsUtf8;
let error = false; let error = false,
progress = 0;
if (containsFc && ENVIRONMENT_IS_WORKER()) self.setOption("attemptHighlight", false); if (containsFc && ENVIRONMENT_IS_WORKER()) self.setOption("attemptHighlight", false);
// Clean up progress // Load data
if (progress >= recipeConfig.length) { const type = input instanceof ArrayBuffer ? Dish.ARRAY_BUFFER : Dish.STRING;
progress = 0; this.dish.set(input, type);
}
if (step) {
// Unset breakpoint on this step
recipe.setBreakpoint(progress, false);
// Set breakpoint on next step
recipe.setBreakpoint(progress + 1, true);
}
// If the previously run operation presented a different value to its
// normal output, we need to recalculate it.
if (recipe.lastOpPresented(progress)) {
progress = 0;
}
// If stepping with flow control, we have to start from the beginning
// but still want to skip all previous breakpoints.
// If stepping, we need to start from the beginning as the current dish
// value may not be for the correct input, so should be recalculated.
if (progress > 0 && containsFc || step) {
recipe.removeBreaksUpTo(progress);
progress = 0;
}
// If starting from scratch, load data
if (progress === 0) {
const type = input instanceof ArrayBuffer ? Dish.ARRAY_BUFFER : Dish.STRING;
this.dish.set(input, type);
}
try { try {
progress = await recipe.execute(this.dish, progress); progress = await recipe.execute(this.dish, progress);

View File

@ -102,9 +102,7 @@ async function bake(data) {
const response = await self.chef.bake( const response = await self.chef.bake(
data.input, // The user's input data.input, // The user's input
data.recipeConfig, // The configuration of the recipe data.recipeConfig, // The configuration of the recipe
data.options, // Options set by the user data.options // Options set by the user
data.progress, // The current position in the recipe
data.step // Whether or not to take one step or execute the whole recipe
); );
const transferable = (data.input instanceof ArrayBuffer) ? [data.input] : undefined; const transferable = (data.input instanceof ArrayBuffer) ? [data.input] : undefined;

View File

@ -156,7 +156,9 @@ class App {
log.debug("Auto-baking"); log.debug("Auto-baking");
this.manager.input.inputWorker.postMessage({ this.manager.input.inputWorker.postMessage({
action: "autobake", action: "autobake",
data: this.manager.input.getActiveTab() data: {
activeTab: this.manager.input.getActiveTab()
}
}); });
} else { } else {
this.manager.controls.showStaleIndicator(); this.manager.controls.showStaleIndicator();

View File

@ -73,16 +73,17 @@ class ControlsWaiter {
// Reset status using cancelBake // Reset status using cancelBake
this.manager.worker.cancelBake(true, false); this.manager.worker.cancelBake(true, false);
const activeTab = this.manager.input.getActiveTab(); const activeTab = this.manager.input.getActiveTab();
let progress = 0;
if (this.manager.output.outputs[activeTab].progress === false){ if (this.manager.output.outputs[activeTab].progress !== false) {
this.app.progress = 0; progress = this.manager.output.outputs[activeTab].progress;
} else {
this.app.progress = this.manager.output.outputs[activeTab].progress;
} }
this.manager.input.inputWorker.postMessage({ this.manager.input.inputWorker.postMessage({
action: "step", action: "step",
data: activeTab data: {
activeTab: activeTab,
progress: progress + 1
}
}); });
} }

View File

@ -85,7 +85,7 @@ self.addEventListener("message", function(e) {
self.changeTabLeft(r.data.activeTab, r.data.nums); self.changeTabLeft(r.data.activeTab, r.data.nums);
break; break;
case "autobake": case "autobake":
self.autoBake(r.data, false); self.autoBake(r.data.activeTab, 0, false);
break; break;
case "filterTabs": case "filterTabs":
self.filterTabs(r.data); self.filterTabs(r.data);
@ -100,7 +100,7 @@ self.addEventListener("message", function(e) {
self.updateTabHeader(r.data); self.updateTabHeader(r.data);
break; break;
case "step": case "step":
self.autoBake(r.data, true); self.autoBake(r.data.activeTab, r.data.progress, true);
break; break;
case "getInput": case "getInput":
self.getInput(r.data); self.getInput(r.data);
@ -145,17 +145,19 @@ self.getLoadProgress = function(inputNum) {
* Queues the active input and sends a bake command. * Queues the active input and sends a bake command.
* *
* @param {number} inputNum - The input to be baked * @param {number} inputNum - The input to be baked
* @param {number} progress - The current progress of the bake through the recipe
* @param {boolean} [step=false] - Set to true if we should only execute one operation instead of the * @param {boolean} [step=false] - Set to true if we should only execute one operation instead of the
* whole recipe * whole recipe
*/ */
self.autoBake = function(inputNum, step=false) { self.autoBake = function(inputNum, progress, step=false) {
const input = self.getInputObj(inputNum); const input = self.getInputObj(inputNum);
if (input) { if (input) {
self.postMessage({ self.postMessage({
action: "bakeAllInputs", action: "bakeAllInputs",
data: { data: {
nums: [parseInt(inputNum, 10)], nums: [parseInt(inputNum, 10)],
step: step step: step,
progress: progress
} }
}); });
} }
@ -178,7 +180,8 @@ self.bakeAllInputs = function() {
action: "bakeAllInputs", action: "bakeAllInputs",
data: { data: {
nums: nums, nums: nums,
step: false step: false,
progress: 0
} }
}); });
}; };

View File

@ -414,35 +414,36 @@ class WorkerWaiter {
this.manager.output.updateOutputStatus("baking", nextInput.inputNum); this.manager.output.updateOutputStatus("baking", nextInput.inputNum);
this.chefWorkers[workerIdx].inputNum = nextInput.inputNum; this.chefWorkers[workerIdx].inputNum = nextInput.inputNum;
const input = nextInput.input; const input = nextInput.input,
if (input instanceof ArrayBuffer || ArrayBuffer.isView(input)) { recipeConfig = this.recipeConfig;
this.chefWorkers[workerIdx].worker.postMessage({
action: "bake", if (this.step) {
data: { // Remove all breakpoints from the recipe up to progress
input: input, for (let i = 0; i < this.app.progress; i++) {
recipeConfig: this.recipeConfig, if (recipeConfig[i].hasOwnProperty("breakpoint")) {
options: this.options, delete recipeConfig[i].breakpoint;
progress: this.progress,
step: this.step,
inputNum: nextInput.inputNum,
bakeId: this.bakeId
} }
}, [input]); }
} else {
this.chefWorkers[workerIdx].worker.postMessage({ // Set a breakpoint at the next operation so we stop baking there
action: "bake", if (recipeConfig[this.app.progress]) recipeConfig[this.app.progress].breakpoint = true;
data: {
input: input,
recipeConfig: this.recipeConfig,
options: this.options,
progress: this.progress,
step: this.step,
inputNum: nextInput.inputNum,
bakeId: this.bakeId
}
});
} }
let transferable;
if (input instanceof ArrayBuffer || ArrayBuffer.isView(input)) {
transferable = [input];
}
this.chefWorkers[workerIdx].worker.postMessage({
action: "bake",
data: {
input: input,
recipeConfig: recipeConfig,
options: this.options,
inputNum: nextInput.inputNum,
bakeId: this.bakeId
}
}, transferable);
if (this.inputNums.length > 0) { if (this.inputNums.length > 0) {
this.manager.input.inputWorker.postMessage({ this.manager.input.inputWorker.postMessage({
action: "bakeNext", action: "bakeNext",
@ -523,8 +524,9 @@ class WorkerWaiter {
* Queues a list of inputNums to be baked by ChefWorkers, and begins baking * Queues a list of inputNums to be baked by ChefWorkers, and begins baking
* *
* @param {object} inputData * @param {object} inputData
* @param {number[]} inputData.nums * @param {number[]} inputData.nums - The inputNums to be queued for baking
* @param {boolean} inputData.step * @param {boolean} inputData.step - If true, only execute the next operation in the recipe
* @param {number} inputData.progress - The current progress through the recipe. Used when stepping
*/ */
async bakeAllInputs(inputData) { async bakeAllInputs(inputData) {
return await new Promise(resolve => { return await new Promise(resolve => {
@ -537,6 +539,7 @@ class WorkerWaiter {
this.inputNums = inputNums; this.inputNums = inputNums;
this.totalOutputs = inputNums.length; this.totalOutputs = inputNums.length;
this.app.progress = inputData.progress;
let inactiveWorkers = 0; let inactiveWorkers = 0;
for (let i = 0; i < this.chefWorkers.length; i++) { for (let i = 0; i < this.chefWorkers.length; i++) {