Skip to content

Commit 0090371

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

File tree

3 files changed

+85
-1
lines changed

3 files changed

+85
-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").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

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

0 commit comments

Comments
 (0)