Skip to content

EventEmitter

A type-safe event emitter with support for wildcard listeners and both synchronous and asynchronous event dispatch.

Defined in: src/core/events/EventEmitter.ts:66

A type-safe event emitter with support for wildcard listeners and both synchronous and asynchronous event dispatch.

Remarks

EventEmitter is the internal event bus used by CompositeVoice to propagate pipeline events (transcription, LLM, TTS, agent lifecycle, and audio events) to consumer code. Key features include:

  • Type safety: Listeners are typed according to the event they subscribe to, so TypeScript enforces correct event payloads at compile time.
  • Wildcard support: Subscribing to '*' receives every event, useful for logging or debugging.
  • One-time listeners: The once() method automatically removes the listener after the first invocation.
  • Memory leak detection: A configurable maximum listener count per event emits a console warning when exceeded, helping catch subscription leaks.
  • Dual emission modes: emit() awaits all listeners (including async ones), while emitSync() fires listeners without awaiting, suitable for hot paths.

Examples

const emitter = new EventEmitter();

// Typed listener -- TypeScript knows the event shape
emitter.on('transcription.final', ({ text, confidence }) => {
  console.log(`Final transcript: ${text} (confidence: ${confidence})`);
});

// Wildcard listener for logging
emitter.on('*', (event) => {
  console.log(`[${event.type}]`, event);
});
const unsubscribe = emitter.once('agent.ready', () => {
  console.log('Agent is ready');
});

// If you need to cancel before it fires:
unsubscribe();

See

Constructors

Constructor

new EventEmitter(maxListeners?): EventEmitter;

Defined in: src/core/events/EventEmitter.ts:77

Creates a new EventEmitter instance.

Parameters

ParameterTypeDefault valueDescription
maxListenersnumber100The maximum number of listeners allowed per event type before a memory leak warning is logged to the console. Defaults to 100.

Returns

EventEmitter

Methods

emit()

emit<T>(event): Promise<void>;

Defined in: src/core/events/EventEmitter.ts:206

Emit an event to all registered listeners

Type Parameters

Type Parameter
T extends CompositeVoiceEvent

Parameters

ParameterTypeDescription
eventTEvent object to emit

Returns

Promise<void>


emitSync()

emitSync<T>(event): void;

Defined in: src/core/events/EventEmitter.ts:228

Emit an event synchronously (doesn’t wait for async listeners)

Type Parameters

Type Parameter
T extends CompositeVoiceEvent

Parameters

ParameterTypeDescription
eventTEvent object to emit

Returns

void


eventNames()

eventNames(): (
  | "transcription.start"
  | "transcription.interim"
  | "transcription.final"
  | "transcription.speechFinal"
  | "transcription.preflight"
  | "transcription.error"
  | "llm.start"
  | "llm.chunk"
  | "llm.complete"
  | "llm.error"
  | "tts.start"
  | "tts.audio"
  | "tts.metadata"
  | "tts.complete"
  | "tts.error"
  | "agent.ready"
  | "agent.stateChange"
  | "agent.error"
  | "audio.capture.start"
  | "audio.capture.stop"
  | "audio.capture.error"
  | "audio.playback.start"
  | "audio.playback.end"
  | "audio.playback.error"
  | "queue.overflow"
  | "queue.stats"
  | "*")[];

Defined in: src/core/events/EventEmitter.ts:258

Get all event types that have listeners

Returns

( | "transcription.start" | "transcription.interim" | "transcription.final" | "transcription.speechFinal" | "transcription.preflight" | "transcription.error" | "llm.start" | "llm.chunk" | "llm.complete" | "llm.error" | "tts.start" | "tts.audio" | "tts.metadata" | "tts.complete" | "tts.error" | "agent.ready" | "agent.stateChange" | "agent.error" | "audio.capture.start" | "audio.capture.stop" | "audio.capture.error" | "audio.playback.start" | "audio.playback.end" | "audio.playback.error" | "queue.overflow" | "queue.stats" | "*")[]

Array of event types


getMaxListeners()

getMaxListeners(): number;

Defined in: src/core/events/EventEmitter.ts:274

Get the maximum number of listeners per event

Returns

number

Maximum number of listeners


listenerCount()

listenerCount(event): number;

Defined in: src/core/events/EventEmitter.ts:249

Get the number of listeners for an event

Parameters

ParameterTypeDescription
event| "transcription.start" | "transcription.interim" | "transcription.final" | "transcription.speechFinal" | "transcription.preflight" | "transcription.error" | "llm.start" | "llm.chunk" | "llm.complete" | "llm.error" | "tts.start" | "tts.audio" | "tts.metadata" | "tts.complete" | "tts.error" | "agent.ready" | "agent.stateChange" | "agent.error" | "audio.capture.start" | "audio.capture.stop" | "audio.capture.error" | "audio.playback.start" | "audio.playback.end" | "audio.playback.error" | "queue.overflow" | "queue.stats" | "*"Event type

