diff options
author | Benjamin Gruenbaum <benjamingr@gmail.com> | 2020-05-30 00:51:02 +0300 |
---|---|---|
committer | James M Snell <jasnell@gmail.com> | 2020-06-24 03:08:10 +0300 |
commit | e8f4ac7dadb2bb8d311db0e959d7453a2fc674dc (patch) | |
tree | 247f3d1ea3d4881047a18245dbaee1daa1647c2d /lib/internal/event_target.js | |
parent | 9d324f0441688cef6b9403f2492e6d41cc6a2fc2 (diff) |
events: support event handlers
PR-URL: https://github.com/nodejs/node/pull/34015
Reviewed-By: Denys Otrishko <shishugi@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Diffstat (limited to 'lib/internal/event_target.js')
-rw-r--r-- | lib/internal/event_target.js | 21 |
1 files changed, 19 insertions, 2 deletions
diff --git a/lib/internal/event_target.js b/lib/internal/event_target.js index 7c34acd5c5d..9fef64aaba2 100644 --- a/lib/internal/event_target.js +++ b/lib/internal/event_target.js @@ -479,10 +479,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; + 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, }; |