Skip to content

Commit c22cea0

Browse files
committed
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: nodejs#39395
1 parent 75bd4da commit c22cea0

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

lib/internal/event_target.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -422,8 +422,11 @@ class EventTarget {
422422
}
423423

424424
const root = this[kEvents].get(type);
425-
if (root === undefined || root.next === undefined)
425+
if (root === undefined || root.next === undefined) {
426+
if (event !== undefined)
427+
event[kIsBeingDispatched] = false;
426428
return true;
429+
}
427430

428431
let handler = root.next;
429432
let next;

test/parallel/test-eventtarget.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,22 @@ let asyncTest = Promise.resolve();
186186
deepStrictEqual(event.composedPath(), []);
187187

188188

189+
eventTarget2.dispatchEvent(event);
190+
strictEqual(event.eventPhase, Event.NONE);
191+
strictEqual(event.target, eventTarget2);
192+
deepStrictEqual(event.composedPath(), []);
193+
}
194+
{
195+
// Same event dispatched multiple times, without listeners added.
196+
const event = new Event('foo');
197+
const eventTarget1 = new EventTarget();
198+
const eventTarget2 = new EventTarget();
199+
200+
eventTarget1.dispatchEvent(event);
201+
strictEqual(event.eventPhase, Event.NONE);
202+
strictEqual(event.target, eventTarget1);
203+
deepStrictEqual(event.composedPath(), []);
204+
189205
eventTarget2.dispatchEvent(event);
190206
strictEqual(event.eventPhase, Event.NONE);
191207
strictEqual(event.target, eventTarget2);

0 commit comments

Comments
 (0)