even better sandbox

This commit is contained in:
りき萌 2024-02-18 12:10:02 +01:00
parent 0580db6c68
commit 668e9a050e
10 changed files with 169 additions and 19 deletions

View file

@ -20,6 +20,7 @@
<script type="module">
import { evaluate } from "treehouse/components/literate-programming/eval.js";
import { internals as sandboxInternals } from "treehouse/sandbox.js";
// I'm aware there's also ResizeObserver but it didn't seem to fire off any events when a
// canvas was added, rendering it pretty much useless.
@ -35,11 +36,28 @@
addEventListener("message", async event => {
let message = event.data;
if (message.action == "eval") {
document.body.replaceChildren();
evaluate(message.input);
evaluate(message.input, {
success() {
// A double buffered approach for flickerless code modifications.
document.body.replaceChildren(...sandboxInternals.body.childNodes);
sandboxInternals.body.replaceChildren();
requestAnimationFrame(() => {
postMessage({
kind: "resize",
width: document.body.scrollWidth,
height: document.body.scrollHeight,
});
});
},
error() {
sandboxInternals.body.replaceChildren();
},
});
}
});
postMessage({ kind: "ready" });
</script>
</head>