Returns

number

Number of listeners


off()

off<T>(event, listener): void;

Defined in: src/core/events/EventEmitter.ts:177

Remove an event listener

Type Parameters

Type Parameter
T extends | "transcription.start" | "transcription.interim" | "transcription.final" | "transcription.speechFinal" | "transcription.preflight" | "transcription.error" | "llm.start" | "llm.chunk" | "llm.complete" | "llm.error" | "tts.start" | "tts.audio" | "tts.metadata" | "tts.complete" | "tts.error" | "agent.ready" | "agent.stateChange" | "agent.error" | "audio.capture.start" | "audio.capture.stop" | "audio.capture.error" | "audio.playback.start" | "audio.playback.end" | "audio.playback.error" | "queue.overflow" | "queue.stats"

Parameters

ParameterTypeDescription
event"*" | TEvent type
listenerT extends "*" ? EventListener : EventListenerMap[T]Listener function to remove

Returns

void


on()

on<T>(event, listener): () => void;

Defined in: src/core/events/EventEmitter.ts:110

Registers an event listener for the specified event type.

Type Parameters

Type ParameterDescription
T extends | "transcription.start" | "transcription.interim" | "transcription.final" | "transcription.speechFinal" | "transcription.preflight" | "transcription.error" | "llm.start" | "llm.chunk" | "llm.complete" | "llm.error" | "tts.start" | "tts.audio" | "tts.metadata" | "tts.complete" | "tts.error" | "agent.ready" | "agent.stateChange" | "agent.error" | "audio.capture.start" | "audio.capture.stop" | "audio.capture.error" | "audio.playback.start" | "audio.playback.end" | "audio.playback.error" | "queue.overflow" | "queue.stats"The event type string, inferred from the event argument.

Parameters

ParameterTypeDescription
event"*" | TThe event type to listen for (e.g., 'llm.chunk'), or '*' to receive all events.
listenerT extends "*" ? EventListener : EventListenerMap[T]The callback function invoked when the event fires.

Returns

A function that, when called, removes this listener (equivalent to calling off()).

(): void;
Returns

void

Remarks

If the number of listeners for the given event exceeds the configured maximum (see setMaxListeners()), a warning is logged to the console to help detect memory leaks. The listener is stored in a Set, so adding the same function reference twice for the same event is a no-op.

Example

const unsubscribe = emitter.on('tts.complete', () => {
  console.log('TTS playback finished');
});

// Later, remove the listener
unsubscribe();

once()

once<T>(event, listener): () => void;

Defined in: src/core/events/EventEmitter.ts:160

Registers a one-time event listener that automatically unsubscribes after its first invocation.

Type Parameters

Type ParameterDescription
T extends | "transcription.start" | "transcription.interim" | "transcription.final" | "transcription.speechFinal" | "transcription.preflight" | "transcription.error" | "llm.start" | "llm.chunk" | "llm.complete" | "llm.error" | "tts.start" | "tts.audio" | "tts.metadata" | "tts.complete" | "tts.error" | "agent.ready" | "agent.stateChange" | "agent.error" | "audio.capture.start" | "audio.capture.stop" | "audio.capture.error" | "audio.playback.start" | "audio.playback.end" | "audio.playback.error" | "queue.overflow" | "queue.stats"The event type string, inferred from the event argument.

Parameters

ParameterTypeDescription
eventTThe event type to listen for.
listenerEventListenerMap[T]The callback function invoked once when the event fires.

Returns

A function that, when called, removes this listener before it has a chance to fire.

(): void;
Returns

void

Remarks

Internally, this wraps the provided listener in a function that calls off() before invoking the original callback. The returned unsubscribe function can be called to cancel the listener before it fires.

Example

emitter.once('agent.ready', () => {
  console.log('This will only fire once');
});

removeAllListeners()

removeAllListeners(event?): void;

Defined in: src/core/events/EventEmitter.ts:194

Remove all listeners for an event, or all listeners if no event specified

Parameters

ParameterTypeDescription
event?| "transcription.start" | "transcription.interim" | "transcription.final" | "transcription.speechFinal" | "transcription.preflight" | "transcription.error" | "llm.start" | "llm.chunk" | "llm.complete" | "llm.error" | "tts.start" | "tts.audio" | "tts.metadata" | "tts.complete" | "tts.error" | "agent.ready" | "agent.stateChange" | "agent.error" | "audio.capture.start" | "audio.capture.stop" | "audio.capture.error" | "audio.playback.start" | "audio.playback.end" | "audio.playback.error" | "queue.overflow" | "queue.stats" | "*"Optional event type to remove listeners for

Returns

void


setMaxListeners()

setMaxListeners(n): void;

Defined in: src/core/events/EventEmitter.ts:266

Set the maximum number of listeners per event

Parameters

ParameterTypeDescription
nnumberMaximum number of listeners

Returns

void

© 2026 CompositeVoice. All rights reserved.

Font size
Contrast
Motion
Transparency