callback-registry
Advanced tools
Comparing version 2.2.0 to 2.2.1
@@ -29,3 +29,3 @@ module.exports = function () { | ||
if (!callbacksForKey || callbacksForKey.length === 0){ | ||
return; | ||
return []; | ||
} | ||
@@ -36,5 +36,9 @@ | ||
callbacksForKey.forEach(function (callback) { | ||
var result = callback.apply(undefined, args); | ||
results.push(result); | ||
}); | ||
try { | ||
var result = callback.apply(undefined, args); | ||
results.push(result); | ||
} catch (err) { | ||
results.push(undefined); | ||
} | ||
}); | ||
@@ -41,0 +45,0 @@ return results; |
{ | ||
"name": "callback-registry", | ||
"version": "2.2.0", | ||
"version": "2.2.1", | ||
"description": "Registry for callbacks", | ||
@@ -5,0 +5,0 @@ "main": "./lib/index.js", |
# Intro | ||
A simple registry for callbacks that allows you to add one or more callbacks under some key and then execute all callbacks by just using the key. | ||
A simple registry for callbacks that allows you to add one or more callbacks | ||
under some key and then execute all by just using the key. | ||
@@ -30,5 +31,5 @@ Example: | ||
# Returning results | ||
The _execute_ method returns an array with the reuslts retuned from the callbacks. | ||
The _execute_ method returns an array with the results returned from the callbacks. | ||
# Remove a callback | ||
# Removing a callback | ||
When you add a new callback a function is returned that can be used to unsubscribe | ||
@@ -45,1 +46,7 @@ | ||
``` | ||
# Change log | ||
* 2.1.1 | ||
* return empty array as result if no subscribers | ||
* catch errors in user callbacks (returns undefine in the result if error) |
@@ -88,5 +88,5 @@ var expect = require('chai').expect; | ||
expect(result[1].a).to.equal(2); | ||
}) | ||
}); | ||
it('should return arguments', function(){ | ||
it('should return results from the callbacks in array', function(){ | ||
var registry = Registry(); | ||
@@ -105,3 +105,27 @@ registry.add('test', function () { | ||
expect(result.length).to.equal(1); | ||
}) | ||
}); | ||
it('should return empty array if no subscribers', function(){ | ||
var registry = Registry(); | ||
var result = registry.execute('test'); | ||
expect(Array.isArray(result)).to.equal(true); | ||
expect(result.length).to.equal(0); | ||
}); | ||
it('should return results from the callbacks even if some of the callbacks throw Error', function(){ | ||
var registry = Registry(); | ||
registry.add('test', function () { | ||
throw Error('test'); | ||
}); | ||
registry.add('test', function () { | ||
return 1; | ||
}); | ||
var result = registry.execute('test'); | ||
expect(Array.isArray(result)).to.equal(true); | ||
expect(result.length).to.equal(2); | ||
expect(result[0]).to.equal(undefined); | ||
expect(result[1]).to.equal(1); | ||
}); | ||
}); |
8216
155
51