2017-07-05 01:11:57 +02:00
|
|
|
/**
|
|
|
|
* Web Worker to handle communications between the front-end and the core.
|
|
|
|
*
|
|
|
|
* @author n1474335 [n1474335@gmail.com]
|
|
|
|
* @copyright Crown Copyright 2017
|
|
|
|
* @license Apache-2.0
|
|
|
|
*/
|
|
|
|
|
|
|
|
import "babel-polyfill";
|
2018-03-27 00:14:23 +02:00
|
|
|
import Chef from "./Chef";
|
|
|
|
import OperationConfig from "./config/OperationConfig.json";
|
2018-05-29 18:22:04 +02:00
|
|
|
import OpModules from "./config/modules/OpModules";
|
2017-08-09 21:09:23 +02:00
|
|
|
|
2017-12-29 18:32:23 +01:00
|
|
|
// Add ">" to the start of all log messages in the Chef Worker
|
|
|
|
import loglevelMessagePrefix from "loglevel-message-prefix";
|
|
|
|
|
|
|
|
loglevelMessagePrefix(log, {
|
|
|
|
prefixes: [],
|
|
|
|
staticPrefixes: [">"],
|
|
|
|
prefixFormat: "%p"
|
|
|
|
});
|
|
|
|
|
2017-07-05 01:11:57 +02:00
|
|
|
|
|
|
|
// Set up Chef instance
|
|
|
|
self.chef = new Chef();
|
2017-09-17 14:47:33 +02:00
|
|
|
|
2017-08-09 21:09:23 +02:00
|
|
|
self.OpModules = OpModules;
|
2017-09-17 14:47:33 +02:00
|
|
|
self.OperationConfig = OperationConfig;
|
2017-07-05 01:11:57 +02:00
|
|
|
|
2017-07-28 15:43:44 +02:00
|
|
|
// Tell the app that the worker has loaded and is ready to operate
|
|
|
|
self.postMessage({
|
|
|
|
action: "workerLoaded",
|
|
|
|
data: {}
|
|
|
|
});
|
|
|
|
|
2017-07-05 01:11:57 +02:00
|
|
|
/**
|
|
|
|
* Respond to message from parent thread.
|
|
|
|
*
|
|
|
|
* Messages should have the following format:
|
|
|
|
* {
|
|
|
|
* action: "bake" | "silentBake",
|
|
|
|
* data: {
|
|
|
|
* input: {string},
|
|
|
|
* recipeConfig: {[Object]},
|
|
|
|
* options: {Object},
|
|
|
|
* progress: {number},
|
|
|
|
* step: {boolean}
|
|
|
|
* } | undefined
|
|
|
|
* }
|
|
|
|
*/
|
|
|
|
self.addEventListener("message", function(e) {
|
|
|
|
// Handle message
|
2017-09-20 01:37:57 +02:00
|
|
|
const r = e.data;
|
2017-12-28 19:17:38 +01:00
|
|
|
log.debug("ChefWorker receiving command '" + r.action + "'");
|
|
|
|
|
2017-09-20 01:37:57 +02:00
|
|
|
switch (r.action) {
|
2017-07-05 01:11:57 +02:00
|
|
|
case "bake":
|
2017-09-20 01:37:57 +02:00
|
|
|
bake(r.data);
|
2017-07-05 01:11:57 +02:00
|
|
|
break;
|
|
|
|
case "silentBake":
|
2017-09-20 01:37:57 +02:00
|
|
|
silentBake(r.data);
|
2017-07-05 01:11:57 +02:00
|
|
|
break;
|
2018-04-21 13:25:48 +02:00
|
|
|
case "getDishAs":
|
|
|
|
getDishAs(r.data);
|
|
|
|
break;
|
2017-08-25 01:25:49 +02:00
|
|
|
case "docURL":
|
|
|
|
// Used to set the URL of the current document so that scripts can be
|
|
|
|
// imported into an inline worker.
|
2017-09-20 01:37:57 +02:00
|
|
|
self.docURL = r.data;
|
2017-08-25 01:25:49 +02:00
|
|
|
break;
|
2017-09-20 00:34:03 +02:00
|
|
|
case "highlight":
|
|
|
|
calculateHighlights(
|
2017-09-20 01:37:57 +02:00
|
|
|
r.data.recipeConfig,
|
|
|
|
r.data.direction,
|
|
|
|
r.data.pos
|
2017-09-20 00:34:03 +02:00
|
|
|
);
|
|
|
|
break;
|
2017-12-28 19:17:38 +01:00
|
|
|
case "setLogLevel":
|
|
|
|
log.setLevel(r.data, false);
|
|
|
|
break;
|
2017-07-05 01:11:57 +02:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Baking handler
|
2017-08-09 21:09:23 +02:00
|
|
|
*
|
|
|
|
* @param {Object} data
|
2017-07-05 01:11:57 +02:00
|
|
|
*/
|
|
|
|
async function bake(data) {
|
2017-08-09 21:09:23 +02:00
|
|
|
// Ensure the relevant modules are loaded
|
2018-01-23 00:34:24 +01:00
|
|
|
self.loadRequiredModules(data.recipeConfig);
|
2017-08-09 21:09:23 +02:00
|
|
|
|
2017-07-05 01:11:57 +02:00
|
|
|
try {
|
|
|
|
const response = await self.chef.bake(
|
|
|
|
data.input, // The user's input
|
|
|
|
data.recipeConfig, // The configuration of the recipe
|
|
|
|
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
|
|
|
|
);
|
|
|
|
|
|
|
|
self.postMessage({
|
2017-12-29 18:32:23 +01:00
|
|
|
action: "bakeComplete",
|
2018-06-03 18:33:13 +02:00
|
|
|
data: Object.assign(response, {
|
|
|
|
id: data.id
|
|
|
|
})
|
2017-07-05 01:11:57 +02:00
|
|
|
});
|
|
|
|
} catch (err) {
|
|
|
|
self.postMessage({
|
|
|
|
action: "bakeError",
|
2018-06-03 18:33:13 +02:00
|
|
|
data: Object.assign(err, {
|
|
|
|
id: data.id
|
|
|
|
})
|
2017-07-05 01:11:57 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Silent baking handler
|
|
|
|
*/
|
|
|
|
function silentBake(data) {
|
|
|
|
const duration = self.chef.silentBake(data.recipeConfig);
|
|
|
|
|
|
|
|
self.postMessage({
|
|
|
|
action: "silentBakeComplete",
|
|
|
|
data: duration
|
|
|
|
});
|
|
|
|
}
|
2017-08-09 21:09:23 +02:00
|
|
|
|
|
|
|
|
2018-04-21 13:25:48 +02:00
|
|
|
/**
|
|
|
|
* Translates the dish to a given type.
|
|
|
|
*/
|
|
|
|
async function getDishAs(data) {
|
|
|
|
const value = await self.chef.getDishAs(data.dish, data.type);
|
|
|
|
|
|
|
|
self.postMessage({
|
|
|
|
action: "dishReturned",
|
|
|
|
data: {
|
|
|
|
value: value,
|
|
|
|
id: data.id
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-09-20 00:34:03 +02:00
|
|
|
/**
|
|
|
|
* Calculates highlight offsets if possible.
|
|
|
|
*
|
|
|
|
* @param {Object[]} recipeConfig
|
|
|
|
* @param {string} direction
|
|
|
|
* @param {Object} pos - The position object for the highlight.
|
|
|
|
* @param {number} pos.start - The start offset.
|
|
|
|
* @param {number} pos.end - The end offset.
|
|
|
|
*/
|
|
|
|
function calculateHighlights(recipeConfig, direction, pos) {
|
|
|
|
pos = self.chef.calculateHighlights(recipeConfig, direction, pos);
|
|
|
|
|
|
|
|
self.postMessage({
|
|
|
|
action: "highlightsCalculated",
|
|
|
|
data: pos
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-01-23 00:34:24 +01:00
|
|
|
/**
|
|
|
|
* Checks that all required modules are loaded and loads them if not.
|
|
|
|
*
|
|
|
|
* @param {Object} recipeConfig
|
|
|
|
*/
|
|
|
|
self.loadRequiredModules = function(recipeConfig) {
|
|
|
|
recipeConfig.forEach(op => {
|
2018-05-20 17:49:42 +02:00
|
|
|
const module = self.OperationConfig[op.op].module;
|
2018-01-23 00:34:24 +01:00
|
|
|
|
|
|
|
if (!OpModules.hasOwnProperty(module)) {
|
2018-05-20 17:49:42 +02:00
|
|
|
log.info(`Loading ${module} module`);
|
|
|
|
self.sendStatusMessage(`Loading ${module} module`);
|
|
|
|
self.importScripts(`${self.docURL}/${module}.js`);
|
|
|
|
self.sendStatusMessage("");
|
2018-01-23 00:34:24 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2017-08-25 02:24:12 +02:00
|
|
|
/**
|
|
|
|
* Send status update to the app.
|
|
|
|
*
|
|
|
|
* @param {string} msg
|
|
|
|
*/
|
2017-08-31 01:24:24 +02:00
|
|
|
self.sendStatusMessage = function(msg) {
|
2017-08-25 02:24:12 +02:00
|
|
|
self.postMessage({
|
|
|
|
action: "statusMessage",
|
|
|
|
data: msg
|
|
|
|
});
|
2017-08-31 01:24:24 +02:00
|
|
|
};
|
2017-09-20 01:37:57 +02:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send an option value update to the app.
|
|
|
|
*
|
|
|
|
* @param {string} option
|
|
|
|
* @param {*} value
|
|
|
|
*/
|
|
|
|
self.setOption = function(option, value) {
|
|
|
|
self.postMessage({
|
|
|
|
action: "optionUpdate",
|
|
|
|
data: {
|
|
|
|
option: option,
|
|
|
|
value: value
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
2017-09-28 19:35:52 +02:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send register values back to the app.
|
|
|
|
*
|
|
|
|
* @param {number} opIndex
|
2017-09-28 20:39:35 +02:00
|
|
|
* @param {number} numPrevRegisters
|
2017-09-28 19:35:52 +02:00
|
|
|
* @param {string[]} registers
|
|
|
|
*/
|
2017-09-28 20:39:35 +02:00
|
|
|
self.setRegisters = function(opIndex, numPrevRegisters, registers) {
|
2017-09-28 19:35:52 +02:00
|
|
|
self.postMessage({
|
|
|
|
action: "setRegisters",
|
|
|
|
data: {
|
|
|
|
opIndex: opIndex,
|
2017-09-28 20:39:35 +02:00
|
|
|
numPrevRegisters: numPrevRegisters,
|
2017-09-28 19:35:52 +02:00
|
|
|
registers: registers
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|