interstellar_ai/app/backend/threads/GetWorker.js

34 lines
845 B
JavaScript
Raw Normal View History

2024-09-23 11:11:45 +02:00
import axios from "axios";
let shouldRun = false
onmessage = (event) => {
2024-09-23 14:54:13 +02:00
const { action, access_token } = event.data
if (action === "start") {
2024-09-23 11:11:45 +02:00
shouldRun = true
2024-09-23 14:54:13 +02:00
fetchData(access_token)
} else if (action === "terminate") {
2024-09-23 11:11:45 +02:00
shouldRun = false
}
}
2024-09-23 14:54:13 +02:00
console.log('starting get loop');
const fetchData = (access_token) => {
2024-09-23 11:11:45 +02:00
if (!shouldRun) return
2024-09-23 14:54:13 +02:00
const apiURL = "http://localhost:5000/interstellar/api/ai_get?access_token="+access_token
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-23 14:54:13 +02:00
setTimeout(fetchData,100)
2024-09-23 11:11:45 +02:00
})
.catch(error => {
console.log('Error fetching data:', error);
postMessage({error:"failed fetching data"})
})
}