events: define abort on prototype

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:
Benjamin Gruenbaum 2020-11-02 22:59:54 +02:00
parent b80d847608
commit 1c99cdebbc
3 changed files with 10 additions and 14 deletions

View File

@ -48,6 +48,8 @@ ObjectDefineProperties(AbortSignal.prototype, {
aborted: { enumerable: true }
});
defineEventHandler(AbortSignal.prototype, 'abort');
function abortSignal(signal) {
if (signal[kAborted]) return;
signal[kAborted] = true;
@ -65,7 +67,6 @@ class AbortController {
constructor() {
this[kSignal] = new AbortSignal();
emitExperimentalWarning('AbortController');
defineEventHandler(this[kSignal], 'abort');
}
get signal() { return this[kSignal]; }

View File

@ -4,17 +4,16 @@ const {
ArrayFrom,
Boolean,
Error,
Map,
NumberIsInteger,
ObjectAssign,
ObjectDefineProperties,
ObjectDefineProperty,
ObjectGetOwnPropertyDescriptor,
SafeMap,
String,
Symbol,
SymbolFor,
SymbolToStringTag,
SafeWeakMap,
SafeWeakSet,
} = primordials;
@ -36,6 +35,7 @@ const kIsEventTarget = SymbolFor('nodejs.event_target');
const kEvents = Symbol('kEvents');
const kStop = Symbol('kStop');
const kTarget = Symbol('kTarget');
const kHandlers = Symbol('khandlers');
const kHybridDispatch = SymbolFor('nodejs.internal.kHybridDispatch');
const kCreateEvent = Symbol('kCreateEvent');
@ -219,7 +219,7 @@ class Listener {
}
function initEventTarget(self) {
self[kEvents] = new Map();
self[kEvents] = new SafeMap();
}
class EventTarget {
@ -578,27 +578,24 @@ 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
ObjectDefineProperty(emitter, `on${name}`, {
get() {
return eventHandlerValueMap.get(this)?.get(name);
return this[kHandlers]?.get(name);
},
set(value) {
const oldValue = eventHandlerValueMap.get(this)?.get(name);
const oldValue = this[kHandlers]?.get(name);
if (oldValue) {
this.removeEventListener(name, oldValue);
}
if (typeof value === 'function') {
this.addEventListener(name, value);
}
if (!eventHandlerValueMap.has(this)) {
eventHandlerValueMap.set(this, new Map());
if (!this[kHandlers]) {
this[kHandlers] = new SafeMap();
}
eventHandlerValueMap.get(this).set(name, value);
this[kHandlers].set(name, value);
},
configurable: true,
enumerable: true

View File

@ -146,8 +146,6 @@ ObjectDefineProperty(
// This is called from inside the `MessagePort` constructor.
function oninit() {
initNodeEventTarget(this);
defineEventHandler(this, 'message');
defineEventHandler(this, 'messageerror');
setupPortReferencing(this, this, 'message');
}