function getCurrentTime(){
    return Intl.DateTimeFormat('it-IT', {hour: '2-digit', minute: '2-digit', second: '2-digit'}).format(new Date());
}

function mt(m){
    return `${getCurrentTime()} - [KeepAliveWorker]: ${m}`;
}

onmessage = function (ev) {
    const { type, data } = ev.data;

    if (type === 'START_KEEP_ALIVE') {
        const { url, minutes } = data;
        
        async function fetchKeepAlive() {
            console.log(mt(`calling ${url}`));
            
            fetch(url).then(
                (response) => response.json().then(
                    (json) => postMessage({
                        type: 'KEEP_ALIVE_RESULT',
                        data: json
                    })
                )
            );
        }
        
        console.log(mt('starting keep alive'), data);
        fetchKeepAlive();
        setInterval(fetchKeepAlive, minutes * 60000);
    } else {
        postMessage({ type, data: 'OK' });
    }
}