mirror of
https://github.com/nodejs/node.git
synced 2026-11-24 21:50:36 -05:00
events: fix bug listenerCount don't compare wrapped listener
When add listener by once, it will be wrapped into another function. And when pass listener and there is just one event listener added by once, it will return 0 even if passed listener equal wrapped event listener. Refs: https://github.com/nodejs/node/pull/46523 PR-URL: https://github.com/nodejs/node/pull/48592 Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Paolo Insogna <paolo@cowtech.it> Reviewed-By: Minwoo Jung <nodecorelab@gmail.com>
This commit is contained in:
1 parent
1aa798d69f
commit
3b8ec348b0
2 files changed
+13
-1
No files matched your search
+1
-1
@@ -850,7 +850,7 @@ function listenerCount(type, listener) {
|
||||
|
||||
if (typeof evlistener === 'function') {
|
||||
if (listener != null) {
|
||||
return listener === evlistener ? 1 : 0;
|
||||
return listener === evlistener || listener === evlistener.listener ? 1 : 0;
|
||||
}
|
||||
|
||||
return 1;
|
||||
|
||||
@@ -12,6 +12,18 @@ assert.strictEqual(EE.listenerCount('event'), 0);
|
||||
assert.strictEqual(EE.listenerCount('event', handler), 0);
|
||||
assert.strictEqual(EE.listenerCount('event', anotherHandler), 0);
|
||||
|
||||
EE.once('event', handler);
|
||||
|
||||
assert.strictEqual(EE.listenerCount('event'), 1);
|
||||
assert.strictEqual(EE.listenerCount('event', handler), 1);
|
||||
assert.strictEqual(EE.listenerCount('event', anotherHandler), 0);
|
||||
|
||||
EE.removeAllListeners('event');
|
||||
|
||||
assert.strictEqual(EE.listenerCount('event'), 0);
|
||||
assert.strictEqual(EE.listenerCount('event', handler), 0);
|
||||
assert.strictEqual(EE.listenerCount('event', anotherHandler), 0);
|
||||
|
||||
EE.on('event', handler);
|
||||
|
||||
assert.strictEqual(EE.listenerCount('event'), 1);
|
||||
|
||||
Reference in new issue
Block a user