A lightweight and flexible event emitter library for JavaScript applications.
npm install @muvon/event
import EventEmitter from '@muvon/event';
// Subscribe to a specific event
const unsubscribe = EventEmitter.on('userLoggedIn', (event, data, source) => {
console.log(`User ${data.username} logged in`);
});
// Emit an event
EventEmitter.send('userLoggedIn', { username: 'john_doe' });
// Unsubscribe from the event
unsubscribe();
// Subscribe to all events
EventEmitter.all((event, data, source) => {
console.log(`Event ${event} was triggered`);
});
Subscribe to a specific event.
event
: String - The name of the event to subscribe to.callback
: Function - The function to be called when the event is emitted.- Returns: Function - A function that can be called to unsubscribe from the event.
Unsubscribe from all events for a specific callback.
callback
: Function - The callback function to unsubscribe.
Subscribe to all events.
callback
: Function - The function to be called for all events.- Returns: Function - A function that can be called to unsubscribe from all events.
Emit an event.
event
: String - The name of the event to emit.data
: Any - The data to pass to the event subscribers.source
: Any - The source of the event (optional).
- Lightweight and dependency-free
- Support for event namespacing
- Easy subscription and unsubscription
- Ability to listen to all events
MIT License
Contributions are welcome! Please feel free to submit a Pull Request.