"Fossies" - the Fresh Open Source Software Archive

Member "Atom/resources/app/apm/node_modules/npm-registry-client/node_modules/retry/lib/retry.js" (8 Mar 2017, 2123 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 var RetryOperation = require('./retry_operation');
    2 
    3 exports.operation = function(options) {
    4   var retryForever = false;
    5   if (options && options.forever === true) retryForever = true;
    6   var timeouts = exports.timeouts(options);
    7   return new RetryOperation(timeouts, retryForever);
    8 };
    9 
   10 exports.timeouts = function(options) {
   11   if (options instanceof Array) {
   12     return [].concat(options);
   13   }
   14 
   15   var opts = {
   16     retries: 10,
   17     factor: 2,
   18     minTimeout: 1 * 1000,
   19     maxTimeout: Infinity,
   20     randomize: false
   21   };
   22   for (var key in options) {
   23     opts[key] = options[key];
   24   }
   25 
   26   if (opts.minTimeout > opts.maxTimeout) {
   27     throw new Error('minTimeout is greater than maxTimeout');
   28   }
   29 
   30   var timeouts = [];
   31   for (var i = 0; i < opts.retries; i++) {
   32     timeouts.push(this.createTimeout(i, opts));
   33   }
   34 
   35   // sort the array numerically ascending
   36   timeouts.sort(function(a,b) {
   37     return a - b;
   38   });
   39 
   40   return timeouts;
   41 };
   42 
   43 exports.createTimeout = function(attempt, opts) {
   44   var random = (opts.randomize)
   45     ? (Math.random() + 1)
   46     : 1;
   47 
   48   var timeout = Math.round(random * opts.minTimeout * Math.pow(opts.factor, attempt));
   49   timeout = Math.min(timeout, opts.maxTimeout);
   50 
   51   return timeout;
   52 };
   53 
   54 exports.wrap = function(obj, options, methods) {
   55   if (options instanceof Array) {
   56     methods = options;
   57     options = null;
   58   }
   59 
   60   if (!methods) {
   61     methods = [];
   62     for (var key in obj) {
   63       if (typeof obj[key] === 'function') {
   64         methods.push(key);
   65       }
   66     }
   67   }
   68 
   69   for (var i = 0; i < methods.length; i++) {
   70     var method   = methods[i];
   71     var original = obj[method];
   72 
   73     obj[method] = function retryWrapper() {
   74       var op       = exports.operation(options);
   75       var args     = Array.prototype.slice.call(arguments);
   76       var callback = args.pop();
   77 
   78       args.push(function(err) {
   79         if (op.retry(err)) {
   80           return;
   81         }
   82         if (err) {
   83           arguments[0] = op.mainError();
   84         }
   85         callback.apply(this, arguments);
   86       });
   87 
   88       op.attempt(function() {
   89         original.apply(obj, args);
   90       });
   91     };
   92     obj[method].options = options;
   93   }
   94 };