{"size": 3288, "ext": "js", "max_stars_repo_path": "lib/utils.js", "max_stars_repo_name": "gtriggiano/esgrpctest", "max_stars_repo_stars_event_min_datetime": "2017-05-19T15:40:49.000Z", "max_stars_repo_stars_event_max_datetime": "2017-05-19T15:40:49.000Z", "max_issues_repo_path": "lib/utils.js", "max_issues_repo_name": "gtriggiano/esgrpctest", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "lib/utils.js", "max_forks_repo_name": "gtriggiano/esgrpctest", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 1, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 33.8969072165, "max_line_length": 226, "alphanum_fraction": 0.6964720195}
'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.eventsStreamFromBackendEmitter = exports.eventsStreamFromBus = exports.zeropad = exports.isPositiveInteger = exports.isValidHostname = exports.isValidString = exports.timeoutCallback = exports.prefixString = undefined;

var _lodash = require('lodash');

var _rxjs = require('rxjs');

var _rxjs2 = _interopRequireDefault(_rxjs);

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

var validHostnameRegex = /^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$/;

var prefixString = exports.prefixString = (0, _lodash.curry)(function (prefix, str) {
  return '' + prefix + str;
});

var timeoutCallback = exports.timeoutCallback = (0, _lodash.curry)(function (timeout, msg, cb) {
  var _called = false;
  var _invoke = function _invoke() {
    if (_called) return;
    _called = true;
    cb.apply(undefined, arguments);
  };
  setTimeout(function () {
    _invoke(new Error(msg));
  }, timeout);
  return _invoke;
});

var isValidString = exports.isValidString = function isValidString(str) {
  return (0, _lodash.isString)(str) && !!str.length;
};

var isValidHostname = exports.isValidHostname = function isValidHostname(str) {
  return (0, _lodash.isString)(str) && validHostnameRegex.test(str);
};

var isPositiveInteger = exports.isPositiveInteger = function isPositiveInteger(n) {
  return (0, _lodash.isInteger)(n) && n > 0;
};

var zeropad = exports.zeropad = function zeropad(i, minLength) {
  var str = String(i);
  var diff = minLength - str.length;
  if (diff > 0) {
    str = '' + (0, _lodash.range)(diff).map(function () {
      return 0;
    }).join('') + str;
  }
  return str;
};

var eventsStreamFromBus = exports.eventsStreamFromBus = function eventsStreamFromBus(bus) {
  var delayTime = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 100;

  var receivedEvents = {
    ids: [],
    byId: {}
  };

  // We create an hot observable through the publish().connect() sequence
  var stream = _rxjs2.default.Observable.fromEvent(bus, 'StoredEvents').map(function (msg) {
    return JSON.parse(msg);
  }).flatMap(function (events) {
    return _rxjs2.default.Observable.from(events);
  }).map(function (evt) {
    var evtId = zeropad(evt.id, 25);
    receivedEvents.ids.push(evtId);
    receivedEvents.ids.sort();
    receivedEvents.byId[evtId] = evt;
    return receivedEvents;
  }).delay(delayTime).map(function (evt) {
    var oldestEventId = receivedEvents.ids.shift();
    var oldestEvent = receivedEvents.byId[oldestEventId];
    delete receivedEvents.byId[oldestEventId];
    return oldestEvent;
  }).publish();

  stream.connect(); // TODO: should we take a reference to this subscription to end it later?

  return stream;
};

var eventsStreamFromBackendEmitter = exports.eventsStreamFromBackendEmitter = function eventsStreamFromBackendEmitter(e) {
  var evt = _rxjs2.default.Observable.fromEvent(e, 'event');
  var error = _rxjs2.default.Observable.fromEvent(e, 'error').flatMap(function (err) {
    return _rxjs2.default.Observable.throw(err);
  });
  var end = _rxjs2.default.Observable.fromEvent(e, 'end');

  return evt.merge(error).takeUntil(end);
};