emitevent
v1.1.0
Published
High-performance Event emitter similar to Node.js, written in TypeScript
Maintainers
Readme
EmittEvent
A high-performance, lightweight EventEmitter for modern JavaScript environments, strictly modeled after the Node.js EventEmitter API.
Features
- Lightweight: Zero dependencies, minimal footprint.
- Node.js Compatible: Implements the core
EventEmittermethods. - Performant: Optimized internal storage using
Object.create(null)to avoid prototype overhead. - TypeScript Support: Full type definitions included.
Installation
npm install emitteventUsage
import EventEmitter from 'emittevent';
const em = new EventEmitter();
em.on('hello', (name) => {
console.log(`Hello, ${name}!`);
});
em.emit('hello', 'World'); // "Hello, World!"API Documentation
on(eventName, listener) / addListener(eventName, listener)
Adds the listener function to the end of the listeners array for the event named eventName.
once(eventName, listener)
Adds a one-time listener function for the event named eventName. The next time eventName is triggered, this listener is removed and then invoked.
emit(eventName, ...args)
Synchronously calls each of the listeners registered for the event named eventName, in the order they were registered, passing the supplied arguments to each.
- Returns:
trueif the event had listeners,falseotherwise.
removeListener(eventName, listener) / off(eventName, listener)
Removes the specified listener from the listener array for the event named eventName.
removeAllListeners([eventName])
Removes all listeners, or those of the specified eventName.
eventNames()
Returns an array listing the events for which the emitter has registered listeners.
listeners(eventName)
Returns a copy of the array of listeners for the event named eventName.
listenerCount(eventName)
Returns the number of listeners listening to the event named eventName.
License
MIT
