"Fossies" - the Fresh Open Source Software Archive

Member "Atom/resources/app/apm/node_modules/npm-registry-client/node_modules/retry/lib/retry_operation.js" (11 Apr 2017, 2630 Bytes) of package /windows/misc/atom-windows.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Javascript source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 function RetryOperation(timeouts, retryForever) {
    2   this._timeouts = timeouts;
    3   this._fn = null;
    4   this._errors = [];
    5   this._attempts = 1;
    6   this._operationTimeout = null;
    7   this._operationTimeoutCb = null;
    8   this._timeout = null;
    9 
   10   if (!!retryForever) {
   11     this._cachedTimeouts = this._timeouts.slice(0);
   12   }
   13 }
   14 module.exports = RetryOperation;
   15 
   16 RetryOperation.prototype.retry = function(err) {
   17   if (this._timeout) {
   18     clearTimeout(this._timeout);
   19   }
   20 
   21   if (!err) {
   22     return false;
   23   }
   24 
   25   this._errors.push(err);
   26 
   27   var timeout = this._timeouts.shift();
   28   if (timeout === undefined) {
   29     if (this._cachedTimeouts) {
   30       // retry forever, only keep last error
   31       this._errors.splice(this._errors.length - 1, this._errors.length);
   32       this._timeouts = this._cachedTimeouts.slice(0);
   33       timeout = this._timeouts.shift();
   34     } else {
   35       return false;
   36     }
   37   }
   38 
   39   var self = this;
   40   setTimeout(function() {
   41     self._attempts++;
   42 
   43     if (self._operationTimeoutCb) {
   44       self._timeout = setTimeout(function() {
   45         self._operationTimeoutCb(self._attempts);
   46       }, self._operationTimeout);
   47     }
   48 
   49     self._fn(self._attempts);
   50   }, timeout);
   51 
   52   return true;
   53 };
   54 
   55 RetryOperation.prototype.attempt = function(fn, timeoutOps) {
   56   this._fn = fn;
   57 
   58   if (timeoutOps) {
   59     if (timeoutOps.timeout) {
   60       this._operationTimeout = timeoutOps.timeout;
   61     }
   62     if (timeoutOps.cb) {
   63       this._operationTimeoutCb = timeoutOps.cb;
   64     }
   65   }
   66 
   67   var self = this;
   68   if (this._operationTimeoutCb) {
   69     this._timeout = setTimeout(function() {
   70       self._operationTimeoutCb();
   71     }, self._operationTimeout);
   72   }
   73 
   74   this._fn(this._attempts);
   75 };
   76 
   77 RetryOperation.prototype.try = function(fn) {
   78   console.log('Using RetryOperation.try() is deprecated');
   79   this.attempt(fn);
   80 };
   81 
   82 RetryOperation.prototype.start = function(fn) {
   83   console.log('Using RetryOperation.start() is deprecated');
   84   this.attempt(fn);
   85 };
   86 
   87 RetryOperation.prototype.start = RetryOperation.prototype.try;
   88 
   89 RetryOperation.prototype.errors = function() {
   90   return this._errors;
   91 };
   92 
   93 RetryOperation.prototype.attempts = function() {
   94   return this._attempts;
   95 };
   96 
   97 RetryOperation.prototype.mainError = function() {
   98   if (this._errors.length === 0) {
   99     return null;
  100   }
  101 
  102   var counts = {};
  103   var mainError = null;
  104   var mainErrorCount = 0;
  105 
  106   for (var i = 0; i < this._errors.length; i++) {
  107     var error = this._errors[i];
  108     var message = error.message;
  109     var count = (counts[message] || 0) + 1;
  110 
  111     counts[message] = count;
  112 
  113     if (count >= mainErrorCount) {
  114       mainError = error;
  115       mainErrorCount = count;
  116     }
  117   }
  118 
  119   return mainError;
  120 };