callback-registry
Advanced tools
Comparing version 1.0.0 to 1.0.1
@@ -39,3 +39,3 @@ module.exports = function () { | ||
function execute(key, argumentsArr, context) { | ||
function execute(key, argumentsArr) { | ||
var callbacksForKey = callbacks[key]; | ||
@@ -46,4 +46,6 @@ if (!callbacksForKey || callbacksForKey.length === 0){ | ||
var args = [].splice.call(arguments, 1); | ||
callbacksForKey.forEach(function (callback) { | ||
callback.apply(context, argumentsArr); | ||
callback.apply(undefined, args); | ||
}); | ||
@@ -50,0 +52,0 @@ } |
{ | ||
"name": "callback-registry", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "Registry for callbacks", | ||
@@ -5,0 +5,0 @@ "main": "./lib/index.js", |
# callback-registry | ||
Registry for callbacks | ||
A simple registry that allows you to add one or more callbacks, where each callback is added with some key and then execute all callbacks by just using the key. | ||
Similar to EventEmitter but not limited to Node.js. | ||
```javascript | ||
const Registry = require('../lib/index.js'); | ||
const registry = Registry(); | ||
// add a new callback for that event key | ||
registry.add('event-key', function(){ | ||
console.log('the event occurred') | ||
}); | ||
registry.execute('event-key'); | ||
``` |
var expect = require('chai').expect; | ||
nocache = function(module) { | ||
nocache = function (module) { | ||
delete require.cache[require.resolve(module)]; | ||
@@ -11,51 +11,64 @@ return require(module); | ||
it ('should execute single callback', function (done) { | ||
var registry = Registry(); | ||
registry.add('test', done); | ||
it('should execute single callback', function (done) { | ||
var registry = Registry(); | ||
registry.add('test', done); | ||
registry.execute('test'); | ||
}); | ||
it ('should execute multiple callbacks', function (done) { | ||
it('should execute multiple callbacks', function (done) { | ||
var registry = Registry(); | ||
var invoked = 0; | ||
var p = function(){ | ||
invoked ++; | ||
if (invoked == 3){ | ||
var p = function () { | ||
invoked++; | ||
if (invoked == 3) { | ||
done(); | ||
} | ||
} | ||
registry.add('test', p); | ||
} | ||
registry.add('test', p); | ||
registry.add('test', p); | ||
registry.add('test', p); | ||
registry.execute('test'); | ||
}); | ||
it ('should not execute callbacks on different keys', function (done) { | ||
it('should not execute callbacks on different keys', function (done) { | ||
var registry = Registry(); | ||
var invoked = 0; | ||
var p1 = function(){ | ||
invoked ++; | ||
if (invoked == 2){ | ||
var p1 = function () { | ||
invoked++; | ||
if (invoked == 2) { | ||
done(); | ||
} | ||
} | ||
var p2 = function(){ | ||
} | ||
var p2 = function () { | ||
done('should not be invoked'); | ||
} | ||
registry.add('test1', p1); | ||
registry.add('test1', p1); | ||
registry.add('test2', p2); | ||
registry.execute('test1'); | ||
registry.execute('test1'); | ||
}); | ||
}); | ||
it ('should be able to remove callback', function (done) { | ||
var registry = Registry(); | ||
var callbackKey = registry.add('test', function(){ | ||
it('should be able to remove callback', function (done) { | ||
var registry = Registry(); | ||
var callbackKey = registry.add('test', function () { | ||
console.log('!!! ERROR'); | ||
done('should not be executed'); | ||
}); | ||
registry.remove(callbackKey); | ||
registry.remove(callbackKey); | ||
registry.execute('test'); | ||
done(); | ||
}); | ||
}); | ||
it('should pass arguments', function (done) { | ||
var registry = Registry(); | ||
registry.add('test', function (a, b, c) { | ||
console.log(arguments); | ||
if (a === 1 && b === '2' && c === 3) { | ||
done(); | ||
} else { | ||
done('error - arguments not matching'); | ||
} | ||
}); | ||
registry.execute('test', 1, '2', 3); | ||
}); | ||
}); |
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
12305
109
21