Skip to content

Commit 1822cbc

Browse files
JiaLiPassionmhevery
authored andcommitted
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
1 parent a71f114 commit 1822cbc

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

packages/zone.js/lib/node/events.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Zone.__load_patch('EventEmitter', (global: any) => {
1616
const EE_REMOVE_ALL_LISTENER = 'removeAllListeners';
1717
const EE_LISTENERS = 'listeners';
1818
const EE_ON = 'on';
19+
const EE_OFF = 'off';
1920

2021
const compareTaskCallbackVsDelegate = function(task: any, delegate: any) {
2122
// same callback, same capture, same event name, just return
@@ -47,6 +48,7 @@ Zone.__load_patch('EventEmitter', (global: any) => {
4748
});
4849
if (result && result[0]) {
4950
obj[EE_ON] = obj[EE_ADD_LISTENER];
51+
obj[EE_OFF] = obj[EE_REMOVE_LISTENER];
5052
}
5153
}
5254

packages/zone.js/test/node/events.spec.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,18 @@ describe('nodejs EventEmitter', () => {
6666
emitter.emit('test2', 'test value');
6767
});
6868
});
69+
it('should remove listeners by calling off properly', () => {
70+
zoneA.run(() => {
71+
emitter.on('test', shouldNotRun);
72+
emitter.on('test2', shouldNotRun);
73+
emitter.off('test', shouldNotRun);
74+
});
75+
zoneB.run(() => {
76+
emitter.off('test2', shouldNotRun);
77+
emitter.emit('test', 'test value');
78+
emitter.emit('test2', 'test value');
79+
});
80+
});
6981
it('remove listener should return event emitter', () => {
7082
zoneA.run(() => {
7183
emitter.on('test', shouldNotRun);

0 commit comments

Comments
 (0)