diff --git a/app/backend/threads/GetWorker.ts b/app/backend/threads/GetWorker.ts index 2813a93..ae1cec3 100644 --- a/app/backend/threads/GetWorker.ts +++ b/app/backend/threads/GetWorker.ts @@ -1,37 +1,32 @@ import axios from "axios"; -let windownameGlobal = "" +let windownameGlobal = ""; // Global variable to hold the window name +let accesstoken = ""; // Variable to store the access token -let accesstoken = "" onmessage = (event) => { - const { action, access_token, windowname } = event.data - accesstoken = access_token - - windownameGlobal = windowname + const { action, access_token, windowname } = event.data; + accesstoken = access_token; + windownameGlobal = windowname; if (action === "start") { - fetchData() - } else if (action === "terminate") { + fetchData(); // Start fetching data on 'start' action } } - const fetchData = () => { + const apiURL = new URL("http://localhost:5000/interstellar_ai/api/ai_get?access_token=" + accesstoken); + apiURL.hostname = windownameGlobal; // Set the hostname - const apiURL = new URL("http://localhost:5000/interstellar_ai/api/ai_get?access_token=" + accesstoken) - apiURL.hostname = windownameGlobal; - - console.log(apiURL.href) + console.log(apiURL.href); // Log the constructed URL axios.get(apiURL.href) .then(response => { - const data = response.data - postMessage(data) - setTimeout(fetchData, 100) + postMessage(response.data); // Send data back on success + setTimeout(fetchData, 100); // Schedule next fetch }) .catch(error => { console.log('Error fetching data:', error); - postMessage({ error: "failed fetching data" }) - setTimeout(() => fetchData(), 1000) - }) -} \ No newline at end of file + postMessage({ error: "failed fetching data" }); // Send error message + setTimeout(() => fetchData(), 1000); // Retry after 1 second + }); +}