Better-event provides a robust event emitting solution with type safety, inspired by emittery. It allows asynchronous event emission, supports listener registration during initialization, and offers intuitive control over event listener management. Streamline your event-driven architecture with features like disabling listeners and debugging capabilities.
better-event is an advanced isomorphic event emitter designed for type-safe event handling, inspired by emittery. This package facilitates asynchronous event emission and listener management, making it a valuable tool for developers seeking a robust event handling solution.
createEventEmitter() function.To begin using better-event, an example of how to create a simple event emitter is provided:
import { createEventEmitter } from 'better-event';
const emitter = createEventEmitter({
on: {
event: {
handler: async (data: string) => {
console.log(data);
},
},
},
});
await emitter.emit('event', 'hello world');
// => 'hello world'
on option:
const emitter = createEventEmitter({
on: {
event: {
handler: async (data: string) => {
console.log(data);
},
},
},
});
Use the emitter.emit(eventKey, data) method to trigger events:
await emitter.emit('event');
// => hello world
The emitter.disable(eventKey) method enables developers to prevent an event listener from being triggered:
emitter.disable('event');
await emitter.emit('event');
// nothing happens
Enable debugging by providing a debug name that gets logged when events are emitted:
const emitter = createEventEmitter({
on: {
event: {
handler: async (data: string) => {
console.log(data);
},
},
},
debug: { name: 'event' },
});
With better-event, event-driven programming becomes more efficient, manageable, and type-safe, enhancing the overall development experience while minimizing bugs in asynchronous workflows.
No comments yet.
Sign in to be the first to comment.