From 865e0eaf59c45cbb839e2c2c54a79e2eaee52e24 Mon Sep 17 00:00:00 2001 From: zfx <502545703@qq.com> Date: Tue, 18 Feb 2020 22:35:08 +0800 Subject: [PATCH 1/2] events: fix not emit removeListener Fix not emit 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() ``` --- lib/events.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/events.js b/lib/events.js index 76b376e789115f..c3ac526a5c2d45 100644 --- a/lib/events.js +++ b/lib/events.js @@ -31,7 +31,6 @@ const { ObjectDefineProperty, ObjectGetPrototypeOf, ObjectSetPrototypeOf, - ObjectKeys, Promise, PromiseReject, PromiseResolve, @@ -531,7 +530,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); } From 6bb3d449f3daaf4d3e5dc0327a8c8cec6d733c72 Mon Sep 17 00:00:00 2001 From: zfx <502545703@qq.com> Date: Wed, 19 Feb 2020 10:41:48 +0800 Subject: [PATCH 2/2] test: add removeAllListeners for symbol When the listener's eventName type is 'symbol' and removeListener is called with no parameters, removeListener should be emitted. --- .../test-event-emitter-remove-all-listeners.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/test/parallel/test-event-emitter-remove-all-listeners.js b/test/parallel/test-event-emitter-remove-all-listeners.js index 3dfe65a8b4bfd0..c62183fd08c203 100644 --- a/test/parallel/test-event-emitter-remove-all-listeners.js +++ b/test/parallel/test-event-emitter-remove-all-listeners.js @@ -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(); +}