Skip to content

Commit

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

prependListener(eventName: string | symbol, listener: EventListener): IEventEmitter {
throw new Error('Method not implemented.');
const listeners = this._eventListeners.get(eventName) ?? [];
listeners.unshift(listener);
this._eventListeners.set(eventName, listeners);
return this;
}

prependOnceListener(eventName: string | symbol, listener: EventListener): IEventEmitter {
throw new Error('Method not implemented.');
const onceListener: EventListener = (...arguments_: any[]) => {
this.off(eventName as string, onceListener);
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
listener(...arguments_);
};

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

public maxListeners(): number {
Expand Down
42 changes: 42 additions & 0 deletions test/eventified.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,11 @@ describe('Eventified', () => {
t.expect(emitter.listenerCount('test-event')).toBe(2);
});

test('no listener count', t => {
const emitter = new Eventified();
t.expect(emitter.listenerCount('test-event')).toBe(0);
});

test('get event names', t => {
const emitter = new Eventified();
const listener = () => {};
Expand All @@ -186,4 +191,41 @@ describe('Eventified', () => {
t.expect(emitter.rawListeners('test-event1')).toEqual([listener]);
t.expect(emitter.rawListeners().length).toEqual(4);
});

test('get raw listeners when no listeners', t => {
const emitter = new Eventified();
t.expect(emitter.rawListeners('test-event')).toEqual([]);
});

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

emitter.on('test-event', listener);
emitter.prependListener('test-event', () => {});

t.expect(emitter.rawListeners('test-event').length).toBe(2);
t.expect(emitter.rawListeners('test-event')[0]).not.toBe(listener);
});

test('prepend with no listenters', t => {
const emitter = new Eventified();
emitter.prependListener('test-event', () => {});
t.expect(emitter.rawListeners('test-event').length).toBe(1);
});

test('prepend once listener', t => {
const emitter = new Eventified();
const listener = () => {};

emitter.on('test-event', listener);
emitter.prependOnceListener('test-event', () => {});

t.expect(emitter.rawListeners('test-event').length).toBe(2);
t.expect(emitter.rawListeners('test-event')[0]).not.toBe(listener);

emitter.emit('test-event');

t.expect(emitter.rawListeners('test-event').length).toBe(1);
});
});

0 comments on commit 6f58fcf

Please sign in to comment.