Comparing version 0.0.2 to 0.0.3
@@ -9,10 +9,14 @@ /* | ||
function isDef(value) { | ||
return value !== undefined && value !== null; | ||
} | ||
function ExponentialBackoff(options) { | ||
events.EventEmitter.call(this); | ||
var options = options || {}; | ||
options = options || {}; | ||
if (options.initialTimeout != undefined && options.initialTimeout < 1) { | ||
if (isDef(options.initialTimeout) && options.initialTimeout < 1) { | ||
throw new Error('The initial timeout must be greater than 0.'); | ||
} else if (options.maxTimeout != undefined && options.maxTimeout < 1) { | ||
} else if (isDef(options.maxTimeout) && options.maxTimeout < 1) { | ||
throw new Error('The maximal timeout must be greater than 0.'); | ||
@@ -37,8 +41,10 @@ } | ||
}; | ||
}; | ||
} | ||
util.inherits(ExponentialBackoff, events.EventEmitter); | ||
ExponentialBackoff.prototype.EXPONENTIAL_FACTOR = 2; | ||
ExponentialBackoff.prototype.updateBackoffDelay = function() { | ||
if (this.backoffDelay < this.maxTimeout) { | ||
var multiplicativeFactor = Math.pow(2, this.backoffNumber); | ||
var multiplicativeFactor = Math.pow(this.EXPONENTIAL_FACTOR, this.backoffNumber); | ||
var delay = Math.min(this.initialTimeout * multiplicativeFactor, this.maxTimeout); | ||
@@ -45,0 +51,0 @@ this.backoffDelay = Math.round(delay); |
{ | ||
"name": "backoff", | ||
"description": "Exponential backoff implementation.", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"author": "Mathieu Turcotte <turcotte.mat@gmail.com>", | ||
@@ -13,5 +13,7 @@ "keywords": ["backoff", "exponential"], | ||
"sinon": "1.3", | ||
"nodeunit": "0.7" | ||
"nodeunit": "0.7", | ||
"jshint": "0.7" | ||
}, | ||
"scripts": { | ||
"pretest": "node_modules/jshint/bin/hint *.js tests/*.js", | ||
"test": "node_modules/nodeunit/bin/nodeunit tests/" | ||
@@ -18,0 +20,0 @@ }, |
@@ -1,2 +0,2 @@ | ||
# Exponential backoff implementation for Node.js | ||
# Exponential backoff implementation for Node.js [![Build Status](https://secure.travis-ci.org/MathieuTurcotte/node-backoff.png?branch=master)](http://travis-ci.org/MathieuTurcotte/node-backoff) | ||
@@ -3,0 +3,0 @@ An exponential backoff implementation for Node.js. |
@@ -6,11 +6,7 @@ /* | ||
var events = require('events'), | ||
util = require('util'); | ||
var sinon = require('sinon'); | ||
var testCase = require('nodeunit').testCase, | ||
sinon = require('sinon'); | ||
var Backoff = require('../backoff'); | ||
exports["Backoff"] = testCase({ | ||
exports["Backoff"] = { | ||
setUp: function(callback) { | ||
@@ -28,3 +24,3 @@ this.clock = sinon.useFakeTimers(); | ||
var backoff = new Backoff({ | ||
initialTimeout: 10, | ||
initialTimeout: 10 | ||
}); | ||
@@ -183,2 +179,2 @@ var spy = new sinon.spy(); | ||
} | ||
}); | ||
}; |
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
16125
10
230
3