!7
html {
if (eq(uri, "/worker.js"))
ret [[
postMessage("WORKING!");
var startTime = new Date();
var i = 0;
while (true) {
i = i+1;
if ((i % 1000000) == 0) {
var ms = new Date()-startTime;
var mips = (i/ms/1000).toFixed(2);
postMessage("Counted to: " + i + " in " + ms + " ms ("
+ mips + " million steps/s)");
}
}
]];
ret htitle("Web Worker Test")
+ hbody(
p(hfulltag("output", "", id := "result1"))
+ p(hfulltag("output", "", id := "result2"))
+ p(hbutton("Restart Worker", onClick := "restart()"))
+ hjavascript([[
var workers = [];
function restart() {
for (w of workers) { w.terminate(); }
workers = [];
go();
}
function go() {
for (i = 1; i <= $WORKERS; i++) go1(i);
}
function go1(i) {
var res = document.getElementById("result" + i);
if (typeof(Worker) === "undefined") {
res.innerHTML = "Sorry, your browser does not support Web Workers...";
return;
}
res.innerHTML = "Making web worker";
var worker = new Worker($LINK);
worker.onmessage = function(event) {
res.innerHTML = event.data;
};
workers.push(worker);
}
]]).replace("$LINK", jsQuote(relativeRawBotLink(programID(), "worker.js"))), onLoad := "go()")
.replace("$WORKERS", str(2));
}