diff --git a/src/utilities/EventEmitter.test.ts b/src/utilities/EventEmitter.test.ts index aa4c87f6..330102ea 100644 --- a/src/utilities/EventEmitter.test.ts +++ b/src/utilities/EventEmitter.test.ts @@ -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; }); @@ -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', () => { @@ -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', () => { diff --git a/src/utilities/EventEmitter.ts b/src/utilities/EventEmitter.ts index 247645a2..6c2d1101 100644 --- a/src/utilities/EventEmitter.ts +++ b/src/utilities/EventEmitter.ts @@ -223,46 +223,27 @@ export default class EventEmitter { /** * 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>( - listenerOrEvents: K | K[] | EventListener, + listenerOrEvent: K | EventListener, listener?: EventListener, ): void | Promise { // .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 = function (this: EventListener, 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])); } /**