import { getEmailFetchAction } from './../actions/EmailActions'

let timeoutID;
self.onmessage = (event) => {
  if (event.data.command === 'fetchEmail') {
    // Get the parameter
    const param = event.data.param // { opi_sn: userId }
    console.log('Worker: Received command `fetchEmail` with param:', param)

    // Do something......
    const fetchEmails = () => {
      try {
        getEmailFetchAction(param);
      } catch (error) {
        console.error(error)
      }
      timeoutID = setTimeout(fetchEmails, 60 * 1000);
    };

    // Start fetching emails
    fetchEmails();
  }
  if (event.data.command === 'logout') {
    console.log('logout')
    // clearInterval(intervalID)
    clearTimeout(timeoutID)
  }
}
self.onerror = function (error) {
  console.error('Error in worker', error)
  // You can also post the error back to the main thread
  postMessage({ error: error.message })
}
