"Fossies" - the Fresh Open Source Software Archive

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