Comparing version 0.1.4 to 0.2.0
@@ -10,3 +10,4 @@ /** | ||
var ErrorList = require('./error-list'); | ||
var ErrorList = require('./error-list'); | ||
var TimeoutError = require('./timeout-error'); | ||
@@ -55,6 +56,19 @@ function listen(initialValues) { | ||
function listener() { | ||
function withTimeout(delegate, timeout) { | ||
var timer = setTimeout(function () { | ||
delegate(new TimeoutError()); | ||
delegate = null; | ||
}, timeout); | ||
return function (err, value) { | ||
if (delegate) { | ||
clearTimeout(timer); | ||
delegate(err, value); | ||
} | ||
}; | ||
} | ||
function listener(timeout) { | ||
assertUnresolved(); | ||
var index = offset + count++; | ||
return function (err, value) { | ||
var callback = function (err, value) { | ||
if (err) { | ||
@@ -70,2 +84,3 @@ pushError(err); | ||
}; | ||
return timeout ? withTimeout(callback, timeout) : callback; | ||
} | ||
@@ -72,0 +87,0 @@ |
{ | ||
"name" : "listen", | ||
"version" : "0.1.4", | ||
"version" : "0.2.0", | ||
"description" : "Wait for the results of multiple callbacks", | ||
@@ -21,3 +21,3 @@ "author" : "Maximilian Antoni (http://maxantoni.de)", | ||
"urun" : "~0.0.6", | ||
"sinon" : "~1.4.2", | ||
"sinon" : "~1.5.0", | ||
"uglify-js" : "~1.2.6", | ||
@@ -24,0 +24,0 @@ "autolint" : "~1.0.4" |
@@ -47,9 +47,17 @@ # listen.js | ||
- `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`. | ||
#### `listen([values])` | ||
Creates and returns a new listener. The values array with initial values is optional. | ||
#### `listener([timeout])` | ||
Creates a new callback associated with the listener. If the optional timeout is given, the listener errs with a `TimeoutError` if the callback was not invoked. Throws if called after `then`. | ||
#### `listener.then(func)` | ||
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. | ||
#### `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 | ||
@@ -61,8 +69,16 @@ | ||
## Compile for browsers and minify | ||
## Hacking | ||
This requires [nomo.js](https://github.com/mantoni/nomo.js). | ||
If you'd like to hack listen.js here is how to get started: | ||
``` | ||
make compile | ||
``` | ||
- `npm install` will setup everything you need. | ||
- `make` lints the code with JSLint and runs all unit tests. | ||
- Use can also `make lint` or `make test` individually. | ||
Running the test cases in a browser instead of Node requires [nomo.js](https://github.com/mantoni/nomo.js). | ||
- Run `npm install -g nomo` | ||
- Run `nomo server` from within the project directory. | ||
- Open http://localhost:4444/test in your browser. | ||
To build a browser package containing the merged / minified scripts run `make package`. |
@@ -12,2 +12,3 @@ /** | ||
var assert = require('assert'); | ||
var sinon = require('sinon'); | ||
@@ -21,5 +22,10 @@ var listen = require('../lib/listen'); | ||
this.listener = listen(); | ||
this.clock = sinon.useFakeTimers(); | ||
}, | ||
after: function () { | ||
this.clock.restore(); | ||
}, | ||
'should return callback function': function () { | ||
@@ -44,2 +50,75 @@ var callback = this.listener(); | ||
assert.equal('Cannot be called after then', error.message); | ||
}, | ||
'should err on timeout': function () { | ||
var spy = sinon.spy(); | ||
this.listener(1000); | ||
this.listener.then(spy); | ||
this.clock.tick(999); | ||
sinon.assert.notCalled(spy); | ||
this.clock.tick(1); | ||
sinon.assert.calledOnce(spy); | ||
sinon.assert.calledWithMatch(spy, { | ||
name : 'TimeoutError' | ||
}); | ||
}, | ||
'should not set a timeout by default': function () { | ||
var spy = sinon.spy(); | ||
this.listener(); | ||
this.listener.then(spy); | ||
this.clock.tick(1); | ||
sinon.assert.notCalled(spy); | ||
}, | ||
'should not resolve if waiting for another callback': function () { | ||
var spy = sinon.spy(); | ||
this.listener(1000); | ||
this.listener(); | ||
this.listener.then(spy); | ||
this.clock.tick(1000); | ||
sinon.assert.notCalled(spy); | ||
}, | ||
'should clear timeout': function () { | ||
var spy = sinon.spy(); | ||
var callback1 = this.listener(250); | ||
var callback2 = this.listener(); | ||
this.listener.then(spy); | ||
callback1(); | ||
this.clock.tick(250); | ||
callback2(); | ||
sinon.assert.calledOnce(spy); | ||
sinon.assert.calledWith(spy, null); | ||
}, | ||
'should ignore callback arguments after timeout': function () { | ||
var spy = sinon.spy(); | ||
var callback1 = this.listener(500); | ||
var callback2 = this.listener(); | ||
this.listener.then(spy); | ||
this.clock.tick(500); | ||
callback1(new TypeError()); | ||
callback2(); | ||
sinon.assert.calledOnce(spy); | ||
sinon.assert.calledWithMatch(spy, { | ||
name : 'TimeoutError' | ||
}); | ||
} | ||
@@ -46,0 +125,0 @@ |
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
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
24883
18
579
83
1