Skip to content

Commit

Permalink
events: define abort on prototype
Browse files Browse the repository at this point in the history
PR-URL: nodejs#35931
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Antoine du Hamel <[email protected]>
Reviewed-By: Daijiro Wachi <[email protected]>
Reviewed-By: Rich Trott <[email protected]>
  • Loading branch information
benjamingr authored and targos committed Apr 24, 2021
1 parent ac9532c commit 7b7ce38
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 14 deletions.
3 changes: 2 additions & 1 deletion lib/internal/abort_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ Object.defineProperties(AbortSignal.prototype, {
aborted: { enumerable: true }
});

defineEventHandler(AbortSignal.prototype, 'abort');

function abortSignal(signal) {
if (signal[kAborted]) return;
signal[kAborted] = true;
Expand All @@ -64,7 +66,6 @@ class AbortController {
constructor() {
this[kSignal] = new AbortSignal();
emitExperimentalWarning('AbortController');
defineEventHandler(this[kSignal], 'abort');
}

get signal() { return this[kSignal]; }
Expand Down
19 changes: 8 additions & 11 deletions lib/internal/event_target.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,15 @@ const {
ArrayFrom,
Boolean,
Error,
Map,
NumberIsInteger,
Object,
ObjectDefineProperty,
ObjectGetOwnPropertyDescriptor,
SafeMap,
String,
Symbol,
SymbolFor,
SymbolToStringTag,
SafeWeakMap,
SafeWeakSet,
} = primordials;

Expand All @@ -35,6 +34,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 = Symbol.for('nodejs.internal.kHybridDispatch');
const kCreateEvent = Symbol('kCreateEvent');
Expand Down Expand Up @@ -218,7 +218,7 @@ class Listener {
}

function initEventTarget(self) {
self[kEvents] = new Map();
self[kEvents] = new SafeMap();
}

class EventTarget {
Expand Down Expand Up @@ -577,27 +577,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
Expand Down
2 changes: 0 additions & 2 deletions lib/internal/worker/io.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,6 @@ ObjectDefineProperty(
// This is called from inside the `MessagePort` constructor.
function oninit() {
initNodeEventTarget(this);
defineEventHandler(this, 'message');
defineEventHandler(this, 'messageerror');
setupPortReferencing(this, this, 'message');
}

Expand Down

0 comments on commit 7b7ce38

Please sign in to comment.