Skip to content

Commit

Permalink
events: set default handler value
Browse files Browse the repository at this point in the history
Event handler like BroadcastChannel.onmessage and AbortSignal.onabort
have a default value of `null` but we return `undefined`.

Return `null` and add a test.

PR-URL: #41970
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Mestery <mestery@protonmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
  • Loading branch information
benjamingr authored and danielleadams committed Apr 24, 2022
1 parent 290a2bf commit b0a8311
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/internal/event_target.js
Original file line number Diff line number Diff line change
Expand Up @@ -929,7 +929,7 @@ function defineEventHandler(emitter, name) {
// 8.1.5.1 Event handlers - basically `on[eventName]` attributes
ObjectDefineProperty(emitter, `on${name}`, {
get() {
return this[kHandlers]?.get(name)?.handler;
return this[kHandlers]?.get(name)?.handler ?? null;
},
set(value) {
if (!this[kHandlers]) {
Expand Down
11 changes: 9 additions & 2 deletions test/parallel/test-eventtarget.js
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,13 @@ let asyncTest = Promise.resolve();
target.onfoo = common.mustCall();
target.dispatchEvent(new Event('foo'));
}

{
const target = new EventTarget();
defineEventHandler(target, 'foo');
strictEqual(target.onfoo, null);
}

{
const target = new EventTarget();
defineEventHandler(target, 'foo');
Expand Down Expand Up @@ -623,14 +630,14 @@ let asyncTest = Promise.resolve();
strictEqual(et.constructor.name, 'EventTarget');
}
{
// Weak event handlers work
// Weak event listeners work
const et = new EventTarget();
const listener = common.mustCall();
et.addEventListener('foo', listener, { [kWeakHandler]: et });
et.dispatchEvent(new Event('foo'));
}
{
// Weak event handlers can be removed and weakness is not part of the key
// Weak event listeners can be removed and weakness is not part of the key
const et = new EventTarget();
const listener = common.mustNotCall();
et.addEventListener('foo', listener, { [kWeakHandler]: et });
Expand Down

0 comments on commit b0a8311

Please sign in to comment.