import axios from "axios";

let accesstoken
onmessage = (event) => {
    const { action, access_token } = event.data
    accesstoken = access_token

    if (action === "start") {
        fetchData()
    } else if (action === "terminate") {
    }
}

console.log('starting get loop');

const fetchData = () => {
    console.log(accesstoken);


    const apiURL = "http://localhost:5000/interstellar_ai/api/ai_get?access_token=" + accesstoken

    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" })
            setTimeout(() => fetchData(), 1000)
        })
}