"Fossies" - the Fresh Open Source Software Archive

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