"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; }; })();

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

var DefaultScheduler = (function () {
  function DefaultScheduler() {
    _classCallCheck(this, DefaultScheduler);

    this.updateRequests = [];
    this.updateRequested = false;
    this.performUpdates = this.performUpdates.bind(this);
  }

  _createClass(DefaultScheduler, [{
    key: "updateDocument",
    value: function updateDocument(fn) {
      this.updateRequests.push(fn);
      if (!this.updateRequested) {
        this.updateRequested = true;
        window.requestAnimationFrame(this.performUpdates);
      }
    }
  }, {
    key: "updateDocumentSync",
    value: function updateDocumentSync(fn) {
      this.updateRequests.push(fn);
      if (!this.updateRequested) {
        this.performUpdates();
      }
    }
  }, {
    key: "getNextUpdatePromise",
    value: function getNextUpdatePromise() {
      var _this = this;

      if (!this.nextUpdatePromise) {
        this.nextUpdatePromise = new Promise(function (resolve) {
          _this.resolveNextUpdatePromise = resolve;
        });
      }
      return this.nextUpdatePromise;
    }
  }, {
    key: "performUpdates",
    value: function performUpdates() {
      while (this.updateRequests.length > 0) {
        this.updateRequests.shift()();
      }
      this.updateRequested = false;
      if (this.nextUpdatePromise) {
        var resolveNextUpdatePromise = this.resolveNextUpdatePromise;
        this.nextUpdatePromise = null;
        this.resolveNextUpdatePromise = null;
        resolveNextUpdatePromise();
      }
    }
  }]);

  return DefaultScheduler;
})();

exports["default"] = DefaultScheduler;
module.exports = exports["default"];