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.
@socket.io/component-emitter
Advanced tools
@socket.io/component-emitter is a lightweight event emitter library that allows you to create and manage custom events. It is commonly used in applications where you need to implement an event-driven architecture, such as in real-time web applications.
Event Emission
This feature allows you to emit custom events and listen for them. In the code sample, an event named 'event' is emitted, and a listener logs a message to the console when the event occurs.
const Emitter = require('@socket.io/component-emitter');
const emitter = new Emitter();
emitter.on('event', function() {
console.log('An event occurred!');
});
emitter.emit('event');
Event Removal
This feature allows you to remove specific event listeners. In the code sample, the listener for the 'event' is removed before the event is emitted, so no message is logged to the console.
const Emitter = require('@socket.io/component-emitter');
const emitter = new Emitter();
function response() {
console.log('An event occurred!');
}
emitter.on('event', response);
emitter.off('event', response);
emitter.emit('event');
Once Event Listener
This feature allows you to add a listener that will be called at most once for a particular event. In the code sample, the listener logs a message only the first time the 'event' is emitted.
const Emitter = require('@socket.io/component-emitter');
const emitter = new Emitter();
emitter.once('event', function() {
console.log('This will only log once');
});
emitter.emit('event');
emitter.emit('event');
The 'events' package is the built-in Node.js module for handling events. It provides a similar event-driven architecture and is widely used in Node.js applications. Compared to @socket.io/component-emitter, 'events' is more feature-rich and is part of the Node.js core.
The 'eventemitter3' package is a high-performance event emitter for Node.js and the browser. It is similar to @socket.io/component-emitter in terms of functionality but is optimized for performance and has a smaller footprint.
The 'mitt' package is a tiny (~200 bytes) functional event emitter. It provides a minimal API for event handling and is suitable for use in both Node.js and browser environments. Compared to @socket.io/component-emitter, 'mitt' is more lightweight and has a simpler API.
Event emitter component.
$ component install component/emitter
The Emitter
may also be used as a mixin. For example
a "plain" object may become an emitter, or you may
extend an existing prototype.
As an Emitter
instance:
var Emitter = require('emitter');
var emitter = new Emitter;
emitter.emit('something');
As a mixin:
var Emitter = require('emitter');
var user = { name: 'tobi' };
Emitter(user);
user.emit('im a user');
As a prototype mixin:
var Emitter = require('emitter');
Emitter(User.prototype);
Register an event
handler fn
.
Register a single-shot event
handler fn
,
removed immediately after it is invoked the
first time.
event
and fn
to remove a listener.event
to remove all listeners on that event.Emit an event
with variable option args.
Return an array of callbacks, or an empty array.
Check if this emitter has event
handlers.
MIT
3.0.0 (2021-10-14)
// before
import Emitter from "@socket.io/component-emitter"
// after
import { Emitter } from "@socket.io/component-emitter"
exports
arguments
$
to support object prototype method names.repo
prop.addEventListener()
and .removeEventListener()
aliases.off()
support for removing all listenersthis._callbacks
initialization to prevent funky gotchaEmitter.call(this)
usage.listeners()
.has()
to .hasListeners()
.off()
with .once()
-registered callbacksFAQs
Event emitter
We found that @socket.io/component-emitter demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer 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.