2023-10-29 00:07:24 +00:00
|
|
|
const SW = (function () {
|
|
|
|
// Helper Function
|
|
|
|
function randomID() {
|
|
|
|
return "00000000000000000000000000000000".replace(/[018]/g, (c) =>
|
|
|
|
(c ^ (crypto.getRandomValues(new Uint8Array(1))[0] & (15 >> (c / 4))))
|
|
|
|
.toString(16)
|
|
|
|
.toUpperCase()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Variables
|
|
|
|
let swInstance = null;
|
|
|
|
let outstandingMessages = {};
|
|
|
|
|
2023-10-30 22:25:43 +00:00
|
|
|
navigator.serviceWorker?.addEventListener("message", ({ data }) => {
|
2023-10-29 00:07:24 +00:00
|
|
|
let { id } = data;
|
|
|
|
data = data.data;
|
|
|
|
|
|
|
|
console.log("[SW] Received Message:", { id, data });
|
|
|
|
if (!outstandingMessages[id])
|
|
|
|
return console.warn("[SW] Invalid Outstanding Message:", { id, data });
|
|
|
|
|
|
|
|
outstandingMessages[id](data);
|
|
|
|
delete outstandingMessages[id];
|
|
|
|
});
|
|
|
|
|
|
|
|
async function install() {
|
2023-10-30 22:25:43 +00:00
|
|
|
if (!navigator.serviceWorker)
|
|
|
|
throw new Error("Service Worker Not Supported");
|
|
|
|
|
2023-10-29 00:07:24 +00:00
|
|
|
// Register Service Worker
|
|
|
|
swInstance = await navigator.serviceWorker.register("/sw.js");
|
|
|
|
swInstance.onupdatefound = (data) =>
|
|
|
|
console.log("[SW.install] Update Found:", data);
|
|
|
|
|
|
|
|
// Wait for Registration / Update
|
|
|
|
let serviceWorker =
|
|
|
|
swInstance.installing || swInstance.waiting || swInstance.active;
|
|
|
|
|
|
|
|
// Await Installation
|
|
|
|
await new Promise((resolve) => {
|
|
|
|
serviceWorker.onstatechange = (data) => {
|
|
|
|
console.log("[SW.install] State Change:", serviceWorker.state);
|
2023-10-30 22:25:43 +00:00
|
|
|
if (["installed", "activated"].includes(serviceWorker.state)) resolve();
|
2023-10-29 00:07:24 +00:00
|
|
|
};
|
2023-10-30 22:25:43 +00:00
|
|
|
|
|
|
|
console.log("[SW.install] Current State:", serviceWorker.state);
|
|
|
|
if (["installed", "activated"].includes(serviceWorker.state)) resolve();
|
2023-10-29 00:07:24 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function send(data) {
|
|
|
|
if (!swInstance?.active) return Promise.reject("Inactive Service Worker");
|
|
|
|
let id = randomID();
|
|
|
|
|
|
|
|
let msgPromise = new Promise((resolve) => {
|
|
|
|
outstandingMessages[id] = resolve;
|
|
|
|
});
|
|
|
|
|
|
|
|
swInstance.active.postMessage({ id, data });
|
|
|
|
return msgPromise;
|
|
|
|
}
|
|
|
|
|
|
|
|
return { install, send };
|
|
|
|
})();
|