A simple registry for callbacks that allows you to add one or more callbacks under some key and then execute all callbacks under some key.
Example:
const registryFactory = require('callback-registry');
const registry = registryFactory();
// add a new callback for that event key
registry.add('event-key', function(){
console.log('the event occurred')
});
// execute all callbacks that were registred for that key
registry.execute('event-key');
You can pass any arguments to the callbacks when you execute them
// execute all callbacks that were registred for that key
registry.execute('event-key', arg1, arg2, arg3);
The execute method returns an array with the results returned from the callbacks.
When you add a new callback a function is returned that can be used to unsubscribe
// A callback that will be called just the first time
var unsubscribe = registry.add('event-key', function(){
console.log('the event occurred');
unsubscribe();
});
- 2.1.1
- return empty array as result if no subscribers
- catch errors in user callbacks (returns undefine in the result if error)