Skip to content

Commit

Permalink
adding in rawListeners
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredwray committed Oct 15, 2024
1 parent 6b17b31 commit 627f6eb
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/eventified.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,15 @@ export class Eventified implements IEventEmitter {
}

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

rawListeners(eventName: string | symbol): EventListener[] {
throw new Error('Method not implemented.');
rawListeners(eventName?: string | symbol): EventListener[] {
if (!eventName) {
return this.getAllListeners();
}

return this._eventListeners.get(eventName) ?? [];
}

prependListener(eventName: string | symbol, listener: EventListener): IEventEmitter {
Expand Down
25 changes: 25 additions & 0 deletions test/eventified.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,4 +161,29 @@ describe('Eventified', () => {
t.expect(emitter.listenerCount()).toBe(4);
t.expect(emitter.listenerCount('test-event')).toBe(2);
});

test('get event names', t => {
const emitter = new Eventified();
const listener = () => {};

emitter.on('test-event', listener);
emitter.on('test-event1', listener);
emitter.on('test-event2', listener);

t.expect(emitter.eventNames()).toEqual(['test-event', 'test-event1', 'test-event2']);
});

test('get raw listeners', t => {
const emitter = new Eventified();
const listener = () => {};

emitter.on('test-event', listener);
emitter.on('test-event', listener);
emitter.on('test-event1', listener);
emitter.on('test-event2', listener);

t.expect(emitter.rawListeners('test-event')).toEqual([listener, listener]);
t.expect(emitter.rawListeners('test-event1')).toEqual([listener]);
t.expect(emitter.rawListeners().length).toEqual(4);
});
});

0 comments on commit 627f6eb

Please sign in to comment.