Security News
GitHub Removes Malicious Pull Requests Targeting Open Source Repositories
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Facebook's EventEmitter is a simple emitter implementation that prioritizes speed and simplicity. It is conceptually similar to other emitters like Node's EventEmitter, but the precise APIs differ. More complex abstractions like the event systems used on
The fbemitter package provides an implementation of the EventEmitter module that allows for managing and emitting events in a flexible manner. It's particularly useful in applications where components or modules need to communicate changes or actions to other parts of the application without being directly linked.
EventEmitter
This feature allows you to create an event emitter, subscribe to events, and emit events. The code sample demonstrates creating an EventEmitter instance, subscribing to an event with a callback, emitting an event with data, and finally removing the subscription.
const {EventEmitter} = require('fbemitter');
const emitter = new EventEmitter();
function handleEvent(payload) {
console.log('Event received:', payload);
}
const subscription = emitter.addListener('event-name', handleEvent);
emitter.emit('event-name', {some: 'data'});
// When the subscription is no longer needed
dubscription.remove();
The 'events' package is Node.js's core EventEmitter implementation made available for use in other environments. It offers similar functionality for event handling but is more widely used and recognized as the standard in Node.js environments. Compared to fbemitter, it might be more familiar to developers but lacks some of the additional features fbemitter provides, such as event subscription management.
eventemitter3 is a high performance EventEmitter implementation that is both lightweight and fast. It provides a similar API to fbemitter but focuses on performance optimizations and a minimal footprint. Compared to fbemitter, eventemitter3 might be preferred in performance-critical applications.
wolfy87-eventemitter is another EventEmitter implementation that offers a wide array of features for event handling, including wildcard event names and times to listen. It provides a more feature-rich alternative to fbemitter, catering to applications that need more complex event handling capabilities.
Facebook's EventEmitter is a simple emitter implementation that prioritizes speed and simplicity. It is conceptually similar to other emitters like Node's EventEmitter, but the precise APIs differ. More complex abstractions like the event systems used on facebook.com and m.facebook.com can be built on top of EventEmitter as well DOM event systems.
EventEmitter's API shares many concepts with other emitter APIs. When events are emitted through an emitter instance, all listeners for the given event type are invoked.
var emitter = new EventEmitter();
emitter.addListener('event', function(x, y) { console.log(x, y); });
emitter.emit('event', 5, 10); // Listener prints "5 10".
EventEmitters return a subscription for each added listener. Subscriptions provide a convenient way to remove listeners that ensures they are removed from the correct emitter instance.
var subscription = emitter.addListener('event', listener);
subscription.remove();
First install the fbemitter
package via npm
, then you can require or import it.
var {EventEmitter} = require('fbemitter');
var emitter = new EventEmitter();
Once you have the repository cloned, building a copy of fbemitter
is easy, just run gulp build
. This assumes you've installed gulp
globally with npm install -g gulp
.
gulp build
constructor()
Create a new emitter using the class' constructor. It accepts no arguments.
var {EventEmitter} = require('fbemitter');
var emitter = new EventEmitter();
addListener(eventType, callback)
Register a specific callback to be called on a particular event. A token is returned that can be used to remove the listener.
var token = emitter.addListener('change', (...args) => {
console.log(...args);
});
emitter.emit('change', 10); // 10 is logged
token.remove();
emitter.emit('change', 10); // nothing is logged
once(eventType, callback)
Similar to addListener()
but the callback is removed after it is invoked once. A token is returned that can be used to remove the listener.
var token = emitter.once('change', (...args) => {
console.log(...args);
});
emitter.emit('change', 10); // 10 is logged
emitter.emit('change', 10); // nothing is logged
removeAllListeners(eventType)
Removes all of the registered listeners. eventType
is optional, if provided only listeners for that event type are removed.
var token = emitter.addListener('change', (...args) => {
console.log(...args);
});
emitter.removeAllListeners();
emitter.emit('change', 10); // nothing is logged
listeners(eventType)
Return an array of listeners that are currently registered for the given event type.
emit(eventType, ...args)
Emits an event of the given type with the given data. All callbacks that are listening to the particular event type will be notified.
var token = emitter.addListener('change', (...args) => {
console.log(...args);
});
emitter.emit('change', 10); // 10 is logged
__emitToSubscription(subscription, eventType, ...args)
It is reasonable to extend EventEmitter
in order to inject some custom logic that you want to do on every callback that is called during an emit, such as logging, or setting up error boundaries. __emitToSubscription()
is exposed to make this possible.
class MyEventEmitter extends EventEmitter {
__emitToSubscription(subscription, eventType) {
var args = Array.prototype.slice.call(arguments, 2);
var start = Date.now();
subscription.listener.apply(subscription.context, args);
var time = Date.now() - start;
MyLoggingUtility.log('callback-time', {eventType, time});
}
}
And then you can create instances of MyEventEmitter
and use it like a standard EventEmitter
. If you just want to log on each emit and not on each callback called during an emit you can override emit()
instead of this method.
FAQs
Facebook's EventEmitter is a simple emitter implementation that prioritizes speed and simplicity. It is conceptually similar to other emitters like Node's EventEmitter, but the precise APIs differ. More complex abstractions like the event systems used on
The npm package fbemitter receives a total of 605,004 weekly downloads. As such, fbemitter popularity was classified as popular.
We found that fbemitter demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 3 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
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
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.