From b0a83115db38b3e3827dc57aacc83d1e8ad84872 Mon Sep 17 00:00:00 2001 From: Benjamin Gruenbaum Date: Wed, 16 Feb 2022 21:36:20 +0200 Subject: [PATCH] events: set default handler value MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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: https://github.com/nodejs/node/pull/41970 Reviewed-By: Tobias Nießen Reviewed-By: Michaël Zasso Reviewed-By: Mestery Reviewed-By: Colin Ihrig --- lib/internal/event_target.js | 2 +- test/parallel/test-eventtarget.js | 11 +++++++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/lib/internal/event_target.js b/lib/internal/event_target.js index bc6f91fbe82159..7c539db2eb3c2a 100644 --- a/lib/internal/event_target.js +++ b/lib/internal/event_target.js @@ -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]) { diff --git a/test/parallel/test-eventtarget.js b/test/parallel/test-eventtarget.js index d3b1ee7358a104..0211dc964d253a 100644 --- a/test/parallel/test-eventtarget.js +++ b/test/parallel/test-eventtarget.js @@ -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'); @@ -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 });