interstellar_ai/app/backend/threads/GetWorker.js
2024-09-23 11:11:45 +02:00

29 lines
No EOL
692 B
JavaScript

import axios from "axios";
let shouldRun = false
onmessage = (event) => {
if (event.data === "start") {
shouldRun = true
fetchData()
} else if (event.date === "terminate") {
shouldRun = false
}
}
const fetchData = () => {
if (!shouldRun) return
const apiURL = "http://localhost:5000/interstellar/api/ai_get"
axios.get(apiURL)
.then(response => {
const data = response.data.response
postMessage(data)
setTimeout(fetchData,500)
})
.catch(error => {
console.log('Error fetching data:', error);
postMessage({error:"failed fetching data"})
})
}