diff --git a/lib/internal/event_target.js b/lib/internal/event_target.js index 9fef64aaba2490..66669b31c96848 100644 --- a/lib/internal/event_target.js +++ b/lib/internal/event_target.js @@ -7,7 +7,6 @@ const { Map, NumberIsInteger, Object, - Set, Symbol, SymbolFor, SymbolToStringTag, @@ -199,7 +198,6 @@ class EventTarget { static [kIsEventTarget] = true; [kEvents] = new Map(); - #emitting = new Set(); [kNewListener](size, type, listener, once, capture, passive) {} [kRemoveListener](size, type, listener, capture) {} @@ -266,25 +264,20 @@ class EventTarget { } dispatchEvent(event) { - if (!(event instanceof Event)) { + if (!(event instanceof Event)) throw new ERR_INVALID_ARG_TYPE('event', 'Event', event); - } - if (!isEventTarget(this)) { + if (!isEventTarget(this)) throw new ERR_INVALID_THIS('EventTarget'); - } - if (this.#emitting.has(event.type) || - event[kTarget] !== null) { + if (event[kTarget] !== null) throw new ERR_EVENT_RECURSION(event.type); - } const root = this[kEvents].get(event.type); if (root === undefined || root.next === undefined) return true; event[kTarget] = this; - this.#emitting.add(event.type); let handler = root.next; let next; @@ -310,7 +303,6 @@ class EventTarget { handler = next; } - this.#emitting.delete(event.type); event[kTarget] = undefined; return event.defaultPrevented !== true; diff --git a/test/parallel/test-eventtarget.js b/test/parallel/test-eventtarget.js index 13b40fb32ed95b..1cf88993ba1cf5 100644 --- a/test/parallel/test-eventtarget.js +++ b/test/parallel/test-eventtarget.js @@ -293,13 +293,12 @@ ok(EventTarget); // Once handler only invoked once const ev = common.mustCall((event) => { - throws(() => eventTarget.dispatchEvent(new Event('foo')), { - code: 'ERR_EVENT_RECURSION' - }); + // Can invoke the same event name recursively + eventTarget.dispatchEvent(new Event('foo')); }); // Errors in a handler won't stop calling the others. - eventTarget.addEventListener('foo', ev); + eventTarget.addEventListener('foo', ev, { once: true }); eventTarget.dispatchEvent(new Event('foo')); }