Skip to content

Commit

Permalink
adding in additional iEventEmitter
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredwray committed Oct 15, 2024
1 parent cafca7e commit 24608e0
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/event-emitter.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// eslint-disable-next-line @typescript-eslint/naming-convention
export type IEventEmitter = {
/**
* Registers a listener for the specified event.
Expand Down
46 changes: 40 additions & 6 deletions src/eventified.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,59 @@
import { type IEventEmitter } from './event-emitter.js';
import {type IEventEmitter} from './event-emitter.js';

export type EventListener = (...arguments_: any[]) => void;

export class Eventified implements IEventEmitter {
_eventListeners: Map<string, EventListener[]>;
_eventListeners: Map<string | symbol, EventListener[]>;
_maxListeners: number;

constructor() {
this._eventListeners = new Map();
this._eventListeners = new Map<string | symbol, EventListener[]>();
this._maxListeners = 100; // Default maximum number of listeners
}

once(eventName: string | symbol, listener: EventListener): IEventEmitter {
const onceListener: EventListener = (...arguments_: any[]) => {
this.off(eventName as string, onceListener);

Check warning on line 16 in src/eventified.ts

View check run for this annotation

Codecov / codecov/patch

src/eventified.ts#L15-L16

Added lines #L15 - L16 were not covered by tests
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
listener(...arguments_);
};

Check warning on line 19 in src/eventified.ts

View check run for this annotation

Codecov / codecov/patch

src/eventified.ts#L18-L19

Added lines #L18 - L19 were not covered by tests

this.on(eventName as string, onceListener);
return this;
}

Check warning on line 23 in src/eventified.ts

View check run for this annotation

Codecov / codecov/patch

src/eventified.ts#L21-L23

Added lines #L21 - L23 were not covered by tests

listenerCount(eventName: string | symbol): number {
const listeners = this._eventListeners.get(eventName as string);
return listeners ? listeners.length : 0;
}

Check warning on line 28 in src/eventified.ts

View check run for this annotation

Codecov / codecov/patch

src/eventified.ts#L26-L28

Added lines #L26 - L28 were not covered by tests

eventNames(): Array<string | symbol> {
throw new Error('Method not implemented.');
}

Check warning on line 32 in src/eventified.ts

View check run for this annotation

Codecov / codecov/patch

src/eventified.ts#L31-L32

Added lines #L31 - L32 were not covered by tests

rawListeners(eventName: string | symbol): EventListener[] {
throw new Error('Method not implemented.');
}

Check warning on line 36 in src/eventified.ts

View check run for this annotation

Codecov / codecov/patch

src/eventified.ts#L35-L36

Added lines #L35 - L36 were not covered by tests

prependListener(eventName: string | symbol, listener: EventListener): IEventEmitter {
throw new Error('Method not implemented.');
}

Check warning on line 40 in src/eventified.ts

View check run for this annotation

Codecov / codecov/patch

src/eventified.ts#L39-L40

Added lines #L39 - L40 were not covered by tests

prependOnceListener(eventName: string | symbol, listener: EventListener): IEventEmitter {
throw new Error('Method not implemented.');
}

Check warning on line 44 in src/eventified.ts

View check run for this annotation

Codecov / codecov/patch

src/eventified.ts#L43-L44

Added lines #L43 - L44 were not covered by tests

public maxListeners(): number {
return this._maxListeners;
}

// Add an event listener
public addListener(event: string, listener: EventListener): IEventEmitter {
public addListener(event: string | symbol, listener: EventListener): IEventEmitter {
this.on(event, listener);
return this;
}

public on(event: string, listener: EventListener): IEventEmitter {
public on(event: string | symbol, listener: EventListener): IEventEmitter {
if (!this._eventListeners.has(event)) {
this._eventListeners.set(event, []);
}
Expand All @@ -30,11 +62,13 @@ export class Eventified implements IEventEmitter {

if (listeners) {
if (listeners.length >= this._maxListeners) {
console.warn(`MaxListenersExceededWarning: Possible event memory leak detected. ${listeners.length + 1} ${event} listeners added. Use setMaxListeners() to increase limit.`);
console.warn(`MaxListenersExceededWarning: Possible event memory leak detected. ${listeners.length + 1} ${event as string} listeners added. Use setMaxListeners() to increase limit.`);
}

listeners.push(listener);
}

return this;
}

// Remove an event listener
Expand Down

0 comments on commit 24608e0

Please sign in to comment.