Skip to content

Commit

Permalink
events: reset the event target to null
Browse files Browse the repository at this point in the history
Allow an event to be dispatched multiple times by resetting the event
target to `null` when dispatch is complete.
  • Loading branch information
lpinca committed Jul 15, 2021
1 parent 4b0776a commit 38a7c5c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/internal/event_target.js
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@ class EventTarget {
}

if (event !== undefined)
event[kTarget] = undefined;
event[kTarget] = null;
}

[kCreateEvent](nodeValue, type) {
Expand Down
21 changes: 21 additions & 0 deletions test/parallel/test-eventtarget.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Expand Down

0 comments on commit 38a7c5c

Please sign in to comment.