PitchHut logo
better-event
An isomorphic event emitter with type safety.
Pitch

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.

Description

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.

Key Features

  • Type-Safe Event Handling: Create event emitters with confidence, thanks to type-checking during event listener registration and emission.
  • Asynchronous Emission: Emit events that can be handled asynchronously, enabling non-blocking operations within applications.
  • Listener Registration: Register event listeners at the time of initialization using the createEventEmitter() function.
  • Dynamic Listener Management: Easily disable event listeners when no longer needed, improving application performance and resource management.

Quick Start

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'

API Overview

createEventEmitter(options)

  • Options: Accepts an object to define event handlers and other configurations.
  • Example of the on option:
    const emitter = createEventEmitter({
      on: {
        event: {
          handler: async (data: string) => {
            console.log(data);
          },
        },
      },
    });
    

Emit an Event

Use the emitter.emit(eventKey, data) method to trigger events:

await emitter.emit('event');
// => hello world

Disable an Event Listener

The emitter.disable(eventKey) method enables developers to prevent an event listener from being triggered:

emitter.disable('event');
await emitter.emit('event');
// nothing happens

Debugging

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.

0 comments

No comments yet.

Sign in to be the first to comment.