diff options
author | Louis Pilfold <louis@lpil.uk> | 2024-01-23 14:07:27 +0000 |
---|---|---|
committer | Louis Pilfold <louis@lpil.uk> | 2024-01-23 14:07:27 +0000 |
commit | 2e5b4cb878569e56811f4f58facbe7aa60ab7327 (patch) | |
tree | 46e234d14f8092d09e8222f05ec9fdff7e6b62ea /static | |
parent | ba9abad7375bb3768f2650fe3d086f65f091a5f4 (diff) | |
download | tour-2e5b4cb878569e56811f4f58facbe7aa60ab7327.tar.gz tour-2e5b4cb878569e56811f4f58facbe7aa60ab7327.zip |
Simplify
Diffstat (limited to 'static')
-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({}); |