"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault")["default"];

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;

var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));

var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));

var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));

var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));

var TaskRunner = /*#__PURE__*/function () {
  function TaskRunner(fn) {
    (0, _classCallCheck2["default"])(this, TaskRunner);
    this.callbacks = [];
    this.isRunning = false;

    if (fn) {
      this.fn = fn;
    }
  }

  (0, _createClass2["default"])(TaskRunner, [{
    key: "delay",
    value: function () {
      var _delay2 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee(_delay, fn, callback) {
        return _regenerator["default"].wrap(function _callee$(_context) {
          while (1) {
            switch (_context.prev = _context.next) {
              case 0:
                return _context.abrupt("return", this.start(true, _delay, fn, callback));

              case 1:
              case "end":
                return _context.stop();
            }
          }
        }, _callee, this);
      }));

      function delay(_x, _x2, _x3) {
        return _delay2.apply(this, arguments);
      }

      return delay;
    }()
  }, {
    key: "run",
    value: function () {
      var _run = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee2(interval, fn, callback) {
        return _regenerator["default"].wrap(function _callee2$(_context2) {
          while (1) {
            switch (_context2.prev = _context2.next) {
              case 0:
                return _context2.abrupt("return", this.start(false, interval, fn, callback));

              case 1:
              case "end":
                return _context2.stop();
            }
          }
        }, _callee2, this);
      }));

      function run(_x4, _x5, _x6) {
        return _run.apply(this, arguments);
      }

      return run;
    }()
  }, {
    key: "start",
    value: function () {
      var _start = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee3(once, interval, fn, callback) {
        var _this = this;

        return _regenerator["default"].wrap(function _callee3$(_context3) {
          while (1) {
            switch (_context3.prev = _context3.next) {
              case 0:
                this.cancel();

                if (fn) {
                  this.fn = fn;
                }

                if (typeof callback === 'function') {
                  this.callbacks.push(callback);
                }

                if (!this.fn) {
                  _context3.next = 6;
                  break;
                }

                this.isRunning = true;
                return _context3.abrupt("return", new Promise(function (resolve) {
                  _this.id = setInterval(function () {
                    if (once) {
                      _this.cancel();
                    }

                    resolve(_this.fn());

                    _this.callbacks.forEach(function (cb) {
                      return cb();
                    });

                    _this.callbacks = [];
                  }, interval);
                }));

              case 6:
                return _context3.abrupt("return", Promise.reject(new Error('no caller')));

              case 7:
              case "end":
                return _context3.stop();
            }
          }
        }, _callee3, this);
      }));

      function start(_x7, _x8, _x9, _x10) {
        return _start.apply(this, arguments);
      }

      return start;
    }()
  }, {
    key: "cancel",
    value: function cancel() {
      this.isRunning = false;

      if (this.id) {
        clearInterval(this.id);
        delete this.id;
      }

      return this;
    }
  }]);
  return TaskRunner;
}();

exports["default"] = TaskRunner;
//# sourceMappingURL=TaskRunner.js.map
