From ba9abad7375bb3768f2650fe3d086f65f091a5f4 Mon Sep 17 00:00:00 2001 From: Daniel Sherlock Date: Mon, 22 Jan 2024 15:45:22 +0000 Subject: Fix race condition in webworker logic (hopefully) --- static/index.js | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) (limited to 'static/index.js') 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); -- cgit v1.2.3