diff --git a/src/event-emitter-type.ts b/src/event-emitter.ts similarity index 93% rename from src/event-emitter-type.ts rename to src/event-emitter.ts index 1fd3a94..e1ae346 100644 --- a/src/event-emitter-type.ts +++ b/src/event-emitter.ts @@ -1,4 +1,4 @@ -type EventEmitterType = { +export type IEventEmitter = { /** * Registers a listener for the specified event. * @@ -11,7 +11,7 @@ type EventEmitterType = { * console.log(message); * }); */ - on(eventName: string | symbol, listener: (...arguments_: any[]) => void): EventEmitterType; + on(eventName: string | symbol, listener: (...arguments_: any[]) => void): IEventEmitter; /** * Alias for `on`. Registers a listener for the specified event. @@ -20,7 +20,7 @@ type EventEmitterType = { * @param listener - A callback function that will be invoked when the event is emitted. * @returns The current instance of EventEmitter for method chaining. */ - addListener(eventName: string | symbol, listener: (...arguments_: any[]) => void): EventEmitterType; + addListener(eventName: string | symbol, listener: (...arguments_: any[]) => void): IEventEmitter; /** * Registers a one-time listener for the specified event. The listener is removed after it is called once. @@ -34,7 +34,7 @@ type EventEmitterType = { * console.log('The connection was closed.'); * }); */ - once(eventName: string | symbol, listener: (...arguments_: any[]) => void): EventEmitterType; + once(eventName: string | symbol, listener: (...arguments_: any[]) => void): IEventEmitter; /** * Removes a previously registered listener for the specified event. @@ -46,7 +46,7 @@ type EventEmitterType = { * @example * emitter.off('data', myListener); */ - off(eventName: string | symbol, listener: (...arguments_: any[]) => void): EventEmitterType; + off(eventName: string | symbol, listener: (...arguments_: any[]) => void): IEventEmitter; /** * Alias for `off`. Removes a previously registered listener for the specified event. @@ -55,7 +55,7 @@ type EventEmitterType = { * @param listener - The specific callback function to remove. * @returns The current instance of EventEmitter for method chaining. */ - removeListener(eventName: string | symbol, listener: (...arguments_: any[]) => void): EventEmitterType; + removeListener(eventName: string | symbol, listener: (...arguments_: any[]) => void): IEventEmitter; /** * Emits the specified event, invoking all registered listeners with the provided arguments. @@ -90,7 +90,7 @@ type EventEmitterType = { * @example * emitter.removeAllListeners('data'); */ - removeAllListeners(eventName?: string | symbol): EventEmitterType; + removeAllListeners(eventName?: string | symbol): IEventEmitter; /** * Returns an array of event names for which listeners have been registered. @@ -138,7 +138,7 @@ type EventEmitterType = { * console.log('This will run first.'); * }); */ - prependListener(eventName: string | symbol, listener: (...arguments_: any[]) => void): EventEmitterType; + prependListener(eventName: string | symbol, listener: (...arguments_: any[]) => void): IEventEmitter; /** * Adds a one-time listener to the beginning of the listeners array for the specified event. @@ -152,5 +152,5 @@ type EventEmitterType = { * console.log('This will run first and only once.'); * }); */ - prependOnceListener(eventName: string | symbol, listener: (...arguments_: any[]) => void): EventEmitterType; + prependOnceListener(eventName: string | symbol, listener: (...arguments_: any[]) => void): IEventEmitter; }; diff --git a/src/eventified.ts b/src/eventified.ts index 08d7d8f..a08ed84 100644 --- a/src/eventified.ts +++ b/src/eventified.ts @@ -1,6 +1,8 @@ +import { type IEventEmitter } from './event-emitter.js'; + export type EventListener = (...arguments_: any[]) => void; -export class Eventified { +export class Eventified implements IEventEmitter { _eventListeners: Map; _maxListeners: number; @@ -14,11 +16,12 @@ export class Eventified { } // Add an event listener - public addListener(event: string, listener: EventListener): void { + public addListener(event: string, listener: EventListener): IEventEmitter { this.on(event, listener); + return this; } - public on(event: string, listener: EventListener): void { + public on(event: string, listener: EventListener): IEventEmitter { if (!this._eventListeners.has(event)) { this._eventListeners.set(event, []); } @@ -35,11 +38,12 @@ export class Eventified { } // Remove an event listener - public removeListener(event: string, listener: EventListener): void { + public removeListener(event: string, listener: EventListener): IEventEmitter { this.off(event, listener); + return this; } - public off(event: string, listener: EventListener): void { + public off(event: string, listener: EventListener): IEventEmitter { const listeners = this._eventListeners.get(event) ?? []; const index = listeners.indexOf(listener); if (index > -1) { @@ -49,10 +53,12 @@ export class Eventified { if (listeners.length === 0) { this._eventListeners.delete(event); } + + return this; } // Emit an event - public emit(event: string, ...arguments_: any[]): void { + public emit(event: string, ...arguments_: any[]): boolean { const listeners = this._eventListeners.get(event); if (listeners && listeners.length > 0) { @@ -61,6 +67,8 @@ export class Eventified { listener(...arguments_); } } + + return true; } // Get all listeners for a specific event @@ -69,12 +77,14 @@ export class Eventified { } // Remove all listeners for a specific event - public removeAllListeners(event?: string): void { + public removeAllListeners(event?: string): IEventEmitter { if (event) { this._eventListeners.delete(event); } else { this._eventListeners.clear(); } + + return this; } // Set the maximum number of listeners for a single event