From 89eaf9ce6064c7ce694c38cab67913b704615abc Mon Sep 17 00:00:00 2001 From: Benjamin Gruenbaum Date: Sat, 30 May 2020 00:51:02 +0300 Subject: [PATCH 1/3] events: support event handlers --- lib/internal/event_target.js | 21 ++++++++++++++++++-- test/parallel/test-eventtarget.js | 33 +++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 2 deletions(-) diff --git a/lib/internal/event_target.js b/lib/internal/event_target.js index 8641129b132914..b0eafe8fa1d3e8 100644 --- a/lib/internal/event_target.js +++ b/lib/internal/event_target.js @@ -445,10 +445,27 @@ function emitUnhandledRejectionOrErr(that, err, event) { process.emit('error', err, event); } -// EventEmitter-ish API: - +function defineEventHandler(emitter, name) { + // 8.1.5.1 Event handlers - basically `on[eventName]` attributes + let eventHandlerValue = undefined; + Object.defineProperty(emitter, `on${name}`, { + get() { + return eventHandlerValue; + }, + set(value) { + if (eventHandlerValue) { + emitter.removeEventListener(name, eventHandlerValue); + } + if (typeof value === 'function') { + emitter.addEventListener(name, value); + } + eventHandlerValue = value; + } + }); +} module.exports = { Event, EventTarget, NodeEventTarget, + defineEventHandler, }; diff --git a/test/parallel/test-eventtarget.js b/test/parallel/test-eventtarget.js index 3e652e1e3396b4..a7165ec2d8304b 100644 --- a/test/parallel/test-eventtarget.js +++ b/test/parallel/test-eventtarget.js @@ -6,6 +6,7 @@ const { Event, EventTarget, NodeEventTarget, + defineEventHandler } = require('internal/event_target'); const { @@ -438,4 +439,36 @@ ok(EventTarget); strictEqual(target.toString(), '[object EventTarget]'); const event = new Event(''); strictEqual(event.toString(), '[object Event]'); +{ + const target = new EventTarget(); + defineEventHandler(target, 'foo'); + target.onfoo = common.mustCall(); + target.dispatchEvent(new Event('foo')); +} +{ + const target = new EventTarget(); + defineEventHandler(target, 'foo'); + let count = 0; + target.onfoo = () => count++; + target.onfoo = common.mustCall(() => count++); + target.dispatchEvent(new Event('foo')); + strictEqual(count, 1); +} +{ + const target = new EventTarget(); + defineEventHandler(target, 'foo'); + let count = 0; + target.addEventListener('foo', () => count++); + target.onfoo = common.mustCall(() => count++); + target.dispatchEvent(new Event('foo')); + strictEqual(count, 2); +} +{ + const target = new EventTarget(); + defineEventHandler(target, 'foo'); + let fn = common.mustNotCall(() => count++); + target.onfoo = fn; + strictEqual(target.onfoo, fn); + target.onfoo = null; + target.dispatchEvent(new Event('foo')); } From 8ac4425149fa1cfb03eda569f81f115e22dd554a Mon Sep 17 00:00:00 2001 From: Benjamin Gruenbaum Date: Sat, 30 May 2020 01:31:17 +0300 Subject: [PATCH 2/3] fixup! lint --- lib/internal/event_target.js | 2 +- test/parallel/test-eventtarget.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/internal/event_target.js b/lib/internal/event_target.js index b0eafe8fa1d3e8..49c16b90e82f04 100644 --- a/lib/internal/event_target.js +++ b/lib/internal/event_target.js @@ -447,7 +447,7 @@ function emitUnhandledRejectionOrErr(that, err, event) { function defineEventHandler(emitter, name) { // 8.1.5.1 Event handlers - basically `on[eventName]` attributes - let eventHandlerValue = undefined; + let eventHandlerValue; Object.defineProperty(emitter, `on${name}`, { get() { return eventHandlerValue; diff --git a/test/parallel/test-eventtarget.js b/test/parallel/test-eventtarget.js index a7165ec2d8304b..cae672564680a5 100644 --- a/test/parallel/test-eventtarget.js +++ b/test/parallel/test-eventtarget.js @@ -466,7 +466,7 @@ ok(EventTarget); { const target = new EventTarget(); defineEventHandler(target, 'foo'); - let fn = common.mustNotCall(() => count++); + const fn = common.mustNotCall(); target.onfoo = fn; strictEqual(target.onfoo, fn); target.onfoo = null; From 076b122b0bd5b72e0957d7378e89248ba4841fb2 Mon Sep 17 00:00:00 2001 From: Benjamin Gruenbaum Date: Sun, 21 Jun 2020 13:18:02 +0300 Subject: [PATCH 3/3] Update test/parallel/test-eventtarget.js Co-authored-by: James M Snell --- test/parallel/test-eventtarget.js | 1 + 1 file changed, 1 insertion(+) diff --git a/test/parallel/test-eventtarget.js b/test/parallel/test-eventtarget.js index cae672564680a5..b6ef00cb59e045 100644 --- a/test/parallel/test-eventtarget.js +++ b/test/parallel/test-eventtarget.js @@ -439,6 +439,7 @@ ok(EventTarget); strictEqual(target.toString(), '[object EventTarget]'); const event = new Event(''); strictEqual(event.toString(), '[object Event]'); +} { const target = new EventTarget(); defineEventHandler(target, 'foo');