diff options
-rw-r--r-- | static/index.js | 14 | ||||
-rw-r--r-- | static/worker.js | 4 |
2 files changed, 9 insertions, 9 deletions
diff --git a/static/index.js b/static/index.js index aa51d3b..825a4b0 100644 --- a/static/index.js +++ b/static/index.js @@ -64,6 +64,10 @@ function debounce(fn, delay) { }; } +// Whether the worker is currently working or not, used to avoid sending +// multiple messages to the worker at once. +// This will be true when the worker is compiling and executing the code, but +// this first time it is as the worker is initialising. let workerWorking = true; let queuedWork = undefined; const worker = new Worker("/worker.js", { type: "module" }); @@ -80,10 +84,10 @@ function sendToWorker(code) { worker.onmessage = (event) => { // Handle the result of the compilation and execution const result = event.data; - if (!result.initialReadyMessage) clearOutput(); + 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"); } @@ -94,9 +98,3 @@ worker.onmessage = (event) => { }; 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 c437532..a3c4a51 100644 --- a/static/worker.js +++ b/static/worker.js @@ -66,4 +66,6 @@ self.onmessage = async (event) => { postMessage(await result); }; -postMessage({initialReadyMessage: true}); +// Send an initial message to the main thread to indicate that the worker is +// ready to receive messages. +postMessage({}); |