Skip to content

Commit

Permalink
events: define abort on prototype
Browse files Browse the repository at this point in the history
PR-URL: #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 danielleadams committed Nov 9, 2020
1 parent ab0eb4f commit ff59fcd
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 @@ -48,6 +48,8 @@ ObjectDefineProperties(AbortSignal.prototype, {
aborted: { enumerable: true }
});

defineEventHandler(AbortSignal.prototype, 'abort');

function abortSignal(signal) {
if (signal[kAborted]) return;
signal[kAborted] = true;
Expand All @@ -65,7 +67,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,17 +4,16 @@ const {
ArrayFrom,
Boolean,
Error,
Map,
NumberIsInteger,
ObjectAssign,
ObjectDefineProperties,
ObjectDefineProperty,
ObjectGetOwnPropertyDescriptor,
SafeMap,
String,
Symbol,
SymbolFor,
SymbolToStringTag,
SafeWeakMap,
SafeWeakSet,
} = primordials;

Expand All @@ -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');
Expand Down Expand Up @@ -219,7 +219,7 @@ class Listener {
}

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

class EventTarget {
Expand Down Expand Up @@ -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
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 @@ -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');
}

Expand Down

0 comments on commit ff59fcd

Please sign in to comment.