Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
16 changes: 16 additions & 0 deletions lesson-06/service-wookiee/main.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
const template = document.querySelector("#template").content;
const boxes = document.querySelector("#boxes");
const count = document.querySelector("#request-count");
let counts = 0;

if ("serviceWorker" in navigator) {
navigator.serviceWorker.register("sw.js");
}

navigator.serviceWorker.addEventListener("message", (e) => {
console.log(e.data);
counter();
});

function counter() {
counts++;
count.innerHTML = counts;
}

for (const fileType of ["text/css", "text/html", "application/json"]) {
// Clone the template for each box
Expand Down
12 changes: 12 additions & 0 deletions lesson-06/service-wookiee/sw.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
self.addEventListener("install", async (e) => {
console.log("installed");
});

self.addEventListener("fetch", async (e) => {
console.log("fetched " + e.clientId);
console.log(await self.clients.get(e.clientId));
let client = await clients.get(e.clientId);
if (client) {
client.postMessage("test");
}
});
49 changes: 16 additions & 33 deletions lesson-06/worker-example/index.html
Original file line number Diff line number Diff line change
@@ -1,37 +1,20 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style.css">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="style.css" />
<title>Worker example</title>
</head>

<body class="bg-gray-50 p-4">
<button id="worker" class="p-4 bg-red-500 rounded text-white">Do hard work</button>
<button id="clicky" class="p-4 bg-blue-500 rounded text-white">See if you can click me</button>
<script>
const hardWorkButton = document.querySelector("#worker")
const clickButton = document.querySelector("#clicky");

function doHardWork() {
let start = Date.now();
console.log("Blocking...");
while (Date.now() < start + 3000) { }
console.log("Unblocked!")
}

let clickCount = 0;
function handleButtonClick(event) {
clickCount++
event.target.innerText = `Clicked ${clickCount} times`;
}

clickButton.addEventListener("click", handleButtonClick);
hardWorkButton.addEventListener("click", doHardWork);
</script>
</body>
</head>

</html>
<body class="bg-gray-50 p-4">
<button id="worker" class="p-4 bg-red-500 rounded text-white">
Do hard work
</button>
<button id="clicky" class="p-4 bg-blue-500 rounded text-white">
See if you can click me
</button>
<script src="script.js"></script>
</body>
</html>
18 changes: 18 additions & 0 deletions lesson-06/worker-example/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
let myWorker = new Worker("worker.js");

const hardWorkButton = document.querySelector("#worker");
const clickButton = document.querySelector("#clicky");

let clickCount = 0;
function handleButtonClick(event) {
clickCount++;
event.target.innerText = `Clicked ${clickCount} times`;
}

function doHardWork() {
myWorker.postMessage("test");
console.log("Message posted to worker");
}

clickButton.addEventListener("click", handleButtonClick);
hardWorkButton.addEventListener("click", doHardWork);
7 changes: 7 additions & 0 deletions lesson-06/worker-example/worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
self.addEventListener("message", (e) => {
console.log(e.data);
let start = Date.now();
console.log("Blocking...");
while (Date.now() < start + 3000) {}
console.log("Unblocked!");
});