Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add service worker for offline compatibility #115

Merged
merged 14 commits into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions src/simulator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
68 changes: 68 additions & 0 deletions src/sw.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
function initSimulatorServiceWorker() {
const simUrls = ["simulator.html", "build/simulator.js", "build/firmware.js"];
let didInstall = false;
const cacheName = "simulator";
microbit-robert marked this conversation as resolved.
Show resolved Hide resolved

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.
microbit-robert marked this conversation as resolved.
Show resolved Hide resolved
})
.then(function () {
if (didInstall) {
// Only notify clients for the first activation
didInstall = false;
// Necessary?
return notifyAllClientsAsync();
microbit-robert marked this conversation as resolved.
Show resolved Hide resolved
}
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();
Loading