Comparing version 2.0.0 to 2.1.0
@@ -45,4 +45,6 @@ /* | ||
* Starts a backoff operation. | ||
* @param err Optional paramater to let listeners know why the backoff | ||
* operation what started. | ||
*/ | ||
Backoff.prototype.backoff = function() { | ||
Backoff.prototype.backoff = function(err) { | ||
if (this.timeoutID_ !== -1) { | ||
@@ -53,3 +55,3 @@ throw new Error('Backoff in progress.'); | ||
if (this.backoffNumber_ === this.maxNumberOfRetry_) { | ||
this.emit('fail'); | ||
this.emit('fail', err); | ||
this.reset(); | ||
@@ -59,3 +61,3 @@ } else { | ||
this.timeoutID_ = setTimeout(this.handlers.backoff, this.backoffDelay_); | ||
this.emit('backoff', this.backoffNumber_, this.backoffDelay_); | ||
this.emit('backoff', this.backoffNumber_, this.backoffDelay_, err); | ||
} | ||
@@ -62,0 +64,0 @@ }; |
@@ -74,2 +74,3 @@ /* | ||
* @param strategy The backoff strategy to use. | ||
* @return Itself for chaining. | ||
*/ | ||
@@ -81,2 +82,3 @@ FunctionCall.prototype.setStrategy = function(strategy) { | ||
this.strategy_ = strategy; | ||
return this; | ||
}; | ||
@@ -96,2 +98,3 @@ | ||
* @param maxNumberOfRetry The maximum number of backoffs. | ||
* @return Itself for chaining. | ||
*/ | ||
@@ -103,2 +106,3 @@ FunctionCall.prototype.failAfter = function(maxNumberOfRetry) { | ||
this.failAfter_ = maxNumberOfRetry; | ||
return this; | ||
}; | ||
@@ -178,3 +182,3 @@ | ||
if (args[0]) { | ||
this.backoff_.backoff(); | ||
this.backoff_.backoff(args[0]); | ||
} else { | ||
@@ -189,8 +193,9 @@ this.doCallback_(); | ||
* @param delay Backoff delay. | ||
* @param err The error that caused the backoff. | ||
* @private | ||
*/ | ||
FunctionCall.prototype.handleBackoff_ = function(number, delay) { | ||
this.emit('backoff', number, delay); | ||
FunctionCall.prototype.handleBackoff_ = function(number, delay, err) { | ||
this.emit('backoff', number, delay, err); | ||
}; | ||
module.exports = FunctionCall; |
{ | ||
"name": "backoff", | ||
"description": "Fibonacci and exponential backoffs.", | ||
"version": "2.0.0", | ||
"version": "2.1.0", | ||
"author": "Mathieu Turcotte <turcotte.mat@gmail.com>", | ||
@@ -14,6 +14,6 @@ "keywords": ["backoff", "retry", "fibonacci", "exponential"], | ||
"nodeunit": "0.7", | ||
"jshint": "0.9.0" | ||
"jshint": "1.1.0" | ||
}, | ||
"scripts": { | ||
"pretest": "node_modules/jshint/bin/hint lib/ lib/ tests/ examples/ index.js", | ||
"pretest": "node_modules/jshint/bin/jshint lib/ lib/ tests/ examples/ index.js", | ||
"test": "node_modules/nodeunit/bin/nodeunit tests/" | ||
@@ -20,0 +20,0 @@ }, |
@@ -159,7 +159,10 @@ # Backoff for Node.js [![Build Status](https://secure.travis-ci.org/MathieuTurcotte/node-backoff.png?branch=master)](http://travis-ci.org/MathieuTurcotte/node-backoff) | ||
#### backoff.backoff() | ||
#### backoff.backoff([err]) | ||
Starts a backoff operation. Will throw an error if a backoff operation is | ||
already in progress. | ||
Starts a backoff operation. If provided, the error parameter will be emitted | ||
as the last argument of the `backoff` and `fail` events to let the listeners | ||
know why the backoff operation was attempted. | ||
An error will be thrown an error if a backoff operation is already in progress. | ||
In practice, this method should be called after a failed attempt to perform a | ||
@@ -183,2 +186,3 @@ sensitive operation (connecting to a database, downloading a resource over the | ||
- delay: backoff delay in milliseconds | ||
- err: optional error parameter passed to `backoff.backoff([err])` | ||
@@ -198,2 +202,4 @@ Emitted when a backoff operation is started. Signals to the client how long | ||
- err: optional error parameter passed to `backoff.backoff([err])` | ||
Emitted when the maximum number of backoffs is reached. This event will only | ||
@@ -315,2 +321,3 @@ be emitted if the client has set a limit on the number of backoffs by calling | ||
- delay: backoff delay in milliseconds | ||
- err: the error that triggered the backoff operation | ||
@@ -317,0 +324,0 @@ Emitted each time a backoff operation is started. |
@@ -72,2 +72,4 @@ /* | ||
"the fail event should be emitted when backoff limit is reached": function(test) { | ||
var err = new Error('Fail'); | ||
this.backoffStrategy.next.returns(10); | ||
@@ -86,4 +88,5 @@ this.backoff.on('fail', this.spy); | ||
test.ok(!this.spy.calledOnce, 'Fail event shouldn\'t have been emitted.'); | ||
this.backoff.backoff(); | ||
this.backoff.backoff(err); | ||
test.ok(this.spy.calledOnce, 'Fail event should have been emitted.'); | ||
test.equal(this.spy.getCall(0).args[0], err, 'Error should be passed'); | ||
@@ -90,0 +93,0 @@ test.done(); |
@@ -6,2 +6,3 @@ /* | ||
var assert = require('assert'); | ||
var events = require('events'); | ||
@@ -246,16 +247,20 @@ var sinon = require('sinon'); | ||
"backoff event should be emitted on backoff start": function(test) { | ||
var err = new Error('backoff event error'); | ||
var call = new FunctionCall(this.wrappedFn, [1, 'two'], this.callback); | ||
var backoffSpy = sinon.spy(); | ||
call.on('backoff', backoffSpy); | ||
this.wrappedFn.yields(new Error()); | ||
this.wrappedFn.yields(err); | ||
call.start(this.backoffFactory); | ||
this.backoff.emit('backoff', 3, 1234); | ||
this.backoff.emit('backoff', 3, 1234, err); | ||
test.ok(this.backoff.backoff.calledWith(err), | ||
'The backoff instance should have been called with the error.'); | ||
test.equal(1, backoffSpy.callCount, | ||
'Backoff event should have been emitted 1 time.'); | ||
test.deepEqual([3, 1234], backoffSpy.firstCall.args, | ||
'Backoff event should carry current backoff number and delay.'); | ||
test.deepEqual([3, 1234, err], backoffSpy.firstCall.args, | ||
'Backoff event should carry the backoff number, delay and error.'); | ||
test.done(); | ||
} | ||
}; |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
97727
31
1165
325