let fetchType;
let fetchInterval;
let fetchURL;


onmessage = function(e) {
    let msgType = e.data.msgType;

    // console.log("Configuration Message:");
    console.log(e.data);

    fetchType = e.data.fetchType;
    fetchURL = e.data.fetchURL;
    fetchInterval = 1000 * e.data.fetchInterval;
};


function doFetch() {
    let now = new Date();

    if (fetchURL) {
        fetch(fetchURL).then(
            b => b.json()).then(
                j => postMessage({msgType: fetchType,
                                  json: j,
                                  timestamp: now}))
            .catch(e => console.log(e.message));
    }
}


// Periodic resource fetching
(function periodicFetch() {
    doFetch();
    setTimeout(periodicFetch, fetchInterval);
})();
