2015-03-06 13:27:24 +00:00
|
|
|
// Any copyright is dedicated to the Public Domain.
|
|
|
|
// http://creativecommons.org/publicdomain/zero/1.0/
|
|
|
|
//
|
|
|
|
// ServiceWorker equivalent of worker_wrapper.js.
|
|
|
|
|
|
|
|
var client;
|
|
|
|
|
|
|
|
function ok(a, msg) {
|
|
|
|
dump("OK: " + !!a + " => " + a + ": " + msg + "\n");
|
|
|
|
client.postMessage({ type: "status", status: !!a, msg: a + ": " + msg });
|
|
|
|
}
|
|
|
|
|
|
|
|
function is(a, b, msg) {
|
|
|
|
dump("IS: " + (a === b) + " => " + a + " | " + b + ": " + msg + "\n");
|
|
|
|
client.postMessage({
|
|
|
|
type: "status",
|
|
|
|
status: a === b,
|
|
|
|
msg: a + " === " + b + ": " + msg,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function workerTestArrayEquals(a, b) {
|
|
|
|
if (!Array.isArray(a) || !Array.isArray(b) || a.length != b.length) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
for (var i = 0, n = a.length; i < n; ++i) {
|
|
|
|
if (a[i] !== b[i]) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
function workerTestDone() {
|
|
|
|
client.postMessage({ type: "finish" });
|
|
|
|
}
|
|
|
|
|
2018-12-10 22:19:26 +00:00
|
|
|
function workerTestGetHelperData(cb) {
|
|
|
|
addEventListener("message", function workerTestGetHelperDataCB(e) {
|
|
|
|
if (e.data.type !== "returnHelperData") {
|
2015-03-06 13:27:24 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-12-10 22:19:26 +00:00
|
|
|
removeEventListener("message", workerTestGetHelperDataCB);
|
2015-03-06 13:27:24 +00:00
|
|
|
cb(e.data.result);
|
|
|
|
});
|
|
|
|
client.postMessage({
|
2018-12-10 22:19:26 +00:00
|
|
|
type: "getHelperData",
|
2015-03-06 13:27:24 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-06-14 12:57:36 +00:00
|
|
|
function workerTestGetStorageManager(cb) {
|
|
|
|
addEventListener("message", function workerTestGetStorageManagerCB(e) {
|
|
|
|
if (e.data.type !== "returnStorageManager") {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
removeEventListener("message", workerTestGetStorageManagerCB);
|
|
|
|
cb(e.data.result);
|
|
|
|
});
|
|
|
|
client.postMessage({
|
|
|
|
type: "getStorageManager",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-06-05 17:49:12 +00:00
|
|
|
var completeInstall;
|
|
|
|
|
2015-03-06 13:27:24 +00:00
|
|
|
addEventListener("message", function workerWrapperOnMessage(e) {
|
|
|
|
removeEventListener("message", workerWrapperOnMessage);
|
|
|
|
var data = e.data;
|
2018-06-05 17:49:12 +00:00
|
|
|
self.clients.matchAll({ includeUncontrolled: true }).then(function(clients) {
|
|
|
|
for (var i = 0; i < clients.length; ++i) {
|
|
|
|
if (clients[i].url.includes("message_receiver.html")) {
|
|
|
|
client = clients[i];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-03-06 13:27:24 +00:00
|
|
|
try {
|
|
|
|
importScripts(data.script);
|
2019-06-14 18:04:01 +00:00
|
|
|
} catch (ex) {
|
2015-03-06 13:27:24 +00:00
|
|
|
client.postMessage({
|
|
|
|
type: "status",
|
|
|
|
status: false,
|
2019-06-14 18:04:01 +00:00
|
|
|
msg:
|
|
|
|
"worker failed to import " + data.script + "; error: " + ex.message,
|
2015-03-06 13:27:24 +00:00
|
|
|
});
|
|
|
|
}
|
2018-06-05 17:49:12 +00:00
|
|
|
completeInstall();
|
2015-03-06 13:27:24 +00:00
|
|
|
});
|
|
|
|
});
|
2018-06-05 17:49:12 +00:00
|
|
|
|
|
|
|
addEventListener("install", e => {
|
|
|
|
e.waitUntil(new Promise(resolve => (completeInstall = resolve)));
|
|
|
|
});
|