Comparing version 0.1.1 to 0.1.2
@@ -36,7 +36,21 @@ /** | ||
function assertUnresolved() { | ||
if (handler) { | ||
throw new Error("Cannot be called after then"); | ||
} | ||
} | ||
function pushError(e) { | ||
if (!errList) { | ||
errList = []; | ||
} | ||
errList.push(e); | ||
} | ||
function listener() { | ||
assertUnresolved(); | ||
var index = offset + count++; | ||
return function (err, value) { | ||
if (err) { | ||
listener.err(err); | ||
pushError(err); | ||
} | ||
@@ -53,2 +67,3 @@ if (value !== undefined) { | ||
listener.push = function push(value) { | ||
assertUnresolved(); | ||
values[offset + count] = value; | ||
@@ -58,7 +73,5 @@ offset++; | ||
listener.err = function err(err) { | ||
if (!errList) { | ||
errList = []; | ||
} | ||
errList.push(err); | ||
listener.err = function err(e) { | ||
assertUnresolved(); | ||
pushError(e); | ||
}; | ||
@@ -65,0 +78,0 @@ |
{ | ||
"name" : "listen", | ||
"version" : "0.1.1", | ||
"version" : "0.1.2", | ||
"description" : "Wait for the results of multiple callbacks", | ||
@@ -5,0 +5,0 @@ "author" : "Maximilian Antoni (http://maxantoni.de)", |
@@ -1,4 +0,63 @@ | ||
listen.js | ||
========= | ||
# listen.js | ||
Wait for the results of multiple callbacks | ||
Wait for the results of multiple callbacks. | ||
[![Build Status](https://secure.travis-ci.org/mantoni/listen.js.png?branch=master)](http://travis-ci.org/mantoni/listen.js) | ||
## Install | ||
``` | ||
npm install listen | ||
```` | ||
## Usage | ||
```js | ||
var listen = require('listen'); | ||
var listener = listen(); | ||
var callbackA = listener(); | ||
var callbackB = listener(); | ||
// ... do async stuff with callbacks | ||
listener.then(function (err, values) { | ||
/* | ||
* err - 1) null if no callback err'd | ||
* 2) the error of the callback that err'd | ||
* 3) an error with name ErrorList wrapping multiple errors | ||
* | ||
* values - The non-undefined return values from all callbacks in order of | ||
* callback creation | ||
*/ | ||
}); | ||
``` | ||
## API | ||
`listen()` - creates a new listener. | ||
`listen(values)` - creates a new listener with initial values. The given argument must be an array. | ||
`listener()` - creates a new callback associated with the listener. Throws if called after `then`. | ||
`listener.then(func)` - invokes the given function once all callbacks where invoked. Throws if already called. | ||
`listener.push(value)` - pushes a value to the internal values array. Throws if called after `then`. | ||
`listener.err(error)` - adds an error to the internal error list. Throws if called after `then`. | ||
## Run tests | ||
``` | ||
make | ||
``` | ||
## Compile for browsers | ||
This requires [nomo.js](https://github.com/mantoni/nomo.js). | ||
``` | ||
make compile | ||
``` |
@@ -32,2 +32,12 @@ /** | ||
assert.deepEqual(spy.firstCall.args[0].errors, ['a', 'b']); | ||
}, | ||
'should throw if called after then': function () { | ||
this.listener.then(function () {}); | ||
var self = this; | ||
assert.throws(function () { | ||
self.listener.err(); | ||
}, /^Error: Cannot be called after then$/); | ||
} | ||
@@ -34,0 +44,0 @@ |
@@ -27,2 +27,12 @@ /** | ||
assert.equal(typeof callback, 'function'); | ||
}, | ||
'should throw if called after then': function () { | ||
this.listener.then(function () {}); | ||
var self = this; | ||
assert.throws(function () { | ||
self.listener(); | ||
}, /^Error: Cannot be called after then$/); | ||
} | ||
@@ -29,0 +39,0 @@ |
@@ -44,6 +44,15 @@ /** | ||
sinon.assert.calledWith(spy, null, ['a', 'b']); | ||
}, | ||
'should throw if called after then': function () { | ||
this.listener.then(function () {}); | ||
var self = this; | ||
assert.throws(function () { | ||
self.listener.push(1); | ||
}, /^Error: Cannot be called after then$/); | ||
} | ||
}); |
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
15358
14
431
64