From ebaee19bcb939a78f5ab3edddfdc1ac4ad01d22c Mon Sep 17 00:00:00 2001 From: Steve Dunham Date: Mon, 2 Dec 2024 21:03:18 -0800 Subject: [PATCH] work around worker stack issues in safari --- playground/samples/worker.html | 18 ++++++++++++++++++ playground/src/main.ts | 20 +++++++++++++++++++- playground/src/worker.ts | 8 ++++++-- 3 files changed, 43 insertions(+), 3 deletions(-) create mode 100644 playground/samples/worker.html diff --git a/playground/samples/worker.html b/playground/samples/worker.html new file mode 100644 index 0000000..fd39e83 --- /dev/null +++ b/playground/samples/worker.html @@ -0,0 +1,18 @@ + + + + + + + + + + + + diff --git a/playground/src/main.ts b/playground/src/main.ts index 527a048..131fcbb 100644 --- a/playground/src/main.ts +++ b/playground/src/main.ts @@ -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) => { diff --git a/playground/src/worker.ts b/playground/src/worker.ts index fe8d855..42ef66a 100644 --- a/playground/src/worker.ts +++ b/playground/src/worker.ts @@ -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')