From 38a7c5cd121f13fb3c58944f293b4512d3c39863 Mon Sep 17 00:00:00 2001 From: Luigi Pinca Date: Thu, 15 Jul 2021 09:33:06 +0200 Subject: [PATCH] events: reset the event target to null Allow an event to be dispatched multiple times by resetting the event target to `null` when dispatch is complete. --- lib/internal/event_target.js | 2 +- test/parallel/test-eventtarget.js | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/lib/internal/event_target.js b/lib/internal/event_target.js index 825e1e8b2597ab..0010d3987e2992 100644 --- a/lib/internal/event_target.js +++ b/lib/internal/event_target.js @@ -464,7 +464,7 @@ class EventTarget { } if (event !== undefined) - event[kTarget] = undefined; + event[kTarget] = null; } [kCreateEvent](nodeValue, type) { diff --git a/test/parallel/test-eventtarget.js b/test/parallel/test-eventtarget.js index a85e2cd7f1b9f9..87c6eabe521b69 100644 --- a/test/parallel/test-eventtarget.js +++ b/test/parallel/test-eventtarget.js @@ -162,6 +162,27 @@ let asyncTest = Promise.resolve(); eventTarget.dispatchEvent(ev); } +{ + // Same event dispatched multiple times. + const event = new Event('foo'); + const eventTarget1 = new EventTarget(); + const eventTarget2 = new EventTarget(); + + eventTarget1.addEventListener('foo', common.mustCall((event) => { + strictEqual(event.target, eventTarget1); + })); + + eventTarget2.addEventListener('foo', common.mustCall((event) => { + strictEqual(event.target, eventTarget2); + })); + + eventTarget1.dispatchEvent(event); + strictEqual(event.target, null); + + eventTarget2.dispatchEvent(event); + strictEqual(event.target, null); +} + { const eventTarget = new EventTarget(); const event = new Event('foo', { cancelable: true });