Make set() asynchronous

This commit is contained in:
j433866 2019-05-08 13:46:29 +01:00
parent 3cffd9334f
commit 50f9bbeac3
2 changed files with 140 additions and 137 deletions

View File

@ -306,39 +306,40 @@ class InputWaiter {
* @param {number} inputData.progress * @param {number} inputData.progress
* @param {boolean} [silent=false] * @param {boolean} [silent=false]
*/ */
set(inputData, silent=false) { async set(inputData, silent=false) {
const activeTab = this.getActiveTab(); return new Promise(function(resolve, reject) {
if (inputData.inputNum !== activeTab) return; const activeTab = this.getActiveTab();
if (inputData.inputNum !== activeTab) return;
const inputText = document.getElementById("input-text"); const inputText = document.getElementById("input-text");
if (typeof inputData.input === "string") { if (typeof inputData.input === "string") {
inputText.value = inputData.input; inputText.value = inputData.input;
// close file // close file
const fileOverlay = document.getElementById("input-file"), const fileOverlay = document.getElementById("input-file"),
fileName = document.getElementById("input-file-name"), fileName = document.getElementById("input-file-name"),
fileSize = document.getElementById("input-file-size"), fileSize = document.getElementById("input-file-size"),
fileType = document.getElementById("input-file-type"), fileType = document.getElementById("input-file-type"),
fileLoaded = document.getElementById("input-file-loaded"); fileLoaded = document.getElementById("input-file-loaded");
fileOverlay.style.display = "none"; fileOverlay.style.display = "none";
fileName.textContent = ""; fileName.textContent = "";
fileSize.textContent = ""; fileSize.textContent = "";
fileType.textContent = ""; fileType.textContent = "";
fileLoaded.textContent = ""; fileLoaded.textContent = "";
inputText.style.overflow = "auto"; inputText.style.overflow = "auto";
inputText.classList.remove("blur"); inputText.classList.remove("blur");
const lines = inputData.input.length < (this.app.options.ioDisplayThreshold * 1024) ? const lines = inputData.input.length < (this.app.options.ioDisplayThreshold * 1024) ?
inputData.input.count("\n") + 1 : null; inputData.input.count("\n") + 1 : null;
this.setInputInfo(inputData.input.length, lines); this.setInputInfo(inputData.input.length, lines);
} else { } else {
this.setFile(inputData); this.setFile(inputData);
} }
if (!silent) window.dispatchEvent(this.manager.statechange);
if (!silent) window.dispatchEvent(this.manager.statechange);
}.bind(this));
} }
/** /**

View File

@ -65,6 +65,8 @@ class OutputWaiter {
} }
data = data.buffer; data = data.buffer;
} else if (typeof data !== "object" && typeof data !== "string") {
data = String(data);
} }
return data; return data;
} else if (typeof this.outputs[inputNum].data.result === "string") { } else if (typeof this.outputs[inputNum].data.result === "string") {
@ -226,131 +228,133 @@ class OutputWaiter {
* *
* @param {number} inputNum * @param {number} inputNum
*/ */
set(inputNum) { async set(inputNum) {
const output = this.outputs[inputNum]; return new Promise(function(resolve, reject) {
if (output === undefined || output === null) return; const output = this.outputs[inputNum];
if (typeof inputNum !== "number") inputNum = parseInt(inputNum, 10); if (output === undefined || output === null) return;
if (typeof inputNum !== "number") inputNum = parseInt(inputNum, 10);
if (inputNum !== this.getActiveTab()) return; if (inputNum !== this.getActiveTab()) return;
const outputText = document.getElementById("output-text"); const outputText = document.getElementById("output-text");
const outputHtml = document.getElementById("output-html"); const outputHtml = document.getElementById("output-html");
const outputFile = document.getElementById("output-file"); const outputFile = document.getElementById("output-file");
const outputHighlighter = document.getElementById("output-highlighter"); const outputHighlighter = document.getElementById("output-highlighter");
const inputHighlighter = document.getElementById("input-highlighter"); const inputHighlighter = document.getElementById("input-highlighter");
// If pending or baking, show loader and status message // If pending or baking, show loader and status message
// If error, style the tab and handle the error // If error, style the tab and handle the error
// If done, display the output if it's the active tab // If done, display the output if it's the active tab
// If inactive, show the last bake value (or blank) // If inactive, show the last bake value (or blank)
if (output.status === "inactive" || if (output.status === "inactive" ||
output.status === "stale" || output.status === "stale" ||
(output.status === "baked" && output.bakeId < this.manager.worker.bakeId)) { (output.status === "baked" && output.bakeId < this.manager.worker.bakeId)) {
this.manager.controls.showStaleIndicator(); this.manager.controls.showStaleIndicator();
} else { } else {
this.manager.controls.hideStaleIndicator(); this.manager.controls.hideStaleIndicator();
}
if (output.progress !== undefined) {
this.manager.recipe.updateBreakpointIndicator(output.progress);
} else {
this.manager.recipe.updateBreakpointIndicator(false);
}
document.getElementById("show-file-overlay").style.display = "none";
if (output.status === "pending" || output.status === "baking") {
// show the loader and the status message if it's being shown
// otherwise don't do anything
this.toggleLoader(true);
document.querySelector("#output-loader .loading-msg").textContent = output.statusMessage;
} else if (output.status === "error") {
// style the tab if it's being shown
// run app.handleError()
this.toggleLoader(false);
outputText.style.display = "block";
outputText.classList.remove("blur");
outputHtml.style.display = "none";
outputFile.style.display = "none";
outputHighlighter.display = "none";
inputHighlighter.display = "none";
outputText.value = output.error;
outputHtml.innerHTML = "";
} else if (output.status === "baked" || output.status === "inactive") {
this.displayTabInfo(inputNum);
this.toggleLoader(false);
this.closeFile();
let scriptElements, lines, length;
if (output.data === null) {
outputText.style.display = "block";
outputHtml.style.display = "none";
outputFile.style.display = "none";
outputHighlighter.display = "block";
inputHighlighter.display = "block";
outputText.value = "";
outputHtml.innerHTML = "";
lines = 0;
length = 0;
return;
} }
switch (output.data.type) { if (output.progress !== undefined) {
case "html": this.manager.recipe.updateBreakpointIndicator(output.progress);
outputText.style.display = "none"; } else {
outputHtml.style.display = "block"; this.manager.recipe.updateBreakpointIndicator(false);
outputFile.style.display = "none"; }
outputHighlighter.style.display = "none";
inputHighlighter.style.display = "none";
outputText.value = ""; document.getElementById("show-file-overlay").style.display = "none";
outputHtml.innerHTML = output.data.result;
// Execute script sections if (output.status === "pending" || output.status === "baking") {
scriptElements = outputHtml.querySelectorAll("script"); // show the loader and the status message if it's being shown
for (let i = 0; i < scriptElements.length; i++) { // otherwise don't do anything
try { this.toggleLoader(true);
eval(scriptElements[i].innerHTML); // eslint-disable-line no-eval document.querySelector("#output-loader .loading-msg").textContent = output.statusMessage;
} catch (err) {
log.error(err);
}
}
length = output.data.dish.value.length;
break; } else if (output.status === "error") {
case "ArrayBuffer": // style the tab if it's being shown
outputText.style.display = "block"; // run app.handleError()
outputHtml.style.display = "none"; this.toggleLoader(false);
outputHighlighter.display = "none"; outputText.style.display = "block";
inputHighlighter.display = "none"; outputText.classList.remove("blur");
outputHtml.style.display = "none";
outputFile.style.display = "none";
outputHighlighter.display = "none";
inputHighlighter.display = "none";
outputText.value = ""; outputText.value = output.error;
outputHtml.innerHTML = ""; outputHtml.innerHTML = "";
} else if (output.status === "baked" || output.status === "inactive") {
this.displayTabInfo(inputNum);
this.toggleLoader(false);
this.closeFile();
let scriptElements, lines, length;
length = output.data.result.length; if (output.data === null) {
this.setFile(output.data.result);
break;
case "string":
default:
outputText.style.display = "block"; outputText.style.display = "block";
outputHtml.style.display = "none"; outputHtml.style.display = "none";
outputFile.style.display = "none"; outputFile.style.display = "none";
outputHighlighter.display = "block"; outputHighlighter.display = "block";
inputHighlighter.display = "block"; inputHighlighter.display = "block";
outputText.value = Utils.printable(output.data.result, true); outputText.value = "";
outputHtml.innerHTML = ""; outputHtml.innerHTML = "";
lines = output.data.result.count("\n") + 1; lines = 0;
length = output.data.result.length; length = 0;
break; return;
}
switch (output.data.type) {
case "html":
outputText.style.display = "none";
outputHtml.style.display = "block";
outputFile.style.display = "none";
outputHighlighter.style.display = "none";
inputHighlighter.style.display = "none";
outputText.value = "";
outputHtml.innerHTML = output.data.result;
// Execute script sections
scriptElements = outputHtml.querySelectorAll("script");
for (let i = 0; i < scriptElements.length; i++) {
try {
eval(scriptElements[i].innerHTML); // eslint-disable-line no-eval
} catch (err) {
log.error(err);
}
}
length = output.data.dish.value.length;
break;
case "ArrayBuffer":
outputText.style.display = "block";
outputHtml.style.display = "none";
outputHighlighter.display = "none";
inputHighlighter.display = "none";
outputText.value = "";
outputHtml.innerHTML = "";
length = output.data.result.length;
this.setFile(output.data.result);
break;
case "string":
default:
outputText.style.display = "block";
outputHtml.style.display = "none";
outputFile.style.display = "none";
outputHighlighter.display = "block";
inputHighlighter.display = "block";
outputText.value = Utils.printable(output.data.result, true);
outputHtml.innerHTML = "";
lines = output.data.result.count("\n") + 1;
length = output.data.result.length;
break;
}
this.setOutputInfo(length, lines, output.data.duration);
this.backgroundMagic();
} }
this.setOutputInfo(length, lines, output.data.duration); }.bind(this));
this.backgroundMagic();
}
} }
/** /**
@ -359,8 +363,6 @@ class OutputWaiter {
* @param {ArrayBuffer} buf * @param {ArrayBuffer} buf
*/ */
setFile(buf) { setFile(buf) {
const file = new File([buf], "output.dat");
// Display file overlay in output area with details // Display file overlay in output area with details
const fileOverlay = document.getElementById("output-file"), const fileOverlay = document.getElementById("output-file"),
fileSize = document.getElementById("output-file-size"), fileSize = document.getElementById("output-file-size"),
@ -368,7 +370,7 @@ class OutputWaiter {
fileSlice = buf.slice(0, 4096); fileSlice = buf.slice(0, 4096);
fileOverlay.style.display = "block"; fileOverlay.style.display = "block";
fileSize.textContent = file.size.toLocaleString() + " bytes"; fileSize.textContent = buf.byteLength.toLocaleString() + " bytes";
outputText.classList.add("blur"); outputText.classList.add("blur");
outputText.value = Utils.printable(Utils.arrayBufferToStr(fileSlice)); outputText.value = Utils.printable(Utils.arrayBufferToStr(fileSlice));