Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert bdb6d12 and add test case for listeners #56282

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions lib/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -276,12 +276,6 @@ ObjectDefineProperty(EventEmitter, 'defaultMaxListeners', {
},
});

function hasEventListener(self, type) {
if (type === undefined)
return self._events !== undefined;
return self._events !== undefined && self._events[type] !== undefined;
};

ObjectDefineProperties(EventEmitter, {
kMaxEventTargetListeners: {
__proto__: null,
Expand Down Expand Up @@ -675,11 +669,13 @@ EventEmitter.prototype.removeListener =
function removeListener(type, listener) {
checkListener(listener);

if (!hasEventListener(this, type))
const events = this._events;
if (events === undefined)
return this;

const events = this._events;
const list = events[type];
if (list === undefined)
return this;

if (list === listener || list.listener === listener) {
this._eventsCount -= 1;
Expand Down Expand Up @@ -733,9 +729,9 @@ EventEmitter.prototype.off = EventEmitter.prototype.removeListener;
*/
EventEmitter.prototype.removeAllListeners =
function removeAllListeners(type) {
if (!hasEventListener(this))
return this;
const events = this._events;
if (events === undefined)
return this;

// Not listening for removeListener, no need to emit
if (events.removeListener === undefined) {
Expand Down Expand Up @@ -780,10 +776,14 @@ EventEmitter.prototype.removeAllListeners =
};

function _listeners(target, type, unwrap) {
if (!hasEventListener(target, type))
const events = target._events;

if (events === undefined)
return [];

const evlistener = target._events[type];
const evlistener = events[type];
if (evlistener === undefined)
return [];

if (typeof evlistener === 'function')
return unwrap ? [evlistener.listener || evlistener] : [evlistener];
Expand Down
5 changes: 5 additions & 0 deletions test/parallel/test-event-emitter-listeners.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ function listener4() {
assert.deepStrictEqual(ee.listeners('foo'), []);
}

{
const ee = new events.EventEmitter();
assert.deepStrictEqual(ee.listeners(), []);
}

{
class TestStream extends events.EventEmitter {}
const s = new TestStream();
Expand Down
Loading