nthmail / lib / scheduler.js
all-in-one smtp+imap with minimal setup
git clone http://git.nthia.dev/nthmail

module.exports = Scheduler

function Scheduler(opts) {
  if (!(this instanceof Scheduler)) return new Scheduler(opts)
  this._timeouts = []
}

Scheduler.prototype.push = function (ms, f) {
  let t = {
    ms,
    f: () => {
      let i = this._timeouts.indexOf(t)
      if (i >= 0) this._timeouts.splice(i,1)
      f()
    },
    target: new Date(new Date().valueOf() + ms),
    timeout: setTimeout(f, ms),
  }
  this._timeouts.push(t)
}

Scheduler.prototype.close = function () {
  for (let i = 0; i < this._timeouts.length; i++) {
    clearTimeout(this._timeouts[i].timeout)
  }
}