Comparing version 0.2.0 to 0.3.0
/** | ||
* listen.js | ||
* | ||
* Copyright (c) 2012 Maximilian Antoni <mail@maxantoni.de> | ||
* Copyright (c) 2012-2013 Maximilian Antoni <mail@maxantoni.de> | ||
* | ||
@@ -10,4 +10,4 @@ * @license MIT | ||
var ErrorList = require('./error-list'); | ||
var TimeoutError = require('./timeout-error'); | ||
var ErrorList = require('./error-list'); | ||
var TimeoutError = require('./timeout-error'); | ||
@@ -25,5 +25,5 @@ function listen(initialValues) { | ||
var offset = values.length; | ||
var count = 0; | ||
var called = 0; | ||
var offset = values.length; | ||
var count = 0; | ||
var called = 0; | ||
var errList; | ||
@@ -70,6 +70,19 @@ var handler; | ||
function listener(timeout) { | ||
function listener(fn, timeout) { | ||
if (typeof fn === 'number') { | ||
timeout = fn; | ||
fn = null; | ||
} | ||
assertUnresolved(); | ||
var index = offset + count++; | ||
var callback = function (err, value) { | ||
if (fn) { | ||
try { | ||
fn(err, value); | ||
} catch (e) { | ||
if (e !== err) { | ||
pushError(e); | ||
} | ||
} | ||
} | ||
if (err) { | ||
@@ -76,0 +89,0 @@ pushError(err); |
{ | ||
"name" : "listen", | ||
"version" : "0.2.0", | ||
"version" : "0.3.0", | ||
"description" : "Wait for the results of multiple callbacks", | ||
@@ -31,2 +31,2 @@ "author" : "Maximilian Antoni (http://maxantoni.de)", | ||
} | ||
} | ||
} |
@@ -13,2 +13,7 @@ # listen.js | ||
## Download for browsers | ||
Browser package are here: http://maxantoni.de/listen.js/ | ||
## Usage | ||
@@ -54,2 +59,5 @@ | ||
#### `listener(func[, timeout])` | ||
Creates a new callback which passes the arguments to the given function. Can be combined with an optional timeout. Throws if called after `then`. | ||
#### `listener.then(func)` | ||
@@ -56,0 +64,0 @@ Invokes the given function once all callbacks where invoked. If none of the callbacks had errors, the first argument is `null`, otherwise it's an `Error`. The second argument is the values array in order of callback creation. Can only be called once. |
@@ -121,5 +121,101 @@ /** | ||
}); | ||
} | ||
}, | ||
'should invoke given function': function () { | ||
var spy = sinon.spy(); | ||
var callback = this.listener(spy); | ||
callback(); | ||
sinon.assert.calledOnce(spy); | ||
}, | ||
'should pass error to callback': function () { | ||
var spy = sinon.spy(); | ||
var callback = this.listener(spy); | ||
var err = new Error(); | ||
callback(err); | ||
sinon.assert.calledWith(spy, err); | ||
}, | ||
'should pass null and value to callback': function () { | ||
var spy = sinon.spy(); | ||
var callback = this.listener(spy); | ||
callback(null, 'some value'); | ||
sinon.assert.calledWith(spy, null, 'some value'); | ||
}, | ||
'should allow to combine function and timeout arguments': function () { | ||
var spy = sinon.spy(); | ||
var callback = this.listener(spy, 250); | ||
this.clock.tick(250); | ||
sinon.assert.calledWithMatch(spy, { | ||
name : 'TimeoutError' | ||
}); | ||
}, | ||
'should invoke given function before resolving the listener': function () { | ||
var spy1 = sinon.spy(); | ||
var spy2 = sinon.spy(); | ||
var callback = this.listener(spy1); | ||
this.listener.then(spy2); | ||
callback(); | ||
sinon.assert.callOrder(spy1, spy2); | ||
}, | ||
'should pass error thrown in given function to then': function () { | ||
var err = new Error('ouch'); | ||
var callback = this.listener(sinon.stub().throws(err)); | ||
var spy = sinon.spy(); | ||
this.listener.then(spy); | ||
callback(); | ||
sinon.assert.calledWith(spy, err); | ||
}, | ||
'should combine error thrown in given function with err passed to callback': | ||
function () { | ||
var err1 = new Error('ouch'); | ||
var err2 = new Error('oh noes'); | ||
var callback = this.listener(sinon.stub().throws(err1)); | ||
var spy = sinon.spy(); | ||
this.listener.then(spy); | ||
callback(err2); | ||
sinon.assert.calledWithMatch(spy, { | ||
name : 'ErrorList', | ||
errors : [err1, err2] | ||
}); | ||
}, | ||
'should not create an error list if the given function re-throws the error': | ||
function () { | ||
var err = new Error('ouch'); | ||
var callback = this.listener(function (e) { throw e; }); | ||
var spy = sinon.spy(); | ||
this.listener.then(spy); | ||
callback(err); | ||
sinon.assert.calledWith(spy, err); | ||
} | ||
}); |
@@ -27,4 +27,4 @@ /** | ||
'should require function argument': function () { | ||
var re = /^TypeError: Function expected$/; | ||
var then = this.then; | ||
var re = /^TypeError: Function expected$/; | ||
var then = this.then; | ||
@@ -76,4 +76,4 @@ assert.throws(function () { | ||
'should invoke given function if callback was already called': function () { | ||
var spy = sinon.spy(); | ||
var callback = this.listener(); | ||
var spy = sinon.spy(); | ||
var callback = this.listener(); | ||
@@ -88,4 +88,4 @@ callback(); | ||
'should invoke given function after callback was called': function () { | ||
var spy = sinon.spy(); | ||
var callback = this.listener(); | ||
var spy = sinon.spy(); | ||
var callback = this.listener(); | ||
@@ -115,4 +115,4 @@ this.then(spy); | ||
function () { | ||
var spy = sinon.spy(); | ||
var callback = this.listener(); | ||
var spy = sinon.spy(); | ||
var callback = this.listener(); | ||
@@ -127,4 +127,4 @@ this.then(spy); | ||
'should pass callback argument to given function': function () { | ||
var spy = sinon.spy(); | ||
var callback = this.listener(); | ||
var spy = sinon.spy(); | ||
var callback = this.listener(); | ||
@@ -131,0 +131,0 @@ callback(null, 123); |
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
Native code
Supply chain riskContains native code (e.g., compiled binaries or shared libraries). Including native code can obscure malicious behavior.
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
Native code
Supply chain riskContains native code (e.g., compiled binaries or shared libraries). Including native code can obscure malicious behavior.
Found 1 instance in 1 package
27725
657
91