Skip to content

Commit

Permalink
events: fix removeListener for Symbols
Browse files Browse the repository at this point in the history
Fix removeListener when eventName type is 'symbol'.

```js
const EventEmitter = require('events');
const myEmitter = new EventEmitter();
const sym = Symbol('symbol');
const fn = () => { };
myEmitter.on(sym, fn);

myEmitter.on('removeListener', (...args) => {
  console.log('removeListener');
  console.log(args, args[0] === sym, args[1] === fn);
});

myEmitter.removeAllListeners()
```

When the listener's eventName type is 'symbol' and removeListener is
called with no parameters, removeListener should be emitted.

PR-URL: #31847
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
Reviewed-By: Shelley Vohr <[email protected]>
Reviewed-By: Yongsheng Zhang <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: James M Snell <[email protected]>
  • Loading branch information
fuxingZhang authored and MylesBorins committed Mar 9, 2020
1 parent 2585b81 commit 55486bc
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
3 changes: 1 addition & 2 deletions lib/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ const {
ObjectDefineProperty,
ObjectGetPrototypeOf,
ObjectSetPrototypeOf,
ObjectKeys,
Promise,
PromiseReject,
PromiseResolve,
Expand Down Expand Up @@ -526,7 +525,7 @@ EventEmitter.prototype.removeAllListeners =

// Emit removeListener for all listeners on all events
if (arguments.length === 0) {
for (const key of ObjectKeys(events)) {
for (const key of ReflectOwnKeys(events)) {
if (key === 'removeListener') continue;
this.removeAllListeners(key);
}
Expand Down
13 changes: 13 additions & 0 deletions test/parallel/test-event-emitter-remove-all-listeners.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,16 @@ function expect(expected) {
ee._events = undefined;
assert.strictEqual(ee, ee.removeAllListeners());
}

{
const ee = new events.EventEmitter();
const symbol = Symbol('symbol');
const noop = common.mustNotCall();
ee.on(symbol, noop);

ee.on('removeListener', common.mustCall((...args) => {
assert.deepStrictEqual(args, [symbol, noop]);
}));

ee.removeAllListeners();
}

0 comments on commit 55486bc

Please sign in to comment.