Skip to content

Commit

Permalink
fix: emitter off
Browse files Browse the repository at this point in the history
  • Loading branch information
romanzy313 authored and skyllo committed Jul 21, 2022
1 parent d1616a9 commit b2d3947
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export class EventEmitter {

public off(event: string, listener: ListenerFunction) {
const listeners = this.eventMap[event] ?? [];
for (let i = listeners.length; i > 0; i -= 1) {
for (let i = listeners.length - 1; i >= 0; i -= 1) {
if (listeners[i] === listener) {
listeners.splice(i, 1);
break;
Expand Down
11 changes: 11 additions & 0 deletions test/emitter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,14 @@ test('emitter can remove multiple listeners', () => {
emitter.emit('eventName');
expect(spyFunc).toBeCalledTimes(0);
});

test('emitter can remove single listener', () => {
const spyFunc = jest.fn();
const emitter = new EventEmitter();
emitter.on('eventName', spyFunc);
emitter.off('eventName', spyFunc);
emitter.emit('eventName');
expect(spyFunc).toBeCalledTimes(0);

spyFunc.mockClear();
});

0 comments on commit b2d3947

Please sign in to comment.