deventor is a minimal event emitter system built entirely in pure Javascript with 0 dependencies, focused on the browser.
- Class: Deventor(deventorSettings)
- Event: Deventor:newListener
- Event: Deventor:removeListener
- Event: Deventor:setMaxListeners
- Static Attribute: Deventor.defaultMaxListeners
- Static Attribute: Deventor.DEVENTOR_EVENTS
- Method: deventor.getDeventorName()
- Method: deventor.setDeventorName(deventorName)
- Method: deventor.getMaxListeners()
- Method: deventor.setMaxListeners(maxListeners)
- Method: deventor.getEventNames()
- Method: deventor.getListenersByEventName(eventName)
- Method: deventor.on(eventName, listener)
- Method: deventor.once(eventName, listener)
- Method: deventor.off(eventName, listener)
- Method: deventor.emit(eventName[,...args])
- Method: deventor.removeAllListeners()
- Method: deventor.addListener(eventName, listener)
- Method: deventor.removeListener(eventName, listener)
The Deventor constructor can accept an object parameter with two attributes:
- name {?String} The name of the
Deventorinstance - maxListeners {?Number} The value of the maximum listeners of the
Deventorinstance
var myDeventor = new Deventor({
name: 'myDeventor',
maxListeners: 20
});- eventName {String} The name of the event being listened for
- listener {Function} The event handler function
The Deventor instance will emit its own 'Deventor:newListener' event before a listener is added to its internal array of listeners.
var myDeventor = new Deventor();
// Only do this once so we don't loop forever
myDeventor.once(Deventor.DEVENTOR_EVENTS.NEW_LISTENER, function (eventName, listener) {
if (eventName === 'myEvent') {
myDeventor.on('myEvent', function () {
console.log('B');
});
}
});
myDeventor.on('myEvent', function () {
console.log('A');
});
myDeventor.emit('myEvent');
// Prints:
// B
// A- eventName {String} The name of the event
- listener {Function} The event handler function to be removed
The Deventor instance will emit its own 'Deventor:removeListener' event after the listener is removed from its internal array of listeners.
var myDeventor = new Deventor();
var myEventHandler = function () {
console.log('A');
};
myDeventor.on('myEvent', myEventHandler);
myDeventor.emit('myEvent');
// Prints:
// A
myDeventor.on(Deventor.DEVENTOR_EVENTS.REMOVE_LISTENER, function (eventName, listener) {
console.log('Event Name -> ' + eventName);
console.log('Listener to be removed -> ', listener);
});
myDeventor.off('myEvent', myEventHandler)
// Prints:
// Event name -> myEvent
// Listener to be removed -> Function- oldMaxListeners {Number} The old value of maximum listeners of
Deventorinstance - newMaxListeners {Number} The new value of maximum listeners of
Deventorinstance
The Deventor instance will emit its own 'Deventor:setMaxListeners' event after the maxListeners attribute has changed.
var myDeventor = new Deventor();
myDeventor.on(Deventor.DEVENTOR_EVENTS.SET_MAX_LISTENERS, function (result) {
console.log('Old max listeners value -> ' + result.oldMaxListeners);
console.log('New max listeners value -> ' + result.newMaxListeners);
});
myDeventor.setMaxListeners(20);
// Prints:
// Old max listeners value -> 10
// New max listeners value -> 20By default, a maximum of 10 listeners can be registered for any single event. This limit can be changed for individual Deventor instances using the deventor.setMaxListeners(maxListeners) method. To change the default for all Deventor instances, the Deventor.defaultMaxListeners property can be used.
Note that this is not a hard limit. The Deventor instance will allow more listeners to be added but will output a trace warning to console indicating that "Max. listeners exceeded to [eventName] event. Max. listeners = [maxListeners]. Take care of memory leaks.". For any single Deventor, the deventor.getMaxListeners() and deventor.setMaxListeners(maxListeners) methods can be used to temporarily avoid this warning:
var myDeventor = new Deventor();
myDeventor.setMaxListeners(myDeventor.getMaxListeners() + 1);
myDeventor.once('event', function () {
// ...
myDeventor.setMaxListeners(Math.max(myDeventor.getMaxListeners() - 1, 0));
});Returns the name of the Deventor instance.
var myDeventor = new Deventor({
name: 'myDeventor'
});
console.log(myDeventor.getDeventorName());
// Prints:
// myDeventorSets the name of the Deventor instance.
- deventorName {String} The new
Deventorname
var myDeventor = new Deventor({
name: 'myDeventor'
});
myDeventor.setDeventorName('myOtherDeventor');
console.log(myDeventor.getDeventorName());
// Prints:
// myOtherDeventorReturns the current max listeners value for the Deventor which is either set by deventor.setMaxListeners(maxListeners) or defaults to Deventor.defaultMaxListeners
var myDeventor = new Deventor();
console.log(myDeventor.getMaxListeners());
// Prints:
// 10Sets the max listeners value for the Deventor instance.
- maxListeners {Number} The new max listeners value for the
Deventorinstance
var myDeventor = new Deventor();
myDeventor.setMaxListeners(30);
console.log(myDeventor.getMaxListeners());
// Prints:
// 30Returns an array listing the events which the emitter has registered listeners. The values in the array will be strings.
var myDeventor = new Deventor();
myDeventor.on('event1', function () {
// ...
});
myDeventor.on('event2', function () {
// ...
});
console.log(myDeventor.getEventNames());
// Prints:
// ['event1', 'event2']Returns an array listing the listeners of an event that has been registered. The values in the array will be functions.
- eventName {String} The name of the event
var myDeventor = new Deventor();
myDeventor.on('event1', function () {
console.log('A');
});
myDeventor.on('event1', function () {
console.log('B');
});
console.log(myDeventor.getListenersByEventName('event1'));
// Prints:
// [function () { console.log('A'); }, function () { console.log('B'); }]- eventName {String} The name of the event
- listener {Function} The callback function
Adds the listener function to the end of the listeners array of the event named eventName. No checks are made to see if the listener has already been added. Multiple calls passing the same combination of eventName and listener will result in the listener being added, and called, multiple times. By default, event listeners are invoked in the order they are added.
var myDeventor = new Deventor();
myDeventor.on('myEvent', function () {
console.log('My event');
});- eventName {String} The name of the event
- listener {Function} The callback function
Adds a one time listener function for the event named eventName. The next time eventName is triggered, this listener is removed and then invoked. By default, event listeners are invoked in the order they are added.
var myDeventor = new Deventor();
myDeventor.once('myEvent', function () {
console.log('My event');
});- eventName {String} The name of the event
- listener {Function} The callback function
Removes the specified listener from the listener array for the event named eventName.
var myDeventor = new Deventor();
var callback = function () {
console.log('My event');
};
myDeventor.on('myEvent', callback);
myDeventor.off('myEvent', callback);- eventName {String} The name of the event
- ...args {?Any} The arguments to pass to the event
Synchronously calls each of the listeners registered for the event named eventName, in the order they were registered, passing the supplied arguments to each.
var myDeventor = new Deventor();
myDeventor.on('myEvent', function (args) {
console.log('My event with args -> ', args);
});
myDeventor.emit('myEvent', {
attribute: 'attribute',
value: 10
});
// Prints:
// My event with args -> { attribute: 'attribute', value: 10 }Removes all listeners.
var myDeventor = new Deventor();
myDeventor.on('myEvent', function (args) {
console.log('My event with args -> ', args);
});
myDeventor.on('myOtherEvent', function () {
console.log('My other event');
});
myDeventor.removeAllListeners();- eventName {String} The name of the event
- listener {Function} The callback function
Alias for deventor.on(eventName, listener)
- eventName {String} The name of the event
- listener {Function} The callback function
Alias for deventor.off(eventName, listener)