interstellar_ai/app/backend/threads/GetWorker.js

34 lines
842 B
JavaScript
Raw Normal View History

2024-09-23 11:11:45 +02:00
import axios from "axios";
2024-09-23 16:34:55 +02:00
let accesstoken
2024-09-23 11:11:45 +02:00
onmessage = (event) => {
2024-09-23 14:54:13 +02:00
const { action, access_token } = event.data
2024-09-24 16:42:36 +02:00
accesstoken = access_token
2024-09-23 14:54:13 +02:00
if (action === "start") {
2024-09-23 16:34:55 +02:00
fetchData()
2024-09-23 14:54:13 +02:00
} else if (action === "terminate") {
2024-09-23 11:11:45 +02:00
}
}
2024-09-23 14:54:13 +02:00
console.log('starting get loop');
2024-09-23 16:34:55 +02:00
const fetchData = () => {
console.log(accesstoken);
2024-09-24 16:42:36 +02:00
const apiURL = "http://localhost:5000/interstellar_ai/api/ai_get?access_token=" + accesstoken
2024-09-23 11:11:45 +02:00
axios.get(apiURL)
.then(response => {
2024-09-23 14:54:13 +02:00
const data = response.data
console.log(data);
2024-09-23 11:11:45 +02:00
postMessage(data)
2024-09-24 16:42:36 +02:00
setTimeout(fetchData, 100)
2024-09-23 11:11:45 +02:00
})
.catch(error => {
console.log('Error fetching data:', error);
2024-09-24 16:42:36 +02:00
postMessage({ error: "failed fetching data" })
setTimeout(() => fetchData(), 1000)
})
2024-09-23 11:11:45 +02:00
}