diff options
Diffstat (limited to 'deps/npm/node_modules/retry/lib/retry.js')
-rw-r--r-- | deps/npm/node_modules/retry/lib/retry.js | 52 |
1 files changed, 48 insertions, 4 deletions
diff --git a/deps/npm/node_modules/retry/lib/retry.js b/deps/npm/node_modules/retry/lib/retry.js index 38406860df..94685652c4 100644 --- a/deps/npm/node_modules/retry/lib/retry.js +++ b/deps/npm/node_modules/retry/lib/retry.js @@ -1,8 +1,10 @@ var RetryOperation = require('./retry_operation'); exports.operation = function(options) { + var retryForever = false; + if (options && options.forever === true) retryForever = true; var timeouts = exports.timeouts(options); - return new RetryOperation(timeouts); + return new RetryOperation(timeouts, retryForever); }; exports.timeouts = function(options) { @@ -27,7 +29,7 @@ exports.timeouts = function(options) { var timeouts = []; for (var i = 0; i < opts.retries; i++) { - timeouts.push(this._createTimeout(i, opts)); + timeouts.push(this.createTimeout(i, opts)); } // sort the array numerically ascending @@ -38,7 +40,7 @@ exports.timeouts = function(options) { return timeouts; }; -exports._createTimeout = function(attempt, opts) { +exports.createTimeout = function(attempt, opts) { var random = (opts.randomize) ? (Math.random() + 1) : 1; @@ -47,4 +49,46 @@ exports._createTimeout = function(attempt, opts) { timeout = Math.min(timeout, opts.maxTimeout); return timeout; -};
\ No newline at end of file +}; + +exports.wrap = function(obj, options, methods) { + if (options instanceof Array) { + methods = options; + options = null; + } + + if (!methods) { + methods = []; + for (var key in obj) { + if (typeof obj[key] === 'function') { + methods.push(key); + } + } + } + + for (var i = 0; i < methods.length; i++) { + var method = methods[i]; + var original = obj[method]; + + obj[method] = function retryWrapper() { + var op = exports.operation(options); + var args = Array.prototype.slice.call(arguments); + var callback = args.pop(); + + args.push(function(err) { + if (op.retry(err)) { + return; + } + if (err) { + arguments[0] = op.mainError(); + } + callback.apply(this, arguments); + }); + + op.attempt(function() { + original.apply(obj, args); + }); + }; + obj[method].options = options; + } +}; |