'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

var _events = require('events');

var _events2 = _interopRequireDefault(_events);

var _lodash = require('lodash.throttle');

var _lodash2 = _interopRequireDefault(_lodash);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }

var nextFrame = typeof window !== 'undefined' ? window.requestAnimationFrame.bind(window) : process.nextTick.bind(process);

var _class = function (_EventEmitter) {
  _inherits(_class, _EventEmitter);

  function _class() {
    var wait = arguments.length <= 0 || arguments[0] === undefined ? 0 : arguments[0];

    _classCallCheck(this, _class);

    var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(_class).call(this));

    _this.queue = [];
    _this.willRun = false;

    _this.run = _this.run.bind(_this);

    if (wait > 0) {
      _this.enqueue = (0, _lodash2.default)(_this.run, wait, {
        leading: false,
        trailing: true
      });
    } else {
      _this.enqueue = function () {
        return nextFrame(_this.run);
      };
    }
    return _this;
  }

  _createClass(_class, [{
    key: 'push',
    value: function push(f) {
      var queue = this.queue;


      queue.push(f);

      if (!this.willRun) {
        this.willRun = true;
        this.enqueue();
      }
    }
  }, {
    key: 'run',
    value: function run() {
      if (!this.willRun) {
        return;
      }

      var queue = this.queue;

      var startTime = Date.now();

      this.willRun = false;

      this.emit('start', queue.length);

      var i = 0;
      while (i < queue.length) {
        queue[i]();
        i++;

        // Yield control after 30ms
        if (Date.now() - startTime > 30) {
          this.willRun = true;
          this.enqueue();
          break;
        }
      }

      this.emit('finish', i);

      queue.splice(0, i);
    }
  }]);

  return _class;
}(_events2.default);

exports.default = _class;