Skip to content

Commit

Permalink
events: allow dispatch many times without listener
Browse files Browse the repository at this point in the history
Fast path for EventTarget dispatch with no listeners didn't reset
kIsBeingDispatched flag, meaning same event couldn't be dispatched
multiple times.

PR-URL: #39772
Refs: #39395
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
  • Loading branch information
mrbbot authored and targos committed Oct 4, 2021
1 parent 528f922 commit 88e7bd0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/internal/event_target.js
Original file line number Diff line number Diff line change
Expand Up @@ -521,8 +521,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;
Expand Down
16 changes: 16 additions & 0 deletions test/parallel/test-eventtarget.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 88e7bd0

Please sign in to comment.