Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add method to remove all listeners #6

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions src/event-mixin.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,22 @@ describe('a class with events', () => {
expect(handlerTwoArgs).toHaveLength(1);
expect(handlerTwoArgs[0]).toBe(42);
});

it('should remove all handlers when removeAllListeners is called', () => {
expect.hasAssertions();
const handlerOneArgs: number[] = [];
const handler = (value: number) => {
handlerOneArgs.push(value);
};
myClass.on('eventOne', handler);
myClass.fireEventOne();
expect(handlerOneArgs).toHaveLength(1);
expect(handlerOneArgs[0]).toBe(42);

myClass.removeAllListeners();
myClass.fireEventOne();
expect(handlerOneArgs).toHaveLength(1); // Should still be 1, as all listeners should be removed
});
});

interface ChildEvents {
Expand Down Expand Up @@ -75,4 +91,22 @@ describe('a child class', () => {
expect(eventOneArgs).toHaveLength(1);
expect(eventThreeArgs).toHaveLength(1);
});

it('should remove all handlers from parent and child when removeAllListeners is called', () => {
expect.hasAssertions();

const eventOneArgs: number[] = [];
const eventThreeArgs: string[] = [];

myClass.on('eventOne', (value: number) => eventOneArgs.push(value));
myClass.on('eventThree', (value: string) => eventThreeArgs.push(value));

myClass.removeAllListeners();

myClass.fireEventOne();
myClass.fireEventThree();

expect(eventOneArgs).toHaveLength(0); // Should be 0, as all listeners should be removed
expect(eventThreeArgs).toHaveLength(0); // Should be 0, as all listeners should be removed
});
});
12 changes: 12 additions & 0 deletions src/event-mixin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,18 @@ export function AddEvents<TBase extends Constructor, U>(Base: TBase) {
off<K extends keyof U, E extends eventHandlerType<U[K]>>(eventName: K, handler: E) {
(this as any)[eventName].off(handler);
}

/**
* Remove all event listeners from all events.
*/
removeAllListeners() {
Object.keys(this).forEach((eventName) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this works correctly with inheritance I guess?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. The removeAllListeners method has been updated to ensure it works with inherited events by traversing the prototype chain using a helper function. Unit tests updated as well.

const event = (this as any)[eventName];
if (event instanceof TypedEvent) {
event.removeAllListeners();
}
});
}
};
}

Expand Down
7 changes: 7 additions & 0 deletions src/typed-event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,11 @@ export class TypedEvent<T extends Handler> {
emit(...args: Parameters<T>): void {
this.emitter.emit('event', ...args);
}

/**
* Remove all listeners from this event.
*/
removeAllListeners(): void {
this.emitter.removeAllListeners('event');
}
}
Loading