interstellar_ai/app/backend/threads/GetWorker.js
2024-09-23 14:54:13 +02:00

34 lines
No EOL
845 B
JavaScript

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