Skip to content

Commit aebead6

Browse files
Add first draft of service worker
1 parent 6204a0b commit aebead6

File tree

3 files changed

+89
-1
lines changed

3 files changed

+89
-1
lines changed

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ build:
88

99
dist: build
1010
mkdir -p $(BUILD)/build
11-
cp -r $(SRC)/*.html $(SRC)/term.js src/examples $(BUILD)
11+
cp -r $(SRC)/*.html $(SRC)/term.js src/examples $(SRC)/sw.js $(BUILD)
1212
cp $(SRC)/build/firmware.js $(SRC)/build/simulator.js $(SRC)/build/firmware.wasm $(BUILD)/build/
1313

1414
watch: dist

src/simulator.ts

+16
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,22 @@ declare global {
1515
}
1616
}
1717

18+
function initServiceWorker() {
19+
if ("serviceWorker" in navigator) {
20+
window.addEventListener("load", () => {
21+
navigator.serviceWorker.register("/sw.js", { scope: "/" }).then(
22+
function (registration) {
23+
console.log("Simulator ServiceWorker registration successful");
24+
},
25+
function (err) {
26+
console.log("Simulator ServiceWorker registration failed: ", err);
27+
}
28+
);
29+
});
30+
}
31+
}
32+
33+
initServiceWorker();
1834
const fs = new FileSystem();
1935
const board = createBoard(new Notifications(window.parent), fs);
2036
window.addEventListener("message", createMessageListener(board));

src/sw.js

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
function initSimulatorServiceWorker() {
2+
const simUrls = [
3+
"/simulator.html",
4+
"/build/simulator.js",
5+
"/build/firmware.js",
6+
];
7+
let didInstall = false;
8+
const cacheName = "simulator";
9+
10+
self.addEventListener("install", function (ev) {
11+
didInstall = true;
12+
console.log("Installing service worker...");
13+
ev.waitUntil(
14+
caches
15+
.open(cacheName)
16+
.then(function (cache) {
17+
console.log("Opened cache");
18+
return cache.addAll(simUrls);
19+
})
20+
.then(function () {
21+
return self.skipWaiting();
22+
})
23+
);
24+
});
25+
26+
self.addEventListener("activate", function (ev) {
27+
console.log("Activating service worker...");
28+
ev.waitUntil(
29+
caches
30+
.keys()
31+
.then(function (_cacheNames) {
32+
// Delete old versions in cache here.
33+
})
34+
.then(function () {
35+
if (didInstall) {
36+
// Only notify clients for the first activation
37+
didInstall = false;
38+
// Necessary?
39+
return notifyAllClientsAsync();
40+
}
41+
return Promise.resolve();
42+
})
43+
);
44+
});
45+
46+
self.addEventListener("fetch", function (ev) {
47+
ev.respondWith(
48+
caches.match(ev.request).then(function (response) {
49+
return response || fetch(ev.request);
50+
})
51+
);
52+
});
53+
54+
function notifyAllClientsAsync() {
55+
var scope = self;
56+
return scope.clients
57+
.claim()
58+
.then(function () {
59+
return scope.clients.matchAll();
60+
})
61+
.then(function (clients) {
62+
clients.forEach(function (client) {
63+
return client.postMessage({
64+
type: "serviceworker",
65+
state: "activated",
66+
});
67+
});
68+
});
69+
}
70+
}
71+
72+
initSimulatorServiceWorker();

0 commit comments

Comments
 (0)