{"size": 2282, "ext": "js", "max_stars_repo_path": "node_modules/bottleneck/lib/Sync.js", "max_stars_repo_name": "emre6943/MoneyBoi", "max_stars_repo_stars_event_min_datetime": "2015-01-11T19:42:37.000Z", "max_stars_repo_stars_event_max_datetime": "2022-03-24T15:53:48.000Z", "max_issues_repo_path": "node_modules/bottleneck/lib/Sync.js", "max_issues_repo_name": "emre6943/MoneyBoi", "max_issues_repo_issues_event_min_datetime": "2020-04-29T15:05:08.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-31T10:13:21.000Z", "max_forks_repo_path": "node_modules/bottleneck/lib/Sync.js", "max_forks_repo_name": "emre6943/MoneyBoi", "max_forks_repo_forks_event_min_datetime": "2020-04-29T09:39:13.000Z", "max_forks_repo_forks_event_max_datetime": "2022-03-28T16:12:12.000Z"}
{"max_stars_count": 1266, "max_issues_count": 525, "max_forks_count": 178, "avg_line_length": 28.525, "max_line_length": 389, "alphanum_fraction": 0.583260298}
"use strict";

function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }

function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }

var DLList, Sync;
DLList = require("./DLList");
Sync = class Sync {
  constructor(name, Promise) {
    this.schedule = this.schedule.bind(this);
    this.name = name;
    this.Promise = Promise;
    this._running = 0;
    this._queue = new DLList();
  }

  isEmpty() {
    return this._queue.length === 0;
  }

  _tryToRun() {
    var _this = this;

    return _asyncToGenerator(function* () {
      var args, cb, error, reject, resolve, returned, task;

      if (_this._running < 1 && _this._queue.length > 0) {
        _this._running++;

        var _this$_queue$shift = _this._queue.shift();

        task = _this$_queue$shift.task;
        args = _this$_queue$shift.args;
        resolve = _this$_queue$shift.resolve;
        reject = _this$_queue$shift.reject;
        cb = yield _asyncToGenerator(function* () {
          try {
            returned = yield task(...args);
            return function () {
              return resolve(returned);
            };
          } catch (error1) {
            error = error1;
            return function () {
              return reject(error);
            };
          }
        })();
        _this._running--;

        _this._tryToRun();

        return cb();
      }
    })();
  }

  schedule(task, ...args) {
    var promise, reject, resolve;
    resolve = reject = null;
    promise = new this.Promise(function (_resolve, _reject) {
      resolve = _resolve;
      return reject = _reject;
    });

    this._queue.push({
      task,
      args,
      resolve,
      reject
    });

    this._tryToRun();

    return promise;
  }

};
module.exports = Sync;