
Security News
Critical Security Vulnerability in React Server Components
React disclosed a CVSS 10.0 RCE in React Server Components and is advising users to upgrade affected packages and frameworks to patched versions now.
@carry0987/event-emitter
Advanced tools
EventEmitter is a TypeScript library that provides a simple yet powerful event-handling mechanism. It allows you to define and manage events in your application.
EventEmitter is a TypeScript library that provides a simple yet powerful event-handling mechanism. It allows you to define and manage events in your application, supporting standard event operations such as on, off, emit, once, and more.
onceTo install the EventEmitter library, use the following command:
npm i @carry0987/event-emitter -D
First, import the EventEmitter class into your TypeScript file:
import { EventEmitter } from '@carry0987/event-emitter';
Define your event types for type safety. For example:
interface MyEvents {
greet: [string];
farewell: [string];
}
Create an instance of EventEmitter with your event types:
const emitter = new EventEmitter<MyEvents>();
Use the on method to add event listeners:
emitter.on('greet', (message) => {
console.log(message);
});
Emit events using the emit method:
emitter.emit('greet', 'Hello, world!');
Remove event listeners with the off method:
const greetListener = (message: string) => {
console.log(message);
};
emitter.on('greet', greetListener);
emitter.off('greet', greetListener);
Use the once method to add a listener that will be called only once:
emitter.once('farewell', (message) => {
console.log(message);
});
emitter.emit('farewell', 'Goodbye, world!'); // Listener will be called
emitter.emit('farewell', 'Goodbye again!'); // Listener will not be called
Add asynchronous event listeners using the on or once method:
emitter.on('greet', async (message) => {
await new Promise(resolve => setTimeout(resolve, 1000));
console.log(`Async Listener: ${message}`);
});
emitter.emit('greet', 'Hello, async world!').then(() => {
console.log('All async listeners have been called.');
});
on(event: EventName, listener: (...args: any[]) => void | Promise<void>): EventEmitterRegisters an event listener for the specified event.
off(event: EventName, listener: (...args: any[]) => void | Promise<void>): EventEmitterRemoves the specified event listener.
emit(event: EventName, ...args: any[]): Promise<boolean>Emits the specified event, calling all registered listeners with the provided arguments. Returns a promise that resolves to true if there were listeners, and false otherwise.
once(event: EventName, listener: (...args: any[]) => void | Promise<void>): EventEmitterRegisters a one-time event listener for the specified event. The listener will be called at most once after being added.
listeners(): { [event: string]: ((...args: any[]) => void | Promise<void>)[] }Returns an object containing all registered event listeners.
Contributions are welcome! Please open an issue or submit a pull request on GitHub.
This project is licensed under the MIT License. See the LICENSE file for details.
FAQs
EventEmitter is a TypeScript library that provides a simple yet powerful event-handling mechanism. It allows you to define and manage events in your application.
We found that @carry0987/event-emitter demonstrated a not healthy version release cadence and project activity because the last version was released 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
React disclosed a CVSS 10.0 RCE in React Server Components and is advising users to upgrade affected packages and frameworks to patched versions now.

Research
/Security News
We spotted a wave of auto-generated “elf-*” npm packages published every two minutes from new accounts, with simple malware variants and early takedowns underway.

Security News
TypeScript 6.0 will be the last JavaScript-based major release, as the project shifts to the TypeScript 7 native toolchain with major build speedups.