// https://github.com/turuslan/HackTimer/blob/master/HackTimerWorker.js
var timeouts = {}
var intervals = {}

onmessage = function (a) {
  const { name, id, time } = a.data
  switch (name) {
    case 'setInterval':
      intervals[id] = setInterval(() => {
        postMessage({ id })
      }, time)
      break
    case 'clearInterval':
      clearInterval(intervals[id])
      delete intervals[id]
      break
    case 'setTimeout':
      timeouts[id] = setTimeout(() => {
        delete timeouts[id]
        postMessage({ id })
      }, time)
      break
    case 'clearTimeout':
      clearTimeout(timeouts[id])
      delete timeouts[id]
      break
  }
}
