Skip to content

Commit

Permalink
Adding in IEventEmitter
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredwray committed Oct 15, 2024
1 parent ba6787c commit fbcebf9
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 16 deletions.
18 changes: 9 additions & 9 deletions src/event-emitter-type.ts → src/event-emitter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
type EventEmitterType = {
export type IEventEmitter = {
/**
* Registers a listener for the specified event.
*
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand All @@ -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;
};
24 changes: 17 additions & 7 deletions src/eventified.ts
Original file line number Diff line number Diff line change
@@ -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 {

Check failure on line 5 in src/eventified.ts

View workflow job for this annotation

GitHub Actions / build (20)

Class 'Eventified' incorrectly implements interface 'IEventEmitter'.

Check failure on line 5 in src/eventified.ts

View workflow job for this annotation

GitHub Actions / build (20)

Class 'Eventified' incorrectly implements interface 'IEventEmitter'.

Check failure on line 5 in src/eventified.ts

View workflow job for this annotation

GitHub Actions / build (22)

Class 'Eventified' incorrectly implements interface 'IEventEmitter'.
_eventListeners: Map<string, EventListener[]>;
_maxListeners: number;

Expand All @@ -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;

Check failure on line 21 in src/eventified.ts

View workflow job for this annotation

GitHub Actions / build (20)

Type 'this' is not assignable to type 'IEventEmitter'.

Check failure on line 21 in src/eventified.ts

View workflow job for this annotation

GitHub Actions / build (20)

Type 'this' is not assignable to type 'IEventEmitter'.

Check failure on line 21 in src/eventified.ts

View workflow job for this annotation

GitHub Actions / build (22)

Type 'this' is not assignable to type 'IEventEmitter'.
}

public on(event: string, listener: EventListener): void {
public on(event: string, listener: EventListener): IEventEmitter {

Check failure on line 24 in src/eventified.ts

View workflow job for this annotation

GitHub Actions / build (20)

A function whose declared type is neither 'undefined', 'void', nor 'any' must return a value.

Check failure on line 24 in src/eventified.ts

View workflow job for this annotation

GitHub Actions / build (20)

A function whose declared type is neither 'undefined', 'void', nor 'any' must return a value.

Check failure on line 24 in src/eventified.ts

View workflow job for this annotation

GitHub Actions / build (22)

A function whose declared type is neither 'undefined', 'void', nor 'any' must return a value.
if (!this._eventListeners.has(event)) {
this._eventListeners.set(event, []);
}
Expand All @@ -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;

Check failure on line 43 in src/eventified.ts

View workflow job for this annotation

GitHub Actions / build (20)

Type 'this' is not assignable to type 'IEventEmitter'.

Check failure on line 43 in src/eventified.ts

View workflow job for this annotation

GitHub Actions / build (20)

Type 'this' is not assignable to type 'IEventEmitter'.

Check failure on line 43 in src/eventified.ts

View workflow job for this annotation

GitHub Actions / build (22)

Type 'this' is not assignable to type 'IEventEmitter'.
}

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) {
Expand All @@ -49,10 +53,12 @@ export class Eventified {
if (listeners.length === 0) {
this._eventListeners.delete(event);
}

return this;

Check failure on line 57 in src/eventified.ts

View workflow job for this annotation

GitHub Actions / build (20)

Type 'this' is not assignable to type 'IEventEmitter'.

Check failure on line 57 in src/eventified.ts

View workflow job for this annotation

GitHub Actions / build (20)

Type 'this' is not assignable to type 'IEventEmitter'.

Check failure on line 57 in src/eventified.ts

View workflow job for this annotation

GitHub Actions / build (22)

Type 'this' is not assignable to type 'IEventEmitter'.
}

// 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) {
Expand All @@ -61,6 +67,8 @@ export class Eventified {
listener(...arguments_);
}
}

return true;
}

// Get all listeners for a specific event
Expand All @@ -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;

Check failure on line 87 in src/eventified.ts

View workflow job for this annotation

GitHub Actions / build (20)

Type 'this' is not assignable to type 'IEventEmitter'.

Check failure on line 87 in src/eventified.ts

View workflow job for this annotation

GitHub Actions / build (20)

Type 'this' is not assignable to type 'IEventEmitter'.

Check failure on line 87 in src/eventified.ts

View workflow job for this annotation

GitHub Actions / build (22)

Type 'this' is not assignable to type 'IEventEmitter'.
}

// Set the maximum number of listeners for a single event
Expand Down

0 comments on commit fbcebf9

Please sign in to comment.