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

Remove ability to pass array of event names to EventEmitter.prototype.once #196

Merged
merged 1 commit into from
Oct 9, 2023
Merged
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
15 changes: 1 addition & 14 deletions src/utilities/EventEmitter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,6 @@ describe('EventEmitter', () => {
eventEmitter['once'](altListener);
eventEmitter['once']('myEvent', context.spy);
eventEmitter['once']('myEvent', altListener);
eventEmitter['once'](['myEvent', 'myOtherEvent', 'myThirdEvent'], altListener);
context.eventEmitter = eventEmitter;
});

Expand Down Expand Up @@ -171,7 +170,7 @@ describe('EventEmitter', () => {
context.eventEmitter['emit']('myEvent', '');
expect(context.spy).toHaveBeenCalledTimes(2);
context.eventEmitter['emit']('myOtherEvent', '');
expect(context.spy).toHaveBeenCalledTimes(3);
expect(context.spy).toHaveBeenCalledTimes(4);
});

it('removes a specific listener from multiple events', () => {
Expand Down Expand Up @@ -254,18 +253,6 @@ describe('EventEmitter', () => {
context.eventEmitter['emit']('myEvent', '');
expect(context.spy).toHaveBeenCalledOnce();
});

it('adds a listener to multiple eventOnce fields on calling `once` with a listener and event name; and after emitting any of the events, all are removed', (context) => {
context.eventEmitter['once'](['myEvent', 'myOtherEvent', 'myThirdEvent'], context.spy);
expect(context.eventEmitter['eventsOnce']['myEvent']).toHaveLength(1);
expect(context.eventEmitter['eventsOnce']['myOtherEvent']).toHaveLength(1);
expect(context.eventEmitter['eventsOnce']['myThirdEvent']).toHaveLength(1);
expect(context.eventEmitter['emit']('myEvent', ''));
expect(context.eventEmitter['eventsOnce']['myEvent']).toBe(undefined);
expect(context.eventEmitter['eventsOnce']['myOtherEvent']).toBe(undefined);
expect(context.eventEmitter['eventsOnce']['myThirdEvent']).toBe(undefined);
expect(context.spy).toHaveBeenCalledOnce();
});
});

describe('calling the emit method', () => {
Expand Down
33 changes: 7 additions & 26 deletions src/utilities/EventEmitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,46 +223,27 @@ export default class EventEmitter<T extends EventMap> {

/**
* Listen for a single occurrence of an event
* @param listenerOrEvents (optional) the name of the event to listen to
* @param listenerOrEvent (optional) the name of the event to listen to
* @param listener (optional) the listener to be called
*/
once<K extends EventKey<T>>(
listenerOrEvents: K | K[] | EventListener<T[K]>,
listenerOrEvent: K | EventListener<T[K]>,
listener?: EventListener<T[K]>,
): void | Promise<any> {
// .once("eventName", () => {})
if (isString(listenerOrEvents) && isFunction(listener)) {
const listeners = this.eventsOnce[listenerOrEvents] || (this.eventsOnce[listenerOrEvents] = []);
if (isString(listenerOrEvent) && isFunction(listener)) {
const listeners = this.eventsOnce[listenerOrEvent] || (this.eventsOnce[listenerOrEvent] = []);
listeners.push(listener);
return;
}

// .once(["eventName"], () => {})
if (isArray(listenerOrEvents) && isFunction(listener)) {
const self = this;

listenerOrEvents.forEach(function (eventName) {
const listenerWrapper: EventListener<T[K]> = function (this: EventListener<T[K]>, listenerThis) {
const innerArgs = Array.prototype.slice.call(arguments) as [params: T[K]];
listenerOrEvents.forEach((eventName) => {
self.off(eventName, this);
});

listener.apply(listenerThis, innerArgs);
};
self.once(eventName, listenerWrapper);
});

return;
}

// .once(() => {})
if (isFunction(listenerOrEvents)) {
this.anyOnce.push(listenerOrEvents);
if (isFunction(listenerOrEvent)) {
this.anyOnce.push(listenerOrEvent);
return;
}

throw new InvalidArgumentError('EventEmitter.once(): invalid arguments:' + inspect([listenerOrEvents, listener]));
throw new InvalidArgumentError('EventEmitter.once(): invalid arguments:' + inspect([listenerOrEvent, listener]));
}

/**
Expand Down
Loading