Skip to content

Commit

Permalink
Avoid flooding the worker
Browse files Browse the repository at this point in the history
  • Loading branch information
lpil committed Jan 19, 2024
1 parent de91b7c commit 0f3f934
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions static/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,32 @@ 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");
if (result.error) appendOutput(result.error, "error");
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));
Expand Down

0 comments on commit 0f3f934

Please sign in to comment.