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 all commits
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
47 changes: 47 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,35 @@ 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
});

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

const eventTwoArgs: boolean[] = [];
myClass.on('eventTwo', (value: boolean) => eventTwoArgs.push(value));

myClass.removeAllListeners();

myClass.fireEventTwo();

expect(eventTwoArgs).toHaveLength(0); // Should be 0, as all listeners should be removed
});
});
25 changes: 25 additions & 0 deletions src/event-mixin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,31 @@ 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() {
/**
* Recursively remove all listeners from the given object and its prototype chain. This is
* necessary because the events may be defined on the prototype chain.
*
* @param obj - The object to remove listeners from.
*/
function removeListeners(obj: any) {
if (!obj || obj === Object.prototype) {
return;
}
Object.keys(obj).forEach((eventName) => {
const event = obj[eventName];
if (event instanceof TypedEvent) {
event.removeAllListeners();
}
});
removeListeners(Object.getPrototypeOf(obj));
}
removeListeners(this);
}
};
}

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