work around worker stack issues in safari

This commit is contained in:
2024-12-02 21:03:18 -08:00
parent 52bbb5aa65
commit ebaee19bcb
3 changed files with 43 additions and 3 deletions

View File

@@ -10,13 +10,26 @@ monaco.languages.setMonarchTokensProvider("newt", newtTokens);
monaco.languages.setLanguageConfiguration("newt", newtConfig);
const newtWorker = new Worker("worker.js");
let postMessage = (msg: any) => newtWorker.postMessage(msg)
// Safari/MobileSafari have small stacks in webworkers.
if (navigator.vendor.includes('Apple')) {
const workerFrame = document.createElement("iframe");
workerFrame.src = "worker.html"
workerFrame.style.display = "none"
document.body.appendChild(workerFrame)
postMessage = (msg: any) => workerFrame.contentWindow?.postMessage(msg, '*')
}
// iframe for running newt output
const iframe = document.createElement("iframe");
iframe.src = "frame.html";
iframe.style.display = "none";
document.body.appendChild(iframe);
function run(src: string) {
newtWorker.postMessage({ src });
console.log('SEND TO', iframe.contentWindow)
postMessage({src})
}
function runOutput() {
@@ -35,6 +48,11 @@ window.onmessage = (ev) => {
if (ev.data.message) {
state.messages.value = [...state.messages.value, ev.data.message]
}
// safari callback
if (ev.data.output !== undefined) {
state.output.value = ev.data.output;
state.javascript.value = ev.data.javascript;
}
};
newtWorker.onmessage = (ev) => {

View File

@@ -123,7 +123,8 @@ const preload = [
"aoc2023/day1/eg.txt",
"aoc2023/day1/eg2.txt",
]
onmessage = async function (e) {
const handleMessage = async function (e) {
console.log('message for you sir', e.data)
for (let fn of preload) {
if (!files[fn]) {
@@ -156,6 +157,9 @@ onmessage = async function (e) {
console.log(`process ${fn} in ${duration} ms`)
let javascript = files['out.js']
let output = stdout
postMessage({javascript, output, duration})
sendResponse({javascript, output, duration})
}
let sendResponse = postMessage
onmessage = handleMessage
importScripts('newt.js')