Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Openai Openai node EventEmitter Class

From Leeroopedia
Knowledge Sources
Domains SDK, Event System
Last Updated 2026-02-15 12:00 GMT

Overview

EventEmitter is a generic, type-safe event emitter class that provides the foundational pub/sub pattern used throughout the SDK's streaming and runner infrastructure.

Description

The EventEmitter<EventTypes> class is parameterized by an EventTypes record that maps event names to their listener signatures. This approach provides full TypeScript type safety, ensuring that listeners registered for a given event have the correct parameter types and that emitted events pass the correct argument types. The class uses private #listeners storage to maintain an array of listener entries per event.

The class exposes four primary public methods: on() to add a persistent listener, off() to remove a specific listener instance, once() to add a one-time listener that is automatically removed after its first invocation, and emitted() which returns a Promise that resolves the next time a given event fires. The protected _emit() method is used by subclasses to fire events, and _hasListener() checks whether any listeners are registered for a given event.

This class serves as the base event system for the AbstractChatCompletionRunner and other event-driven components in the SDK. Unlike Node.js's built-in EventEmitter, this implementation is platform-agnostic and works in browsers, Deno, Bun, and other JavaScript runtimes.

Usage

Use EventEmitter as a base class when building event-driven components within the SDK ecosystem. End users typically interact with it indirectly through runner and stream classes that expose on(), off(), once(), and emitted() methods.

Code Reference

Source Location

Signature

export type EventParameters<Events, EventType extends keyof Events> = {
  [Event in EventType]: EventListener<Events, EventType> extends (...args: infer P) => any ? P : never;
}[EventType];

export class EventEmitter<EventTypes extends Record<string, (...args: any) => any>> {
  on<Event extends keyof EventTypes>(event: Event, listener: EventTypes[Event]): this;
  off<Event extends keyof EventTypes>(event: Event, listener: EventTypes[Event]): this;
  once<Event extends keyof EventTypes>(event: Event, listener: EventTypes[Event]): this;
  emitted<Event extends keyof EventTypes>(event: Event): Promise<...>;
  protected _emit<Event extends keyof EventTypes>(event: Event, ...args: EventParameters<EventTypes, Event>): void;
  protected _hasListener(event: keyof EventTypes): boolean;
}

Import

import { EventEmitter } from 'openai/lib/EventEmitter';

I/O Contract

Inputs

Name Type Required Description
event keyof EventTypes Yes The event name to listen for, emit, or query.
listener EventTypes[Event] Yes (for on/off/once) The callback function to register or remove. Must match the signature defined in the EventTypes type parameter.

Outputs

Name Type Description
this EventEmitter The on(), off(), and once() methods return this for method chaining.
promise Promise The emitted() method returns a Promise that resolves with the event's parameters when the event next fires.

Usage Examples

import { EventEmitter } from 'openai/lib/EventEmitter';

interface MyEvents {
  data: (payload: string) => void;
  error: (err: Error) => void;
  end: () => void;
}

class MyStream extends EventEmitter<MyEvents> {
  start() {
    this._emit('data', 'hello');
    this._emit('end');
  }
}

const stream = new MyStream();

stream.on('data', (payload) => {
  console.log('Received:', payload);
});

// Promise-based usage
const endPromise = stream.emitted('end');
stream.start();
await endPromise; // resolves when 'end' is emitted

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment