mirror of
https://github.com/nodejs/node.git
synced 2026-11-27 17:52:27 -05:00
events: support event handlers on prototypes
PR-URL: https://github.com/nodejs/node/pull/35931 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Daijiro Wachi <daijiro.wachi@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
This commit is contained in:
1 parent
33e2ee58a7
commit
ab0eb4f2c9
3 files changed
+18
-12
No files matched your search
@@ -14,6 +14,7 @@ const {
|
||||
Symbol,
|
||||
SymbolFor,
|
||||
SymbolToStringTag,
|
||||
SafeWeakMap,
|
||||
SafeWeakSet,
|
||||
} = primordials;
|
||||
|
||||
@@ -577,21 +578,27 @@ function emitUnhandledRejectionOrErr(that, err, event) {
|
||||
process.emit('error', err, event);
|
||||
}
|
||||
|
||||
// A map of emitter -> map of name -> handler
|
||||
const eventHandlerValueMap = new SafeWeakMap();
|
||||
|
||||
function defineEventHandler(emitter, name) {
|
||||
// 8.1.5.1 Event handlers - basically `on[eventName]` attributes
|
||||
let eventHandlerValue;
|
||||
ObjectDefineProperty(emitter, `on${name}`, {
|
||||
get() {
|
||||
return eventHandlerValue;
|
||||
return eventHandlerValueMap.get(this)?.get(name);
|
||||
},
|
||||
set(value) {
|
||||
if (eventHandlerValue) {
|
||||
emitter.removeEventListener(name, eventHandlerValue);
|
||||
const oldValue = eventHandlerValueMap.get(this)?.get(name);
|
||||
if (oldValue) {
|
||||
this.removeEventListener(name, oldValue);
|
||||
}
|
||||
if (typeof value === 'function') {
|
||||
emitter.addEventListener(name, value);
|
||||
this.addEventListener(name, value);
|
||||
}
|
||||
eventHandlerValue = value;
|
||||
if (!eventHandlerValueMap.has(this)) {
|
||||
eventHandlerValueMap.set(this, new Map());
|
||||
}
|
||||
eventHandlerValueMap.get(this).set(name, value);
|
||||
},
|
||||
configurable: true,
|
||||
enumerable: true
|
||||
|
||||
@@ -146,13 +146,14 @@ ObjectDefineProperty(
|
||||
// This is called from inside the `MessagePort` constructor.
|
||||
function oninit() {
|
||||
initNodeEventTarget(this);
|
||||
// TODO(addaleax): This should be on MessagePort.prototype, but
|
||||
// defineEventHandler() does not support that.
|
||||
defineEventHandler(this, 'message');
|
||||
defineEventHandler(this, 'messageerror');
|
||||
setupPortReferencing(this, this, 'message');
|
||||
}
|
||||
|
||||
defineEventHandler(MessagePort.prototype, 'message');
|
||||
defineEventHandler(MessagePort.prototype, 'messageerror');
|
||||
|
||||
ObjectDefineProperty(MessagePort.prototype, onInitSymbol, {
|
||||
enumerable: true,
|
||||
writable: false,
|
||||
|
||||
@@ -165,9 +165,7 @@ const { MessageChannel, MessagePort } = require('worker_threads');
|
||||
assert.deepStrictEqual(
|
||||
Object.getOwnPropertyNames(MessagePort.prototype).sort(),
|
||||
[
|
||||
// TODO(addaleax): This should include onmessage (and eventually
|
||||
// onmessageerror).
|
||||
'close', 'constructor', 'postMessage', 'ref', 'start',
|
||||
'unref'
|
||||
'close', 'constructor', 'onmessage', 'onmessageerror', 'postMessage',
|
||||
'ref', 'start', 'unref'
|
||||
]);
|
||||
}
|
||||
Reference in new issue
Block a user