Skip to content

Commit

Permalink
lib: undoing accidental changes
Browse files Browse the repository at this point in the history
Some Changes I just sent wasn't related to the PR in question,
so I'm undoing then (in events.js and the addition of linkedMap.js)
Also, fixed the [SymbolAsyncIterator] function returned by
eventsToAsyncIteratorFactory, that was referencing a no longer
available variable
  • Loading branch information
Farenheith committed Dec 22, 2021
1 parent c163dc1 commit 83c02e8
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 143 deletions.
121 changes: 89 additions & 32 deletions lib/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ const {
ArrayPrototypeShift,
ArrayPrototypeSlice,
ArrayPrototypeSplice,
ArrayFrom,
Boolean,
Error,
ErrorCaptureStackTrace,
Expand Down Expand Up @@ -64,7 +63,6 @@ const {
ERR_UNHANDLED_ERROR
},
} = require('internal/errors');
const getLinkedMap = require('internal/linkedMap');

const {
validateAbortSignal,
Expand Down Expand Up @@ -388,19 +386,30 @@ EventEmitter.prototype.emit = function emit(type, ...args) {
if (handler === undefined)
return false;

const listeners = ArrayFrom(handler);
const len = handler.length;
for (let i = 0; i < len; ++i) {
const result = listeners[i].apply(this, args);
if (typeof handler === 'function') {
const result = handler.apply(this, args);

// We check if result is undefined first because that
// is the most common case so we do not pay any perf
// penalty.
// This code is duplicated because extracting it away
// would make it non-inlineable.
// penalty
if (result !== undefined && result !== null) {
addCatch(this, result, type, args);
}
} else {
const len = handler.length;
const listeners = arrayClone(handler);
for (let i = 0; i < len; ++i) {
const result = listeners[i].apply(this, args);

// We check if result is undefined first because that
// is the most common case so we do not pay any perf
// penalty.
// This code is duplicated because extracting it away
// would make it non-inlineable.
if (result !== undefined && result !== null) {
addCatch(this, result, type, args);
}
}
}

return true;
Expand Down Expand Up @@ -433,29 +442,36 @@ function _addListener(target, type, listener, prepend) {

if (existing === undefined) {
// Optimize the case of one listener. Don't need the extra array object.
existing = events[type] = getLinkedMap().push(listener);
events[type] = listener;
++target._eventsCount;
} else if (prepend) {
existing.unshift(listener);
} else {
existing.push(listener);
}
if (typeof existing === 'function') {
// Adding the second element, need to change to array.
existing = events[type] =
prepend ? [listener, existing] : [existing, listener];
// If we've already got an array, just append.
} else if (prepend) {
existing.unshift(listener);
} else {
existing.push(listener);
}

// Check for listener leak
m = _getMaxListeners(target);
if (m > 0 && existing.length > m && !existing.warned) {
existing.warned = true;
// No error code for this since it is a Warning
// eslint-disable-next-line no-restricted-syntax
const w = new Error('Possible EventEmitter memory leak detected. ' +
`${existing.length} ${String(type)} listeners ` +
`added to ${inspect(target, { depth: -1 })}. Use ` +
'emitter.setMaxListeners() to increase limit');
w.name = 'MaxListenersExceededWarning';
w.emitter = target;
w.type = type;
w.count = existing.length;
process.emitWarning(w);
// Check for listener leak
m = _getMaxListeners(target);
if (m > 0 && existing.length > m && !existing.warned) {
existing.warned = true;
// No error code for this since it is a Warning
// eslint-disable-next-line no-restricted-syntax
const w = new Error('Possible EventEmitter memory leak detected. ' +
`${existing.length} ${String(type)} listeners ` +
`added to ${inspect(target, { depth: -1 })}. Use ` +
'emitter.setMaxListeners() to increase limit');
w.name = 'MaxListenersExceededWarning';
w.emitter = target;
w.type = type;
w.count = existing.length;
process.emitWarning(w);
}
}

return target;
Expand Down Expand Up @@ -548,10 +564,39 @@ EventEmitter.prototype.removeListener =
const list = events[type];
if (list === undefined)
return this;
if (list?.remove(listener)) {
if (list.length === 0) {

if (list === listener || list.listener === listener) {
if (--this._eventsCount === 0)
this._events = ObjectCreate(null);
else {
delete events[type];
if (events.removeListener)
this.emit('removeListener', type, list.listener || listener);
}
} else if (typeof list !== 'function') {
let position = -1;

for (let i = list.length - 1; i >= 0; i--) {
if (list[i] === listener || list[i].listener === listener) {
position = i;
break;
}
}

if (position < 0)
return this;

if (position === 0)
list.shift();
else {
if (spliceOne === undefined)
spliceOne = require('internal/util').spliceOne;
spliceOne(list, position);
}

if (list.length === 1)
events[type] = list[0];

if (events.removeListener !== undefined)
this.emit('removeListener', type, listener);
}
Expand Down Expand Up @@ -675,7 +720,19 @@ EventEmitter.prototype.listenerCount = listenerCount;
* @returns {number}
*/
function listenerCount(type) {
return this._events?.[type]?.length || 0;
const events = this._events;

if (events !== undefined) {
const evlistener = events[type];

if (typeof evlistener === 'function') {
return 1;
} else if (evlistener !== undefined) {
return evlistener.length;
}
}

return 0;
}

/**
Expand Down
110 changes: 0 additions & 110 deletions lib/internal/linkedMap.js

This file was deleted.

2 changes: 1 addition & 1 deletion lib/internal/readline/eventsToAsyncIteratorFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,6 @@ module.exports = function eventsToAsyncIteratorFactory(readable, {
get queueSize() {
return queueSize;
},
[SymbolAsyncIterator]() { return result; },
[SymbolAsyncIterator]() { return this; },
};
};

0 comments on commit 83c02e8

Please sign in to comment.