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
26 changes: 26 additions & 0 deletions lesson-06/service-wookiee/main.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,29 @@
const template = document.querySelector("#template").content;
const boxes = document.querySelector("#boxes");
const counter = document.querySelector("#request-count");

if ("serviceWorker" in navigator) {
navigator.serviceWorker.register("./sw.js").then(
function (registration) {
// Registration was successful
console.log(
"ServiceWorker registration successful with scope: ",
registration.scope
);
},
function (err) {
// registration failed :(
console.log("ServiceWorker registration failed: ", err);
}
);
}

let clickCount = 0;
navigator.serviceWorker.addEventListener("message", (e) => {
clickCount++;
counter.innerText = clickCount;
console.log(clickCount);
});

for (const fileType of ["text/css", "text/html", "application/json"]) {
// Clone the template for each box
Expand Down Expand Up @@ -46,3 +70,5 @@ for (const fileType of ["text/css", "text/html", "application/json"]) {
});
boxes.appendChild(box);
}

console.log(self);
13 changes: 13 additions & 0 deletions lesson-06/service-wookiee/sw.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
let userClient;

self.addEventListener("fetch", async (e) => {
try {
userClient = await self.clients.get(e.clientId);
if (userClient) {
userClient.postMessage("message");
console.log("inside worker if", userClient);
}
} catch (error) {
console.log("SW error: " + error);
}
});
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="main.js"></script>
</body>
</html>
16 changes: 16 additions & 0 deletions lesson-06/worker-example/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
var 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`;
}

clickButton.addEventListener("click", handleButtonClick);
hardWorkButton.addEventListener("click", () => {
myWorker.postMessage("message");
});
8 changes: 8 additions & 0 deletions lesson-06/worker-example/worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
self.addEventListener("message", doHardWork);

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