aboutsummaryrefslogtreecommitdiff
path: root/static
diff options
context:
space:
mode:
authorLouis Pilfold <louis@lpil.uk>2024-01-19 11:49:25 +0000
committerLouis Pilfold <louis@lpil.uk>2024-01-19 11:49:25 +0000
commit0f3f934a1eee62d77bd9917b9cc7becd2dbe989a (patch)
treed374e85f909d53155301f5d58de53ffd333e8cce /static
parentde91b7cd1b3266ae9a1b4d6fe1999d3b8a7c70e5 (diff)
downloadtour-0f3f934a1eee62d77bd9917b9cc7becd2dbe989a.tar.gz
tour-0f3f934a1eee62d77bd9917b9cc7becd2dbe989a.zip
Avoid flooding the worker
Diffstat (limited to 'static')
-rw-r--r--static/index.js16
1 files changed, 16 insertions, 0 deletions
diff --git a/static/index.js b/static/index.js
index 53c20e6..600b638 100644
--- a/static/index.js
+++ b/static/index.js
@@ -64,9 +64,20 @@ function debounce(fn, delay) {
};
}
+let workerWorking = false;
+let queuedWork = undefined;
const worker = new Worker("worker.js", { type: "module" });
+function sendToWorker(code) {
+ if (workerWorking) {
+ queuedWork = code;
+ return;
+ }
+ worker.postMessage(code);
+}
+
worker.onmessage = (event) => {
+ // Handle the result of the compilation and execution
const result = event.data;
clearOutput();
if (result.log) appendOutput(result.log, "log");
@@ -74,6 +85,11 @@ worker.onmessage = (event) => {
for (const warning of result.warnings) {
appendOutput(warning, "warning");
}
+
+ // Deal with any queued work
+ workerWorking = false;
+ if (queuedWork) sendToWorker(queuedWork);
+ queuedWork = undefined;
};
editor.onUpdate(debounce((code) => worker.postMessage(code), 200));