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

fix(node/events): align EventEmitter#addListener with native node tests #976

Merged
merged 1 commit into from
Jun 21, 2021
Merged
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
1 change: 1 addition & 0 deletions node/_tools/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"test-assert.js",
"test-assert-async.js",
"test-assert-fail.js",
"test-event-emitter-add-listeners.js",
"test-event-emitter-max-listeners-warning-for-null.js",
"test-event-emitter-max-listeners-warning-for-symbol.js",
"test-event-emitter-max-listeners-warning.js",
Expand Down
93 changes: 93 additions & 0 deletions node/_tools/suites/parallel/test-event-emitter-add-listeners.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// deno-fmt-ignore-file
// deno-lint-ignore-file

// Copyright Joyent and Node contributors. All rights reserved. MIT license.
// Taken from Node 15.5.1
// This file is automatically generated by "node/_tools/setup.ts". Do not modify this file manually

// Copyright Joyent, Inc. and other Node contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.

'use strict';
const common = require('../common');
const assert = require('assert');
const EventEmitter = require('events');

{
const ee = new EventEmitter();
const events_new_listener_emitted = [];
const listeners_new_listener_emitted = [];

// Sanity check
assert.strictEqual(ee.addListener, ee.on);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👀


ee.on('newListener', function(event, listener) {
// Don't track newListener listeners.
if (event === 'newListener')
return;

events_new_listener_emitted.push(event);
listeners_new_listener_emitted.push(listener);
});

const hello = common.mustCall(function(a, b) {
assert.strictEqual(a, 'a');
assert.strictEqual(b, 'b');
});

ee.once('newListener', function(name, listener) {
assert.strictEqual(name, 'hello');
assert.strictEqual(listener, hello);
assert.deepStrictEqual(this.listeners('hello'), []);
});

ee.on('hello', hello);
ee.once('foo', assert.fail);
assert.deepStrictEqual(['hello', 'foo'], events_new_listener_emitted);
assert.deepStrictEqual([hello, assert.fail], listeners_new_listener_emitted);

ee.emit('hello', 'a', 'b');
}

// Just make sure that this doesn't throw:
{
const f = new EventEmitter();

f.setMaxListeners(0);
}

{
const listen1 = () => {};
const listen2 = () => {};
const ee = new EventEmitter();

ee.once('newListener', function() {
assert.deepStrictEqual(ee.listeners('hello'), []);
ee.once('newListener', function() {
assert.deepStrictEqual(ee.listeners('hello'), []);
});
ee.on('hello', listen2);
});
ee.on('hello', listen1);
// The order of listeners on an event is not always the order in which the
// listeners were added.
assert.deepStrictEqual(ee.listeners('hello'), [listen2, listen1]);
}
29 changes: 22 additions & 7 deletions node/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export class EventEmitter {
prepend: boolean,
): this {
this.checkListenerArgument(listener);
this.emit("newListener", eventName, listener);
this.emit("newListener", eventName, this.unwrapListener(listener));
if (this._events.has(eventName)) {
const listeners = this._events.get(eventName) as Array<
GenericFunction | WrappedFunction
Expand All @@ -109,11 +109,16 @@ export class EventEmitter {
}

/** Alias for emitter.on(eventName, listener). */
public addListener(
addListener(
// deno-lint-ignore no-unused-vars
eventName: string | symbol,
// deno-lint-ignore no-unused-vars
listener: GenericFunction | WrappedFunction,
// deno-lint-ignore ban-ts-comment
// @ts-expect-error
): this {
return this._addListener(eventName, listener, false);
// The body of this method is empty because it will be overwritten by later code. (`EventEmitter.prototype.addListener = EventEmitter.prototype.on;`)
// The purpose of this dirty hack is to get around the current limitation of TypeScript type checking.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

}

/**
Expand Down Expand Up @@ -205,15 +210,22 @@ export class EventEmitter {
: eventListeners.slice(0);
}

private unwrapListeners(arr: GenericFunction[]): GenericFunction[] {
private unwrapListeners(
arr: (GenericFunction | WrappedFunction)[],
): GenericFunction[] {
const unwrappedListeners = new Array(arr.length) as GenericFunction[];
for (let i = 0; i < arr.length; i++) {
// deno-lint-ignore no-explicit-any
unwrappedListeners[i] = (arr[i] as any)["listener"] || arr[i];
unwrappedListeners[i] = this.unwrapListener(arr[i]);
}
return unwrappedListeners;
}

private unwrapListener(
listener: GenericFunction | WrappedFunction,
): GenericFunction {
return (listener as WrappedFunction)["listener"] ?? listener;
}

/** Returns a copy of the array of listeners for the event named eventName.*/
public listeners(eventName: string | symbol): GenericFunction[] {
return this._listeners(this, eventName, true);
Expand Down Expand Up @@ -341,7 +353,7 @@ export class EventEmitter {
for (const listener of listeners) {
this.removeListener(
eventName,
(listener as WrappedFunction)["listener"] ?? listener,
this.unwrapListener(listener),
);
}
}
Expand Down Expand Up @@ -595,6 +607,9 @@ export class EventEmitter {
}
}

// EventEmitter#addListener should point to the same function as EventEmitter#on.
EventEmitter.prototype.addListener = EventEmitter.prototype.on;

class MaxListenersExceededWarning extends Error {
readonly count: number;
constructor(
Expand Down