work around worker stack issues in safari
This commit is contained in:
18
playground/samples/worker.html
Normal file
18
playground/samples/worker.html
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
|
||||||
|
<script src="worker.js"></script>
|
||||||
|
<script>
|
||||||
|
sendResponse = (msg) => {
|
||||||
|
console.log('RETURN', msg)
|
||||||
|
window.parent.postMessage(msg, '*')
|
||||||
|
}
|
||||||
|
console.log('WORKER INITIALIZED')
|
||||||
|
</script>
|
||||||
|
<script src="newt.js"></script>
|
||||||
|
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
@@ -10,13 +10,26 @@ monaco.languages.setMonarchTokensProvider("newt", newtTokens);
|
|||||||
monaco.languages.setLanguageConfiguration("newt", newtConfig);
|
monaco.languages.setLanguageConfiguration("newt", newtConfig);
|
||||||
|
|
||||||
const newtWorker = new Worker("worker.js");
|
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");
|
const iframe = document.createElement("iframe");
|
||||||
iframe.src = "frame.html";
|
iframe.src = "frame.html";
|
||||||
iframe.style.display = "none";
|
iframe.style.display = "none";
|
||||||
document.body.appendChild(iframe);
|
document.body.appendChild(iframe);
|
||||||
|
|
||||||
function run(src: string) {
|
function run(src: string) {
|
||||||
newtWorker.postMessage({ src });
|
console.log('SEND TO', iframe.contentWindow)
|
||||||
|
postMessage({src})
|
||||||
}
|
}
|
||||||
|
|
||||||
function runOutput() {
|
function runOutput() {
|
||||||
@@ -35,6 +48,11 @@ window.onmessage = (ev) => {
|
|||||||
if (ev.data.message) {
|
if (ev.data.message) {
|
||||||
state.messages.value = [...state.messages.value, 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) => {
|
newtWorker.onmessage = (ev) => {
|
||||||
|
|||||||
@@ -123,7 +123,8 @@ const preload = [
|
|||||||
"aoc2023/day1/eg.txt",
|
"aoc2023/day1/eg.txt",
|
||||||
"aoc2023/day1/eg2.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) {
|
for (let fn of preload) {
|
||||||
|
|
||||||
if (!files[fn]) {
|
if (!files[fn]) {
|
||||||
@@ -156,6 +157,9 @@ onmessage = async function (e) {
|
|||||||
console.log(`process ${fn} in ${duration} ms`)
|
console.log(`process ${fn} in ${duration} ms`)
|
||||||
let javascript = files['out.js']
|
let javascript = files['out.js']
|
||||||
let output = stdout
|
let output = stdout
|
||||||
postMessage({javascript, output, duration})
|
sendResponse({javascript, output, duration})
|
||||||
}
|
}
|
||||||
|
let sendResponse = postMessage
|
||||||
|
onmessage = handleMessage
|
||||||
|
|
||||||
importScripts('newt.js')
|
importScripts('newt.js')
|
||||||
|
|||||||
Reference in New Issue
Block a user