Skip to content

Commit

Permalink
Simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
lpil committed Jan 23, 2024
1 parent ba9abad commit 2e5b4cb
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
14 changes: 6 additions & 8 deletions static/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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" });
Expand All @@ -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");
}

Expand All @@ -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);
4 changes: 3 additions & 1 deletion static/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({});

0 comments on commit 2e5b4cb

Please sign in to comment.