interstellar_ai/app/backend/threads/GetWorker.ts
sageTheDM 91353bd051 main (#137)
Reviewed-on: https://interstellardevelopment.org/code/code/React-Group/interstellar_ai/pulls/137
Reviewed-by: Patrick <patrick_pluto@noreply.localhost>
Co-authored-by: sageTheDM <info@photofuel.tech>
Co-committed-by: sageTheDM <info@photofuel.tech>
2024-10-11 10:18:33 +02:00

32 lines
1.1 KiB
TypeScript

import axios from "axios";
let windownameGlobal = ""; // Global variable to hold the window name
let accesstoken = ""; // Variable to store the access token
onmessage = (event) => {
const { action, access_token, windowname } = event.data;
accesstoken = access_token;
windownameGlobal = windowname;
if (action === "start") {
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
console.log(apiURL.href); // Log the constructed URL
axios.get(apiURL.href)
.then(response => {
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" }); // Send error message
setTimeout(() => fetchData(), 1000); // Retry after 1 second
});
}