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:
parent
a71f114ba4
commit
1822cbcd46
|
@ -16,6 +16,7 @@ Zone.__load_patch('EventEmitter', (global: any) => {
|
||||||
const EE_REMOVE_ALL_LISTENER = 'removeAllListeners';
|
const EE_REMOVE_ALL_LISTENER = 'removeAllListeners';
|
||||||
const EE_LISTENERS = 'listeners';
|
const EE_LISTENERS = 'listeners';
|
||||||
const EE_ON = 'on';
|
const EE_ON = 'on';
|
||||||
|
const EE_OFF = 'off';
|
||||||
|
|
||||||
const compareTaskCallbackVsDelegate = function(task: any, delegate: any) {
|
const compareTaskCallbackVsDelegate = function(task: any, delegate: any) {
|
||||||
// same callback, same capture, same event name, just return
|
// same callback, same capture, same event name, just return
|
||||||
|
@ -47,6 +48,7 @@ Zone.__load_patch('EventEmitter', (global: any) => {
|
||||||
});
|
});
|
||||||
if (result && result[0]) {
|
if (result && result[0]) {
|
||||||
obj[EE_ON] = obj[EE_ADD_LISTENER];
|
obj[EE_ON] = obj[EE_ADD_LISTENER];
|
||||||
|
obj[EE_OFF] = obj[EE_REMOVE_LISTENER];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -66,6 +66,18 @@ describe('nodejs EventEmitter', () => {
|
||||||
emitter.emit('test2', 'test value');
|
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', () => {
|
it('remove listener should return event emitter', () => {
|
||||||
zoneA.run(() => {
|
zoneA.run(() => {
|
||||||
emitter.on('test', shouldNotRun);
|
emitter.on('test', shouldNotRun);
|
||||||
|
|
Loading…
Reference in New Issue