fix(zone.js): patch nodejs EventEmtter.prototype.off (#37863)

Close #35473

zone.js nodejs patch should also patch `EventEmitter.prototype.off` as `removeListener`.
So `off` can correctly remove the listeners added by `EventEmitter.prototype.addListener`

PR Close #37863
This commit is contained in:
JiaLiPassion 2020-07-01 07:15:22 +09:00 committed by Misko Hevery
parent a71f114ba4
commit 1822cbcd46
2 changed files with 14 additions and 0 deletions

View File

@ -16,6 +16,7 @@ Zone.__load_patch('EventEmitter', (global: any) => {
const EE_REMOVE_ALL_LISTENER = 'removeAllListeners';
const EE_LISTENERS = 'listeners';
const EE_ON = 'on';
const EE_OFF = 'off';
const compareTaskCallbackVsDelegate = function(task: any, delegate: any) {
// same callback, same capture, same event name, just return
@ -47,6 +48,7 @@ Zone.__load_patch('EventEmitter', (global: any) => {
});
if (result && result[0]) {
obj[EE_ON] = obj[EE_ADD_LISTENER];
obj[EE_OFF] = obj[EE_REMOVE_LISTENER];
}
}

View File

@ -66,6 +66,18 @@ describe('nodejs EventEmitter', () => {
emitter.emit('test2', 'test value');
});
});
it('should remove listeners by calling off properly', () => {
zoneA.run(() => {
emitter.on('test', shouldNotRun);
emitter.on('test2', shouldNotRun);
emitter.off('test', shouldNotRun);
});
zoneB.run(() => {
emitter.off('test2', shouldNotRun);
emitter.emit('test', 'test value');
emitter.emit('test2', 'test value');
});
});
it('remove listener should return event emitter', () => {
zoneA.run(() => {
emitter.on('test', shouldNotRun);