From 0090371cdcdac0e5600415c181131ec2f5c5e217 Mon Sep 17 00:00:00 2001 From: Robert Knight Date: Tue, 23 Apr 2024 16:36:24 +0100 Subject: [PATCH] Add first draft of service worker --- Makefile | 2 +- src/simulator.ts | 16 ++++++++++++ src/sw.js | 68 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 85 insertions(+), 1 deletion(-) create mode 100644 src/sw.js diff --git a/Makefile b/Makefile index 60df0f9f..e054de9f 100644 --- a/Makefile +++ b/Makefile @@ -8,7 +8,7 @@ build: dist: build mkdir -p $(BUILD)/build - cp -r $(SRC)/*.html $(SRC)/term.js src/examples $(BUILD) + cp -r $(SRC)/*.html $(SRC)/term.js src/examples $(SRC)/sw.js $(BUILD) cp $(SRC)/build/firmware.js $(SRC)/build/simulator.js $(SRC)/build/firmware.wasm $(BUILD)/build/ watch: dist diff --git a/src/simulator.ts b/src/simulator.ts index c45f60e3..73db57a1 100644 --- a/src/simulator.ts +++ b/src/simulator.ts @@ -15,6 +15,22 @@ declare global { } } +function initServiceWorker() { + if ("serviceWorker" in navigator) { + window.addEventListener("load", () => { + navigator.serviceWorker.register("sw.js").then( + function (_registration) { + console.log("Simulator ServiceWorker registration successful"); + }, + function (err) { + console.log("Simulator ServiceWorker registration failed: ", err); + } + ); + }); + } +} + +initServiceWorker(); const fs = new FileSystem(); const board = createBoard(new Notifications(window.parent), fs); window.addEventListener("message", createMessageListener(board)); diff --git a/src/sw.js b/src/sw.js new file mode 100644 index 00000000..c9ccea53 --- /dev/null +++ b/src/sw.js @@ -0,0 +1,68 @@ +function initSimulatorServiceWorker() { + const simUrls = ["simulator.html", "build/simulator.js", "build/firmware.js"]; + let didInstall = false; + const cacheName = "simulator"; + + self.addEventListener("install", function (ev) { + didInstall = true; + console.log("Installing service worker..."); + ev.waitUntil( + caches + .open(cacheName) + .then(function (cache) { + console.log("Opened cache"); + return cache.addAll(simUrls); + }) + .then(function () { + return self.skipWaiting(); + }) + ); + }); + + self.addEventListener("activate", function (ev) { + console.log("Activating service worker..."); + ev.waitUntil( + caches + .keys() + .then(function (_cacheNames) { + // Delete old versions in cache here. + }) + .then(function () { + if (didInstall) { + // Only notify clients for the first activation + didInstall = false; + // Necessary? + return notifyAllClientsAsync(); + } + return Promise.resolve(); + }) + ); + }); + + self.addEventListener("fetch", function (ev) { + ev.respondWith( + caches.match(ev.request).then(function (response) { + return response || fetch(ev.request); + }) + ); + }); + + function notifyAllClientsAsync() { + var scope = self; + return scope.clients + .claim() + .then(function () { + return scope.clients.matchAll(); + }) + .then(function (clients) { + clients.forEach(function (client) { + return client.postMessage({ + type: "serviceworker", + state: "activated", + }); + }); + }); + } +} + +initSimulatorServiceWorker();