All files / src/events bus.ts

100% Statements 6/6
100% Branches 0/0
100% Functions 5/5
100% Lines 6/6

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68                                              143x   143x               429x                   246x                   1x                     1x      
/**
 * Event Emitter Bus - Adapter Implementation
 *
 * In-process EventBus implementation using Node.js EventEmitter.
 * This is the ADAPTER for the EventBus PORT.
 *
 * Can be swapped for RabbitMQ, Kafka, WebSocket, etc. later
 * without changing consumers.
 */
 
import { EventEmitter } from 'node:events';
import type { DomainEvent, EventBus } from './types.js';
 
/**
 * EventEmitter-based implementation of the EventBus interface.
 *
 * Wraps Node.js EventEmitter to provide type-safe event handling
 * that conforms to the EventBus port interface.
 */
export class EventEmitterBus implements EventBus {
  private emitter: EventEmitter;
 
  constructor() {
    this.emitter = new EventEmitter();
    // Allow more listeners for complex systems
    this.emitter.setMaxListeners(100);
  }
 
  /**
   * Emit an event to all subscribed handlers.
   * The event's type property determines which handlers receive it.
   */
  emit<T extends DomainEvent>(event: T): void {
    this.emitter.emit(event.type, event);
  }
 
  /**
   * Subscribe to events of a specific type.
   */
  on<T extends DomainEvent>(
    eventType: T['type'],
    handler: (event: T) => void
  ): void {
    this.emitter.on(eventType, handler);
  }
 
  /**
   * Unsubscribe from events of a specific type.
   */
  off<T extends DomainEvent>(
    eventType: T['type'],
    handler: (event: T) => void
  ): void {
    this.emitter.off(eventType, handler);
  }
 
  /**
   * Subscribe to a single occurrence of an event type.
   * Handler is automatically removed after first invocation.
   */
  once<T extends DomainEvent>(
    eventType: T['type'],
    handler: (event: T) => void
  ): void {
    this.emitter.once(eventType, handler);
  }
}