Skip to content

Commit

Permalink
chore: Format web workers dir (#288)
Browse files Browse the repository at this point in the history
  • Loading branch information
bsmth authored Jan 2, 2025
1 parent 5970eb3 commit 085b595
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 22 deletions.
8 changes: 4 additions & 4 deletions web-workers/simple-shared-worker/multiply.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ const result1 = document.querySelector(".result1");
if (!!window.SharedWorker) {
const myWorker = new SharedWorker("worker.js");

[first, second].forEach(input => {
[first, second].forEach((input) => {
input.onchange = () => {
myWorker.port.postMessage([first.value, second.value]);
console.log('Message posted to worker');
}
})
console.log("Message posted to worker");
};
});

myWorker.port.onmessage = (e) => {
result1.textContent = e.data;
Expand Down
24 changes: 12 additions & 12 deletions web-workers/simple-web-worker/main.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
const first = document.querySelector('#number1');
const second = document.querySelector('#number2');
const first = document.querySelector("#number1");
const second = document.querySelector("#number2");

const result = document.querySelector('.result');
const result = document.querySelector(".result");

if (window.Worker) {
const myWorker = new Worker("worker.js");

[first, second].forEach(input => {
input.onchange = function() {
[first, second].forEach((input) => {
input.onchange = () => {
myWorker.postMessage([first.value, second.value]);
console.log('Message posted to worker');
}
})
console.log("Message posted to worker");
};
});

myWorker.onmessage = function(e) {
myWorker.onmessage = (e) => {
result.textContent = e.data;
console.log('Message received from worker');
}
console.log("Message received from worker");
};
} else {
console.log('Your browser doesn\'t support web workers.');
console.log("Your browser doesn't support web workers.");
}
14 changes: 8 additions & 6 deletions web-workers/simple-web-worker/worker.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
onmessage = function(e) {
console.log('Worker: Message received from main script');
onmessage = (e) => {
console.log("Worker: Message received from main script");

const result = e.data[0] * e.data[1];

if (isNaN(result)) {
postMessage('Please write two numbers');
postMessage("Please write two numbers");
} else {
const workerResult = 'Result: ' + result;
console.log('Worker: Posting message back to main script');
const workerResult = "Result: " + result;
console.log("Worker: Posting message back to main script");
postMessage(workerResult);
}
}
};

0 comments on commit 085b595

Please sign in to comment.