Skip to content

@egain/ai-agent-sdk API Reference - v0.1.1 / EventEmitter

Class: EventEmitter<T>

Type-safe event emitter implementation.

Compatible with both browser and Node.js environments. Provides a foundation for event-driven communication throughout the SDK.

Example

typescript
import { EventEmitter } from "@egain/ai-agent-sdk";

interface MyEvents {
  data: { value: number };
  error: { message: string };
}

class MyService extends EventEmitter<MyEvents> {
  doSomething() {
    this.emit("data", { value: 42 });
  }
}

const service = new MyService();
service.on("data", (event) => {
  console.log(event.value); // Typed as number
});

Type parameters

NameTypeDescription
Textends EventMap = EventMapEvent map defining event names and their data types

Hierarchy

Table of contents

Constructors

Methods

Constructors

constructor

new EventEmitter<T>(): EventEmitter<T>

Type parameters

NameType
Textends EventMap = EventMap

Returns

EventEmitter<T>

Methods

on

on<K>(event, handler): this

Register an event handler.

The handler will be called every time the event is emitted.

Type parameters

NameType
Kextends string | number | symbol

Parameters

NameTypeDescription
eventKThe event name to listen for
handlerEventHandler<T[K]>The function to call when the event is emitted

Returns

this

this for method chaining

Example

typescript
agent.on("message", (event) => {
  console.log("Received:", event.payload);
});

Defined in

core/events/EventEmitter.ts:64


once

once<K>(event, handler): this

Register a one-time event handler.

The handler will be called only once, then automatically removed.

Type parameters

NameType
Kextends string | number | symbol

Parameters

NameTypeDescription
eventKThe event name to listen for
handlerEventHandler<T[K]>The function to call when the event is emitted

Returns

this

this for method chaining

Example

typescript
agent.once("connected", () => {
  console.log("First connection established!");
});

Defined in

core/events/EventEmitter.ts:88


off

off<K>(event, handler?): this

Remove an event handler.

If no handler is specified, removes all handlers for the event.

Type parameters

NameType
Kextends string | number | symbol

Parameters

NameTypeDescription
eventKThe event name
handler?EventHandler<T[K]>The specific handler to remove (optional)

Returns

this

this for method chaining

Example

typescript
const handler = (event) => console.log(event);
agent.on("message", handler);
agent.off("message", handler);

Example

typescript
agent.off("message");

Defined in

core/events/EventEmitter.ts:117


removeAllListeners

removeAllListeners<K>(event?): this

Remove all event handlers

Type parameters

NameType
Kextends string | number | symbol

Parameters

NameType
event?K

Returns

this

Defined in

core/events/EventEmitter.ts:180


listenerCount

listenerCount<K>(event): number

Get the number of listeners for an event

Type parameters

NameType
Kextends string | number | symbol

Parameters

NameType
eventK

Returns

number

Defined in

core/events/EventEmitter.ts:194

Released under the MIT License.