callback-registry
Advanced tools
Comparing version 2.1.0 to 2.2.0
@@ -20,3 +20,3 @@ module.exports = function () { | ||
callbacksForKey = callbacksForKey.filter(function(item){ | ||
item != callback; | ||
return item !== callback; | ||
}); | ||
@@ -23,0 +23,0 @@ callbacks[key] = callbacksForKey; |
{ | ||
"name": "callback-registry", | ||
"version": "2.1.0", | ||
"version": "2.2.0", | ||
"description": "Registry for callbacks", | ||
@@ -5,0 +5,0 @@ "main": "./lib/index.js", |
@@ -1,13 +0,12 @@ | ||
# callback-registry | ||
Registry for callbacks | ||
# Intro | ||
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. | ||
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. | ||
Similar to EventEmitter but not limited to Node.js. | ||
Example: | ||
```javascript | ||
const callbackRegistry = require('callback-registry'); | ||
const registry = callbackRegistry(); | ||
const registryFactory = require('callback-registry'); | ||
const registry = registryFactory(); | ||
// add a new callback for that event key | ||
@@ -18,4 +17,28 @@ registry.add('event-key', function(){ | ||
// execute all callbacks that were registred for that key | ||
registry.execute('event-key'); | ||
``` | ||
# Passing arguments | ||
You can pass any arguments to the callbacks when you execute them | ||
```javascript | ||
// execute all callbacks that were registred for that key | ||
registry.execute('event-key', arg1, arg2, arg3); | ||
``` | ||
# Returning results | ||
The _execute_ method returns an array with the reuslts retuned from the callbacks. | ||
# Remove a callback | ||
When you add a new callback a function is returned that can be used to unsubscribe | ||
```javascript | ||
// A callback that will be called just the first time | ||
var unsubscribe = registry.add('event-key', function(){ | ||
console.log('the event occurred'); | ||
unsubscribe(); | ||
}); | ||
``` |
@@ -25,3 +25,3 @@ var expect = require('chai').expect; | ||
} | ||
} | ||
}; | ||
registry.add('test', p); | ||
@@ -41,3 +41,3 @@ registry.add('test', p); | ||
} | ||
} | ||
}; | ||
var p2 = function () { | ||
@@ -75,2 +75,34 @@ done('should not be invoked'); | ||
}); | ||
}); | ||
it('should return arguments', function(){ | ||
var registry = Registry(); | ||
registry.add('test', function () { | ||
return {a:1}; | ||
}); | ||
registry.add('test', function () { | ||
return {a:2}; | ||
}); | ||
var result = registry.execute('test'); | ||
expect(result[0]).to.be.an('object'); | ||
expect(result[0].a).to.equal(1); | ||
expect(result[1]).to.be.an('object'); | ||
expect(result[1].a).to.equal(2); | ||
}) | ||
it('should return arguments', function(){ | ||
var registry = Registry(); | ||
registry.add('test', function () { | ||
return {a:1}; | ||
}); | ||
var unsubscribe2 = registry.add('test', function () { | ||
unsubscribe2(); | ||
return {a:2}; | ||
}); | ||
var result = registry.execute('test'); | ||
expect(result.length).to.equal(2); | ||
result = registry.execute('test'); | ||
expect(result.length).to.equal(1); | ||
}) | ||
}); |
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
131
44
7109
7