From ab72df6fe403a8205e5cfab60fe2e01ef23bd53c Mon Sep 17 00:00:00 2001 From: JiaLiPassion Date: Wed, 14 Mar 2018 12:13:52 +0900 Subject: [PATCH] fix(event): fix #1021, removeListener/removeAllListeners should return eventEmitter (#1022) --- lib/common/events.ts | 7 +++++++ test/node/events.spec.ts | 15 +++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/lib/common/events.ts b/lib/common/events.ts index 49bc6fe16..ecc80122f 100644 --- a/lib/common/events.ts +++ b/lib/common/events.ts @@ -497,6 +497,9 @@ export function patchEventTarget( target[symbolEventName] = null; } existingTask.zone.cancelTask(existingTask); + if (returnTarget) { + return target; + } return; } } @@ -571,6 +574,10 @@ export function patchEventTarget( } } } + + if (returnTarget) { + return this; + } }; // for native toString patch diff --git a/test/node/events.spec.ts b/test/node/events.spec.ts index 4bee40b59..758128e76 100644 --- a/test/node/events.spec.ts +++ b/test/node/events.spec.ts @@ -66,6 +66,13 @@ describe('nodejs EventEmitter', () => { emitter.emit('test2', 'test value'); }); }); + it('remove listener should return event emitter', () => { + zoneA.run(() => { + emitter.on('test', shouldNotRun); + expect(emitter.removeListener('test', shouldNotRun)).toEqual(emitter); + emitter.emit('test', 'test value'); + }); + }); it('should return all listeners for an event', () => { zoneA.run(() => { emitter.on('test', expectZoneA); @@ -106,6 +113,14 @@ describe('nodejs EventEmitter', () => { expect(emitter.listeners('test').length).toEqual(0); }); }); + it('remove All listeners should return event emitter', () => { + zoneA.run(() => { + emitter.on('test', expectZoneA); + emitter.on('test', expectZoneA); + expect(emitter.removeAllListeners('test')).toEqual(emitter); + expect(emitter.listeners('test').length).toEqual(0); + }); + }); it('should remove All listeners properly even without a type parameter', () => { zoneA.run(() => { emitter.on('test', shouldNotRun);