aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Sherlock <d.t.sherlock@live.co.uk>2024-01-22 15:45:22 +0000
committerLouis Pilfold <louis@lpil.uk>2024-01-23 14:03:14 +0000
commitba9abad7375bb3768f2650fe3d086f65f091a5f4 (patch)
tree10236429a4fbae42895f8294652b4ad95b70b3d8
parent2935d24405836ad81d6170bc1c76b6ef972e34d1 (diff)
downloadtour-ba9abad7375bb3768f2650fe3d086f65f091a5f4.tar.gz
tour-ba9abad7375bb3768f2650fe3d086f65f091a5f4.zip
Fix race condition in webworker logic (hopefully)
-rw-r--r--static/index.js16
-rw-r--r--static/worker.js2
2 files changed, 13 insertions, 5 deletions
diff --git a/static/index.js b/static/index.js
index 3f495af..aa51d3b 100644
--- a/static/index.js
+++ b/static/index.js
@@ -64,7 +64,7 @@ function debounce(fn, delay) {
};
}
-let workerWorking = false;
+let workerWorking = true;
let queuedWork = undefined;
const worker = new Worker("/worker.js", { type: "module" });
@@ -73,16 +73,17 @@ function sendToWorker(code) {
queuedWork = code;
return;
}
+ workerWorking = true;
worker.postMessage(code);
}
worker.onmessage = (event) => {
// Handle the result of the compilation and execution
const result = event.data;
- clearOutput();
+ if (!result.initialReadyMessage) clearOutput();
if (result.log) appendOutput(result.log, "log");
if (result.error) appendOutput(result.error, "error");
- for (const warning of result.warnings) {
+ for (const warning of result.warnings ?? []) {
appendOutput(warning, "warning");
}
@@ -92,5 +93,10 @@ worker.onmessage = (event) => {
queuedWork = undefined;
};
-editor.onUpdate(debounce((code) => worker.postMessage(code), 200));
-worker.postMessage(initialCode);
+editor.onUpdate(debounce((code) => sendToWorker(code), 200));
+
+// This line doesn't seem to be needed,
+// because updating the editor to the initial code (line 57)
+// triggers the onUpdate callback above (line 96) on my machine.
+// But you might uncomment it anyway to be extra safe:
+// sendToWorker(initialCode);
diff --git a/static/worker.js b/static/worker.js
index 9b975db..c437532 100644
--- a/static/worker.js
+++ b/static/worker.js
@@ -65,3 +65,5 @@ self.onmessage = async (event) => {
const result = compileEval(event.data);
postMessage(await result);
};
+
+postMessage({initialReadyMessage: true});