Security News
RubyGems.org Adds New Maintainer Role
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
eventemitter2
Advanced tools
A Node.js event emitter implementation with namespaces, wildcards, TTL and browser support
The eventemitter2 npm package is an implementation of the EventEmitter module found in Node.js. It provides an interface for implementing event-driven architecture. It allows you to create objects that can emit named events that cause function objects ('listeners') to be called. It offers several enhancements over the native EventEmitter, such as namespaces, wildcards, and the ability to listen to all events.
Event Emitting
This feature allows you to emit events and register listeners that get called when those events are emitted.
const EventEmitter2 = require('eventemitter2').EventEmitter2;
const emitter = new EventEmitter2();
emitter.on('event', function() {
console.log('an event occurred!');
});
emitter.emit('event');
Namespaces/Wildcards
This feature enables the use of namespaces and wildcards for event names, allowing for more flexible event handling.
const EventEmitter2 = require('eventemitter2').EventEmitter2;
const emitter = new EventEmitter2({
wildcard: true
});
emitter.on('foo.*', function() {
console.log('foo event occurred!');
});
emitter.emit('foo.bar');
Listening to All Events
This feature allows you to listen to all events that are emitted from an EventEmitter2 instance.
const EventEmitter2 = require('eventemitter2').EventEmitter2;
const emitter = new EventEmitter2();
emitter.onAny(function(event, value) {
console.log('All event handler:', event, value);
});
emitter.emit('randomEvent', 'with some value');
The 'events' package is the original EventEmitter that comes with Node.js. It provides the basic functionality for event emitting and listening but lacks some of the advanced features of eventemitter2, such as namespaces and wildcards.
Mitt is a tiny functional event emitter / pubsub. It provides a similar event-driven approach but with a smaller footprint and without namespaces or wildcard features, focusing on simplicity and performance.
This is another EventEmitter implementation with a similar API to eventemitter2. It offers additional features like context binding and once listeners, but it does not support namespaces or wildcards.
EventEmitter2 is a an implementation of the EventEmitter found in Node.js
once
concept var server = EventEmitter2({
delimiter: '/', // the delimiter used to segment namespaces, defaults to `.`.
maxListeners: 20 // the max number of listeners that can be assigned to an event, defaults to 10.
});
server.on('foo.*', function(event, value1, value2) {
console.log('a values were', value1, value2);
});
once
concept. server.many('foo', function(event, value1, value2) {
console.log('a values were', value1, value2);
}, 4);
When an EventEmitter
instance experiences an error, the typical action is
to emit an error
event. Error events are treated as a special case.
If there is no listener for it, then the default action is to print a stack
trace and exit the program.
All EventEmitters emit the event newListener
when new listeners are
added.
Adds a listener to the end of the listeners array for the specified event.
server.on('data', function(value) {
console.log('The event was raised!');
});
server.on('data', function(value) {
console.log('This event will be listened to exactly four times.');
}, 4);
Adds a one time listener for the event. The listener is invoked only the first time the event is fired, after which it is removed.
server.once('get', function (value) {
console.log('Ah, we have our first value!');
});
Adds a listener that will execute n times for the event before being removed. The listener is invoked only the first time the event is fired, after which it is removed.
server.many('get', function (value) {
console.log('Ah, we have our first value!');
}, 4);
Remove a listener from the listener array for the specified event. Caution: changes array indices in the listener array behind the listener.
var callback = function(value) {
console.log('someone connected!');
};
server.on('get', callback);
// ...
server.removeListener('get', callback);
Removes all listeners, or those of the specified event.
By default EventEmitters will print a warning if more than 10 listeners are added to it. This is a useful default which helps finding memory leaks. Obviously not all Emitters should be limited to 10. This function allows that to be increased. Set to zero for unlimited.
Returns an array of listeners for the specified event. This array can be manipulated, e.g. to remove listeners.
server.on('get', function (value) {
console.log('someone connected!');
});
console.log(console.log(server.listeners('get')); // [ [Function] ]
Execute each of the listeners in order with the list of arguments.
There is a test suite that tries to cover each use case, it can be found here.
(The MIT License)
Copyright (c) 2011 hij1nx http://www.twitter.com/hij1nx, indexzero http://www.twitter.com/indexzero, Fedor Indutny http://www.twitter.com/indutny
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
FAQs
A feature-rich Node.js event emitter implementation with namespaces, wildcards, TTL, async listeners and browser/worker support.
We found that eventemitter2 demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.
Security News
Research
Socket's threat research team has detected five malicious npm packages targeting Roblox developers, deploying malware to steal credentials and personal data.