| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 19b5d07 commit 2eb8875
4 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -3373,7 +3373,7 @@ In a future version of Node.js, [`message.headers`][], | |||
| 3373 | 3373 | [`dnsPromises.lookup()`]: dns.md#dnspromiseslookuphostname-options | |
| 3374 | 3374 | [`domain`]: domain.md | |
| 3375 | 3375 | [`ecdh.setPublicKey()`]: crypto.md#ecdhsetpublickeypublickey-encoding | |
| 3376 | - [`emitter.listenerCount(eventName)`]: events.md#emitterlistenercounteventname | ||
| 3376 | + [`emitter.listenerCount(eventName)`]: events.md#emitterlistenercounteventname-listener | ||
| 3377 | 3377 | [`events.listenerCount(emitter, eventName)`]: events.md#eventslistenercountemitter-eventname | |
| 3378 | 3378 | [`fs.FileHandle`]: fs.md#class-filehandle | |
| 3379 | 3379 | [`fs.access()`]: fs.md#fsaccesspath-mode-callback | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -646,16 +646,23 @@ Returns the current max listener value for the `EventEmitter` which is either | |||
| 646 | 646 | set by [`emitter.setMaxListeners(n)`][] or defaults to | |
| 647 | 647 | [`events.defaultMaxListeners`][]. | |
| 648 | 648 | ||
| 649 | - ### `emitter.listenerCount(eventName)` | ||
| 649 | + ### `emitter.listenerCount(eventName[, listener])` | ||
| 650 | 650 | ||
| 651 | 651 | <!-- YAML | |
| 652 | 652 | added: v3.2.0 | |
| 653 | + changes: | ||
| 654 | + - version: REPLACEME | ||
| 655 | + pr-url: https://github.com/nodejs/node/pull/46523 | ||
| 656 | + description: Added the `listener` argument. | ||
| 653 | 657 | --> | |
| 654 | 658 | ||
| 655 | 659 | * `eventName` {string|symbol} The name of the event being listened for | |
| 660 | + * `listener` {Function} The event handler function | ||
| 656 | 661 | * Returns: {integer} | |
| 657 | 662 | ||
| 658 | - Returns the number of listeners listening to the event named `eventName`. | ||
| 663 | + Returns the number of listeners listening for the event named `eventName`. | ||
| 664 | + If `listener` is provided, it will return how many times the listener is found | ||
| 665 | + in the list of the listeners of the event. | ||
| 659 | 666 | ||
| 660 | 667 | ### `emitter.listeners(eventName)` | |
| 661 | 668 | ||
@@ -2482,7 +2489,7 @@ to the `EventTarget`. | |||
| 2482 | 2489 | [`EventTarget` error handling]: #eventtarget-error-handling | |
| 2483 | 2490 | [`Event` Web API]: https://dom.spec.whatwg.org/#event | |
| 2484 | 2491 | [`domain`]: domain.md | |
| 2485 | - [`emitter.listenerCount()`]: #emitterlistenercounteventname | ||
| 2492 | + [`emitter.listenerCount()`]: #emitterlistenercounteventname-listener | ||
| 2486 | 2493 | [`emitter.removeListener()`]: #emitterremovelistenereventname-listener | |
| 2487 | 2494 | [`emitter.setMaxListeners(n)`]: #emittersetmaxlistenersn | |
| 2488 | 2495 | [`event.defaultPrevented`]: #eventdefaultprevented | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -834,17 +834,34 @@ EventEmitter.prototype.listenerCount = listenerCount; | |||
| 834 | 834 | * Returns the number of listeners listening to event name | |
| 835 | 835 | * specified as `type`. | |
| 836 | 836 | * @param {string | symbol} type | |
| 837 | + * @param {Function} listener | ||
| 837 | 838 | * @returns {number} | |
| 838 | 839 | */ | |
| 839 | - function listenerCount(type) { | ||
| 840 | + function listenerCount(type, listener) { | ||
| 840 | 841 | const events = this._events; | |
| 841 | 842 | ||
| 842 | 843 | if (events !== undefined) { | |
| 843 | 844 | const evlistener = events[type]; | |
| 844 | 845 | ||
| 845 | 846 | if (typeof evlistener === 'function') { | |
| 847 | + if (listener != null) { | ||
| 848 | + return listener === evlistener ? 1 : 0; | ||
| 849 | + } | ||
| 850 | + | ||
| 846 | 851 | return 1; | |
| 847 | 852 | } else if (evlistener !== undefined) { | |
| 853 | + if (listener != null) { | ||
| 854 | + let matching = 0; | ||
| 855 | + | ||
| 856 | + for (let i = 0, l = evlistener.length; i < l; i++) { | ||
| 857 | + if (evlistener[i] === listener || evlistener[i].listener === listener) { | ||
| 858 | + matching++; | ||
| 859 | + } | ||
| 860 | + } | ||
| 861 | + | ||
| 862 | + return matching; | ||
| 863 | + } | ||
| 864 | + | ||
| 848 | 865 | return evlistener.length; | |
| 849 | 866 | } | |
| 850 | 867 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,53 @@ | |||
| 1 | + 'use strict'; | ||
| 2 | + | ||
| 3 | + const common = require('../common'); | ||
| 4 | + const EventEmitter = require('events'); | ||
| 5 | + const assert = require('assert'); | ||
| 6 | + | ||
| 7 | + const EE = new EventEmitter(); | ||
| 8 | + const handler = common.mustCall(undefined, 3); | ||
| 9 | + const anotherHandler = common.mustCall(); | ||
| 10 | + | ||
| 11 | + assert.strictEqual(EE.listenerCount('event'), 0); | ||
| 12 | + assert.strictEqual(EE.listenerCount('event', handler), 0); | ||
| 13 | + assert.strictEqual(EE.listenerCount('event', anotherHandler), 0); | ||
| 14 | + | ||
| 15 | + EE.on('event', handler); | ||
| 16 | + | ||
| 17 | + assert.strictEqual(EE.listenerCount('event'), 1); | ||
| 18 | + assert.strictEqual(EE.listenerCount('event', handler), 1); | ||
| 19 | + assert.strictEqual(EE.listenerCount('event', anotherHandler), 0); | ||
| 20 | + | ||
| 21 | + EE.once('event', anotherHandler); | ||
| 22 | + | ||
| 23 | + assert.strictEqual(EE.listenerCount('event'), 2); | ||
| 24 | + assert.strictEqual(EE.listenerCount('event', handler), 1); | ||
| 25 | + assert.strictEqual(EE.listenerCount('event', anotherHandler), 1); | ||
| 26 | + | ||
| 27 | + assert.strictEqual(EE.listenerCount('another-event'), 0); | ||
| 28 | + assert.strictEqual(EE.listenerCount('another-event', handler), 0); | ||
| 29 | + assert.strictEqual(EE.listenerCount('another-event', anotherHandler), 0); | ||
| 30 | + | ||
| 31 | + EE.once('event', handler); | ||
| 32 | + | ||
| 33 | + assert.strictEqual(EE.listenerCount('event'), 3); | ||
| 34 | + assert.strictEqual(EE.listenerCount('event', handler), 2); | ||
| 35 | + assert.strictEqual(EE.listenerCount('event', anotherHandler), 1); | ||
| 36 | + | ||
| 37 | + EE.emit('event'); | ||
| 38 | + | ||
| 39 | + assert.strictEqual(EE.listenerCount('event'), 1); | ||
| 40 | + assert.strictEqual(EE.listenerCount('event', handler), 1); | ||
| 41 | + assert.strictEqual(EE.listenerCount('event', anotherHandler), 0); | ||
| 42 | + | ||
| 43 | + EE.emit('event'); | ||
| 44 | + | ||
| 45 | + assert.strictEqual(EE.listenerCount('event'), 1); | ||
| 46 | + assert.strictEqual(EE.listenerCount('event', handler), 1); | ||
| 47 | + assert.strictEqual(EE.listenerCount('event', anotherHandler), 0); | ||
| 48 | + | ||
| 49 | + EE.off('event', handler); | ||
| 50 | + | ||
| 51 | + assert.strictEqual(EE.listenerCount('event'), 0); | ||
| 52 | + assert.strictEqual(EE.listenerCount('event', handler), 0); | ||
| 53 | + assert.strictEqual(EE.listenerCount('event', anotherHandler), 0); | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments