Comparing version 1.2.1 to 2.0.0
@@ -56,1 +56,2 @@ #!/usr/bin/env node | ||
call.failAfter(2); | ||
call.start(); |
30
index.js
@@ -6,6 +6,6 @@ /* | ||
var Backoff = require('./lib/backoff'), | ||
FunctionCall = require('./lib/function_call.js'), | ||
FibonacciBackoffStrategy = require('./lib/strategy/fibonacci'), | ||
ExponentialBackoffStrategy = require('./lib/strategy/exponential'); | ||
var Backoff = require('./lib/backoff'); | ||
var ExponentialBackoffStrategy = require('./lib/strategy/exponential'); | ||
var FibonacciBackoffStrategy = require('./lib/strategy/fibonacci'); | ||
var FunctionCall = require('./lib/function_call.js'); | ||
@@ -38,18 +38,14 @@ module.exports.Backoff = Backoff; | ||
/** | ||
* Calls a function in a backoff loop. | ||
* @param fn Function to wrap in a backoff handler. | ||
* @param vargs Function's arguments (var args). | ||
* @param callback Function's callback. | ||
* @return The call handle. | ||
* Constructs a FunctionCall for the given function and arguments. | ||
* @param fn The function to wrap in a backoff handler. | ||
* @param vargs The function's arguments (var args). | ||
* @param callback The function's callback. | ||
* @return The FunctionCall instance. | ||
*/ | ||
module.exports.call = function(fn, vargs, callback) { | ||
var args = Array.prototype.slice.call(arguments); | ||
var call = new FunctionCall(args[0], args.slice(1, args.length - 1), | ||
args[args.length - 1]); | ||
process.nextTick(function() { | ||
call.call(); | ||
}); | ||
return call; | ||
fn = args[0]; | ||
vargs = args.slice(1, args.length - 1); | ||
callback = args[args.length - 1]; | ||
return new FunctionCall(fn, vargs, callback); | ||
}; |
@@ -6,4 +6,4 @@ /* | ||
var events = require('events'), | ||
util = require('util'); | ||
var events = require('events'); | ||
var util = require('util'); | ||
@@ -10,0 +10,0 @@ /** |
@@ -6,7 +6,7 @@ /* | ||
var events = require('events'), | ||
util = require('util'); | ||
var events = require('events'); | ||
var util = require('util'); | ||
var Backoff = require('./backoff'), | ||
FibonacciBackoffStrategy = require('./strategy/fibonacci'); | ||
var Backoff = require('./backoff'); | ||
var FibonacciBackoffStrategy = require('./strategy/fibonacci'); | ||
@@ -118,3 +118,3 @@ /** | ||
*/ | ||
FunctionCall.prototype.call = function(backoffFactory) { | ||
FunctionCall.prototype.start = function(backoffFactory) { | ||
if (this.aborted_) { | ||
@@ -121,0 +121,0 @@ return; |
@@ -6,4 +6,4 @@ /* | ||
var events = require('events'), | ||
util = require('util'); | ||
var events = require('events'); | ||
var util = require('util'); | ||
@@ -10,0 +10,0 @@ function isDef(value) { |
{ | ||
"name": "backoff", | ||
"description": "Fibonacci and exponential backoffs.", | ||
"version": "1.2.1", | ||
"version": "2.0.0", | ||
"author": "Mathieu Turcotte <turcotte.mat@gmail.com>", | ||
@@ -12,3 +12,3 @@ "keywords": ["backoff", "retry", "fibonacci", "exponential"], | ||
"devDependencies": { | ||
"sinon": "1.4", | ||
"sinon": "1.5.2", | ||
"nodeunit": "0.7", | ||
@@ -15,0 +15,0 @@ "jshint": "0.9.0" |
@@ -102,2 +102,3 @@ # Backoff for Node.js [![Build Status](https://secure.travis-ci.org/MathieuTurcotte/node-backoff.png?branch=master)](http://travis-ci.org/MathieuTurcotte/node-backoff) | ||
call.failAfter(10); | ||
call.start(); | ||
``` | ||
@@ -129,13 +130,13 @@ | ||
- fn: function to call in a backoff handler | ||
- fn: function to call in a backoff handler, i.e. the wrapped function | ||
- args: function's arguments | ||
- callback: function's callback accepting an error as its first argument | ||
Calls an asynchronous function in a backoff handler so that it gets | ||
automatically retried on error. The wrapped function will get retried until it | ||
succeds or reaches the maximum number of backoffs. In both cases, the callback | ||
function will be invoked with the last result returned by the wrapped function. | ||
Constructs a `FunctionCall` instance for the given function. The wrapped | ||
function will get retried until it succeds or reaches the maximum number | ||
of backoffs. In both cases, the callback function will be invoked with the | ||
last result returned by the wrapped function. | ||
This function returns a `FunctionCall` instance that is going to be invoked on | ||
next tick and can be used to configure and/or abort the call. | ||
It is the caller's responsability to initiate the call by invoking the | ||
`start` method on the returned `FunctionCall` instance. | ||
@@ -284,5 +285,6 @@ ### Class Backoff | ||
#### call.call() | ||
#### call.start() | ||
Calls the wrapped function. This method should only be called once. | ||
Initiates the call the wrapped function. This method should only be called | ||
once per function call instance. | ||
@@ -289,0 +291,0 @@ #### call.abort() |
@@ -27,6 +27,10 @@ /* | ||
"backoff.call should be defined and a function": function(test) { | ||
"backoff.call should be a function that returns a FunctionCall instance": function(test) { | ||
var fn = function() {}; | ||
var callback = function() {}; | ||
test.ok(backoff.Backoff, 'backoff.call should be defined.'); | ||
test.equal(typeof backoff.call, 'function', | ||
'backoff.Backoff should be a function.'); | ||
'backoff.call should be a function.'); | ||
test.equal(backoff.call(fn, 1, 2, 3, callback).constructor.name, | ||
'FunctionCall'); | ||
test.done(); | ||
@@ -33,0 +37,0 @@ }, |
@@ -6,4 +6,4 @@ /* | ||
var sinon = require('sinon'), | ||
util = require('util'); | ||
var sinon = require('sinon'); | ||
var util = require('util'); | ||
@@ -10,0 +10,0 @@ var BackoffStrategy = require('../lib/strategy/strategy'); |
@@ -8,4 +8,4 @@ /* | ||
var Backoff = require('../lib/backoff'), | ||
BackoffStrategy = require('../lib/strategy/strategy'); | ||
var Backoff = require('../lib/backoff'); | ||
var BackoffStrategy = require('../lib/strategy/strategy'); | ||
@@ -12,0 +12,0 @@ exports["Backoff"] = { |
@@ -6,5 +6,5 @@ /* | ||
var events = require('events'), | ||
sinon = require('sinon'), | ||
util = require('util'); | ||
var events = require('events'); | ||
var sinon = require('sinon'); | ||
var util = require('util'); | ||
@@ -54,3 +54,3 @@ var FunctionCall = require('../lib/function_call'); | ||
call.setStrategy(replacementStrategy); | ||
call.call(this.backoffFactory); | ||
call.start(this.backoffFactory); | ||
test.ok(this.backoffFactory.calledWith(replacementStrategy), | ||
@@ -64,3 +64,3 @@ 'User defined strategy should be used to instantiate ' + | ||
var call = new FunctionCall(this.wrappedFn, [], this.callback); | ||
call.call(this.backoffFactory); | ||
call.start(this.backoffFactory); | ||
test.throws(function() { | ||
@@ -76,3 +76,3 @@ call.setStrategy({}); | ||
call.failAfter(failAfterValue); | ||
call.call(this.backoffFactory); | ||
call.start(this.backoffFactory); | ||
test.ok(this.backoff.failAfter.calledWith(failAfterValue), | ||
@@ -86,3 +86,3 @@ 'User defined maximum number of backoffs shoud be ' + | ||
var call = new FunctionCall(this.wrappedFn, [], this.callback); | ||
call.call(this.backoffFactory); | ||
call.start(this.backoffFactory); | ||
test.throws(function() { | ||
@@ -94,9 +94,9 @@ call.failAfter(1234); | ||
"call shouldn't allow overlapping invocation": function(test) { | ||
"start shouldn't allow overlapping invocation": function(test) { | ||
var call = new FunctionCall(this.wrappedFn, [], this.callback); | ||
var backoffFactory = this.backoffFactory; | ||
call.call(backoffFactory); | ||
call.start(backoffFactory); | ||
test.throws(function() { | ||
call.call(backoffFactory); | ||
call.start(backoffFactory); | ||
}, /in progress/); | ||
@@ -108,3 +108,3 @@ test.done(); | ||
var call = new FunctionCall(this.wrappedFn, [1, 2, 3], this.callback); | ||
call.call(this.backoffFactory); | ||
call.start(this.backoffFactory); | ||
test.ok(this.wrappedFn.calledWith(1, 2, 3)); | ||
@@ -114,8 +114,12 @@ test.done(); | ||
"call should complete when the wrapped function succeed": function(test) { | ||
"call should complete when the wrapped function succeeds": function(test) { | ||
var call = new FunctionCall(this.wrappedFn, [1, 2, 3], this.callback); | ||
this.wrappedFn.yields(new Error()); | ||
call.call(this.backoffFactory); | ||
this.wrappedFn.yields(new Error()) | ||
.yields(new Error()) | ||
.yields(new Error()) | ||
.yields(null, 'Success!'); | ||
for (var i = 0; i < 3; i++) { | ||
call.start(this.backoffFactory); | ||
for (var i = 0; i < 2; i++) { | ||
this.backoff.emit('ready'); | ||
@@ -125,7 +129,6 @@ } | ||
test.equals(this.callback.callCount, 0); | ||
this.wrappedFn.yields(null, 'Success!'); | ||
this.backoff.emit('ready'); | ||
test.ok(this.callback.calledWith(null, 'Success!')); | ||
test.ok(this.wrappedFn.alwaysCalledWith(1, 2, 3)); | ||
test.done(); | ||
@@ -138,3 +141,3 @@ }, | ||
this.wrappedFn.yields(error); | ||
call.call(this.backoffFactory); | ||
call.start(this.backoffFactory); | ||
@@ -150,2 +153,3 @@ for (var i = 0; i < 3; i++) { | ||
test.ok(this.callback.calledWith(error)); | ||
test.ok(this.wrappedFn.alwaysCalledWith(1, 2, 3)); | ||
test.done(); | ||
@@ -157,3 +161,3 @@ }, | ||
call.abort(); | ||
call.call(this.backoffFactory); | ||
call.start(this.backoffFactory); | ||
test.equals(this.wrappedFn.callCount, 0, | ||
@@ -170,3 +174,3 @@ 'Wrapped function shouldn\'t be called after abort.'); | ||
call.call(this.backoffFactory); | ||
call.start(this.backoffFactory); | ||
@@ -181,3 +185,3 @@ test.equals(this.callback.callCount, 0, | ||
this.wrappedFn.yields(1); | ||
call.call(this.backoffFactory); | ||
call.start(this.backoffFactory); | ||
@@ -200,3 +204,3 @@ for (var i = 2; i < 5; i++) { | ||
test.throws(function() { | ||
call.call(this.backoffFactory); | ||
call.start(this.backoffFactory); | ||
}, Error); | ||
@@ -211,3 +215,3 @@ test.done(); | ||
test.throws(function() { | ||
call.call(this.backoffFactory); | ||
call.start(this.backoffFactory); | ||
}, Error); | ||
@@ -223,3 +227,3 @@ test.done(); | ||
call.on('call', callEventSpy); | ||
call.call(this.backoffFactory); | ||
call.start(this.backoffFactory); | ||
@@ -243,3 +247,3 @@ for (var i = 1; i < 5; i++) { | ||
this.wrappedFn.yields('error'); | ||
call.call(this.backoffFactory); | ||
call.start(this.backoffFactory); | ||
@@ -264,3 +268,3 @@ this.wrappedFn.yields(null, 'done'); | ||
this.wrappedFn.yields(new Error()); | ||
call.call(this.backoffFactory); | ||
call.start(this.backoffFactory); | ||
this.backoff.emit('backoff', 3, 1234); | ||
@@ -267,0 +271,0 @@ |
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
56167
1152
318