From 8a61cfeefd85e0a86bf1abcbfc6b26713ba9e299 Mon Sep 17 00:00:00 2001 From: Brian Smith Date: Tue, 31 Dec 2024 13:16:13 +0100 Subject: [PATCH] chore: Format web workers dir --- web-workers/simple-shared-worker/multiply.js | 8 +++---- web-workers/simple-web-worker/main.js | 24 ++++++++++---------- web-workers/simple-web-worker/worker.js | 14 +++++++----- 3 files changed, 24 insertions(+), 22 deletions(-) diff --git a/web-workers/simple-shared-worker/multiply.js b/web-workers/simple-shared-worker/multiply.js index b3612a34..0185040e 100644 --- a/web-workers/simple-shared-worker/multiply.js +++ b/web-workers/simple-shared-worker/multiply.js @@ -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; diff --git a/web-workers/simple-web-worker/main.js b/web-workers/simple-web-worker/main.js index 6056de81..2a56c56a 100644 --- a/web-workers/simple-web-worker/main.js +++ b/web-workers/simple-web-worker/main.js @@ -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."); } diff --git a/web-workers/simple-web-worker/worker.js b/web-workers/simple-web-worker/worker.js index 447ede5e..ce9fbdf4 100644 --- a/web-workers/simple-web-worker/worker.js +++ b/web-workers/simple-web-worker/worker.js @@ -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); } -} +};