From dd0146593cdad9fad37c85a8a04a6a6d9abd6f05 Mon Sep 17 00:00:00 2001 From: MrBBot Date: Sun, 15 Aug 2021 15:58:01 +0100 Subject: [PATCH] events: allow dispatch many times without listener Fast path for EventTarget dispatch with no listeners didn't reset kIsBeingDispatched flag, meaning same event couldn't be dispatched multiple times. Refs: https://github.com/nodejs/node/pull/39395 --- lib/internal/event_target.js | 5 ++++- test/parallel/test-eventtarget.js | 16 ++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/lib/internal/event_target.js b/lib/internal/event_target.js index ce867cc1e35793..c2331a0595abfa 100644 --- a/lib/internal/event_target.js +++ b/lib/internal/event_target.js @@ -422,8 +422,11 @@ class EventTarget { } const root = this[kEvents].get(type); - if (root === undefined || root.next === undefined) + if (root === undefined || root.next === undefined) { + if (event !== undefined) + event[kIsBeingDispatched] = false; return true; + } let handler = root.next; let next; diff --git a/test/parallel/test-eventtarget.js b/test/parallel/test-eventtarget.js index cfad9c60a32c01..d3b1ee7358a104 100644 --- a/test/parallel/test-eventtarget.js +++ b/test/parallel/test-eventtarget.js @@ -186,6 +186,22 @@ let asyncTest = Promise.resolve(); deepStrictEqual(event.composedPath(), []); + eventTarget2.dispatchEvent(event); + strictEqual(event.eventPhase, Event.NONE); + strictEqual(event.target, eventTarget2); + deepStrictEqual(event.composedPath(), []); +} +{ + // Same event dispatched multiple times, without listeners added. + const event = new Event('foo'); + const eventTarget1 = new EventTarget(); + const eventTarget2 = new EventTarget(); + + eventTarget1.dispatchEvent(event); + strictEqual(event.eventPhase, Event.NONE); + strictEqual(event.target, eventTarget1); + deepStrictEqual(event.composedPath(), []); + eventTarget2.dispatchEvent(event); strictEqual(event.eventPhase, Event.NONE); strictEqual(event.target, eventTarget2);