| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -786,6 +786,9 @@ The [`domain`][] module is deprecated and should not be used. | |||
| 786 | 786 | ||
| 787 | 787 | <!-- YAML | |
| 788 | 788 | changes: | |
| 789 | + - version: REPLACEME | ||
| 790 | + pr-url: https://github.com/nodejs/node/pull/60214 | ||
| 791 | + description: Deprecation revoked. | ||
| 789 | 792 | - version: | |
| 790 | 793 | - v6.12.0 | |
| 791 | 794 | - v4.8.6 | |
@@ -796,10 +799,12 @@ changes: | |||
| 796 | 799 | description: Documentation-only deprecation. | |
| 797 | 800 | --> | |
| 798 | 801 | ||
| 799 | - Type: Documentation-only | ||
| 802 | + Type: Revoked | ||
| 800 | 803 | ||
| 801 | - The [`events.listenerCount(emitter, eventName)`][] API is | ||
| 802 | - deprecated. Please use [`emitter.listenerCount(eventName)`][] instead. | ||
| 804 | + The [`events.listenerCount(emitter, eventName)`][] API was deprecated, as it | ||
| 805 | + provided identical fuctionality to [`emitter.listenerCount(eventName)`][]. The | ||
| 806 | + deprecation was revoked because this function has been repurposed to also | ||
| 807 | + accept {EventTarget} arguments. | ||
| 803 | 808 | ||
| 804 | 809 | ### DEP0034: `fs.exists(path, callback)` | |
| 805 | 810 | ||
@@ -4422,7 +4427,7 @@ import { opendir } from 'node:fs/promises'; | |||
| 4422 | 4427 | [`domain`]: domain.md | |
| 4423 | 4428 | [`ecdh.setPublicKey()`]: crypto.md#ecdhsetpublickeypublickey-encoding | |
| 4424 | 4429 | [`emitter.listenerCount(eventName)`]: events.md#emitterlistenercounteventname-listener | |
| 4425 | - [`events.listenerCount(emitter, eventName)`]: events.md#eventslistenercountemitter-eventname | ||
| 4430 | + [`events.listenerCount(emitter, eventName)`]: events.md#eventslistenercountemitterortarget-eventname | ||
| 4426 | 4431 | [`fs.Dir`]: fs.md#class-fsdir | |
| 4427 | 4432 | [`fs.FileHandle`]: fs.md#class-filehandle | |
| 4428 | 4433 | [`fs.access()`]: fs.md#fsaccesspath-mode-callback | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1622,39 +1622,66 @@ changes: | |||
| 1622 | 1622 | ||
| 1623 | 1623 | See how to write a custom [rejection handler][rejection]. | |
| 1624 | 1624 | ||
| 1625 | - ## `events.listenerCount(emitter, eventName)` | ||
| 1625 | + ## `events.listenerCount(emitterOrTarget, eventName)` | ||
| 1626 | 1626 | ||
| 1627 | 1627 | <!-- YAML | |
| 1628 | 1628 | added: v0.9.12 | |
| 1629 | - deprecated: v3.2.0 | ||
| 1629 | + changes: | ||
| 1630 | + - version: REPLACEME | ||
| 1631 | + pr-url: https://github.com/nodejs/node/pull/60214 | ||
| 1632 | + description: Now accepts EventTarget arguments. | ||
| 1633 | + - version: REPLACEME | ||
| 1634 | + pr-url: https://github.com/nodejs/node/pull/60214 | ||
| 1635 | + description: Deprecation revoked. | ||
| 1636 | + - version: v3.2.0 | ||
| 1637 | + pr-url: https://github.com/nodejs/node/pull/2349 | ||
| 1638 | + description: Documentation-only deprecation. | ||
| 1630 | 1639 | --> | |
| 1631 | 1640 | ||
| 1632 | - > Stability: 0 - Deprecated: Use [`emitter.listenerCount()`][] instead. | ||
| 1641 | + * `emitterOrTarget` {EventEmitter|EventTarget} | ||
| 1642 | + * `eventName` {string|symbol} | ||
| 1643 | + * Returns: {integer} | ||
| 1644 | + | ||
| 1645 | + Returns the number of registered listeners for the event named `eventName`. | ||
| 1633 | 1646 | ||
| 1634 | - * `emitter` {EventEmitter} The emitter to query | ||
| 1635 | - * `eventName` {string|symbol} The event name | ||
| 1647 | + For `EventEmitter`s this behaves exactly the same as calling `.listenerCount` | ||
| 1648 | + on the emitter. | ||
| 1636 | 1649 | ||
| 1637 | - A class method that returns the number of listeners for the given `eventName` | ||
| 1638 | - registered on the given `emitter`. | ||
| 1650 | + For `EventTarget`s this is the only way to obtain the listener count. This can | ||
| 1651 | + be useful for debugging and diagnostic purposes. | ||
| 1639 | 1652 | ||
| 1640 | 1653 | ```mjs | |
| 1641 | 1654 | import { EventEmitter, listenerCount } from 'node:events'; | |
| 1642 | 1655 | ||
| 1643 | - const myEmitter = new EventEmitter(); | ||
| 1644 | - myEmitter.on('event', () => {}); | ||
| 1645 | - myEmitter.on('event', () => {}); | ||
| 1646 | - console.log(listenerCount(myEmitter, 'event')); | ||
| 1647 | - // Prints: 2 | ||
| 1656 | + { | ||
| 1657 | + const ee = new EventEmitter(); | ||
| 1658 | + ee.on('event', () => {}); | ||
| 1659 | + ee.on('event', () => {}); | ||
| 1660 | + console.log(listenerCount(ee, 'event')); // 2 | ||
| 1661 | + } | ||
| 1662 | + { | ||
| 1663 | + const et = new EventTarget(); | ||
| 1664 | + et.addEventListener('event', () => {}); | ||
| 1665 | + et.addEventListener('event', () => {}); | ||
| 1666 | + console.log(listenerCount(et, 'event')); // 2 | ||
| 1667 | + } | ||
| 1648 | 1668 | ``` | |
| 1649 | 1669 | ||
| 1650 | 1670 | ```cjs | |
| 1651 | 1671 | const { EventEmitter, listenerCount } = require('node:events'); | |
| 1652 | 1672 | ||
| 1653 | - const myEmitter = new EventEmitter(); | ||
| 1654 | - myEmitter.on('event', () => {}); | ||
| 1655 | - myEmitter.on('event', () => {}); | ||
| 1656 | - console.log(listenerCount(myEmitter, 'event')); | ||
| 1657 | - // Prints: 2 | ||
| 1673 | + { | ||
| 1674 | + const ee = new EventEmitter(); | ||
| 1675 | + ee.on('event', () => {}); | ||
| 1676 | + ee.on('event', () => {}); | ||
| 1677 | + console.log(listenerCount(ee, 'event')); // 2 | ||
| 1678 | + } | ||
| 1679 | + { | ||
| 1680 | + const et = new EventTarget(); | ||
| 1681 | + et.addEventListener('event', () => {}); | ||
| 1682 | + et.addEventListener('event', () => {}); | ||
| 1683 | + console.log(listenerCount(et, 'event')); // 2 | ||
| 1684 | + } | ||
| 1658 | 1685 | ``` | |
| 1659 | 1686 | ||
| 1660 | 1687 | ## `events.on(emitter, eventName[, options])` | |
@@ -2648,7 +2675,6 @@ to the `EventTarget`. | |||
| 2648 | 2675 | [`Event` Web API]: https://dom.spec.whatwg.org/#event | |
| 2649 | 2676 | [`domain`]: domain.md | |
| 2650 | 2677 | [`e.stopImmediatePropagation()`]: #eventstopimmediatepropagation | |
| 2651 | - [`emitter.listenerCount()`]: #emitterlistenercounteventname-listener | ||
| 2652 | 2678 | [`emitter.removeListener()`]: #emitterremovelistenereventname-listener | |
| 2653 | 2679 | [`emitter.setMaxListeners(n)`]: #emittersetmaxlistenersn | |
| 2654 | 2680 | [`event.defaultPrevented`]: #eventdefaultprevented | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -33,7 +33,6 @@ const { | |||
| 33 | 33 | Error, | |
| 34 | 34 | ErrorCaptureStackTrace, | |
| 35 | 35 | FunctionPrototypeBind, | |
| 36 | - FunctionPrototypeCall, | ||
| 37 | 36 | NumberMAX_SAFE_INTEGER, | |
| 38 | 37 | ObjectDefineProperties, | |
| 39 | 38 | ObjectDefineProperty, | |
@@ -215,6 +214,7 @@ module.exports.once = once; | |||
| 215 | 214 | module.exports.on = on; | |
| 216 | 215 | module.exports.getEventListeners = getEventListeners; | |
| 217 | 216 | module.exports.getMaxListeners = getMaxListeners; | |
| 217 | + module.exports.listenerCount = listenerCount; | ||
| 218 | 218 | // Backwards-compat with node 0.10.x | |
| 219 | 219 | EventEmitter.EventEmitter = EventEmitter; | |
| 220 | 220 | ||
@@ -813,31 +813,14 @@ EventEmitter.prototype.rawListeners = function rawListeners(type) { | |||
| 813 | 813 | return _listeners(this, type, false); | |
| 814 | 814 | }; | |
| 815 | 815 | ||
| 816 | - /** | ||
| 817 | - * Returns the number of listeners listening to the event name | ||
| 818 | - * specified as `type`. | ||
| 819 | - * @deprecated since v3.2.0 | ||
| 820 | - * @param {EventEmitter} emitter | ||
| 821 | - * @param {string | symbol} type | ||
| 822 | - * @returns {number} | ||
| 823 | - */ | ||
| 824 | - EventEmitter.listenerCount = function(emitter, type) { | ||
| 825 | - if (typeof emitter.listenerCount === 'function') { | ||
| 826 | - return emitter.listenerCount(type); | ||
| 827 | - } | ||
| 828 | - return FunctionPrototypeCall(listenerCount, emitter, type); | ||
| 829 | - }; | ||
| 830 | - | ||
| 831 | - EventEmitter.prototype.listenerCount = listenerCount; | ||
| 832 | - | ||
| 833 | 816 | /** | |
| 834 | 817 | * Returns the number of listeners listening to event name | |
| 835 | 818 | * specified as `type`. | |
| 836 | 819 | * @param {string | symbol} type | |
| 837 | - * @param {Function} listener | ||
| 820 | + * @param {Function} [listener] | ||
| 838 | 821 | * @returns {number} | |
| 839 | 822 | */ | |
| 840 | - function listenerCount(type, listener) { | ||
| 823 | + EventEmitter.prototype.listenerCount = function listenerCount(type, listener) { | ||
| 841 | 824 | const events = this._events; | |
| 842 | 825 | ||
| 843 | 826 | if (events !== undefined) { | |
@@ -867,7 +850,7 @@ function listenerCount(type, listener) { | |||
| 867 | 850 | } | |
| 868 | 851 | ||
| 869 | 852 | return 0; | |
| 870 | - } | ||
| 853 | + }; | ||
| 871 | 854 | ||
| 872 | 855 | /** | |
| 873 | 856 | * Returns an array listing the events for which | |
@@ -949,6 +932,25 @@ function getMaxListeners(emitterOrTarget) { | |||
| 949 | 932 | emitterOrTarget); | |
| 950 | 933 | } | |
| 951 | 934 | ||
| 935 | + /** | ||
| 936 | + * Returns the number of registered listeners for `type`. | ||
| 937 | + * @param {EventEmitter | EventTarget} emitterOrTarget | ||
| 938 | + * @param {string | symbol} type | ||
| 939 | + * @returns {number} | ||
| 940 | + */ | ||
| 941 | + function listenerCount(emitterOrTarget, type) { | ||
| 942 | + if (typeof emitterOrTarget.listenerCount === 'function') { | ||
| 943 | + return emitterOrTarget.listenerCount(type); | ||
| 944 | + } | ||
| 945 | + const { isEventTarget, kEvents } = require('internal/event_target'); | ||
| 946 | + if (isEventTarget(emitterOrTarget)) { | ||
| 947 | + return emitterOrTarget[kEvents].get(type)?.size ?? 0; | ||
| 948 | + } | ||
| 949 | + throw new ERR_INVALID_ARG_TYPE('emitter', | ||
| 950 | + ['EventEmitter', 'EventTarget'], | ||
| 951 | + emitterOrTarget); | ||
| 952 | + } | ||
| 953 | + | ||
| 952 | 954 | /** | |
| 953 | 955 | * Creates a `Promise` that is fulfilled when the emitter | |
| 954 | 956 | * emits the given event. | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -59,7 +59,8 @@ Stream.prototype.pipe = function(dest, options) { | |||
| 59 | 59 | // Don't leave dangling pipes when there are errors. | |
| 60 | 60 | function onerror(er) { | |
| 61 | 61 | cleanup(); | |
| 62 | - if (EE.listenerCount(this, 'error') === 0) { | ||
| 62 | + // If we removed the last error handler, trigger an unhandled error event. | ||
| 63 | + if (this.listenerCount?.('error') === 0) { | ||
| 63 | 64 | this.emit('error', er); | |
| 64 | 65 | } | |
| 65 | 66 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -4,7 +4,7 @@ | |||
| 4 | 4 | const common = require('../common'); | |
| 5 | 5 | const { aborted } = require('util'); | |
| 6 | 6 | const assert = require('assert'); | |
| 7 | - const { getEventListeners } = require('events'); | ||
| 7 | + const { listenerCount } = require('events'); | ||
| 8 | 8 | const { inspect } = require('util'); | |
| 9 | 9 | ||
| 10 | 10 | const { | |
@@ -17,7 +17,7 @@ test('Aborted works when provided a resource', async () => { | |||
| 17 | 17 | ac.abort(); | |
| 18 | 18 | await promise; | |
| 19 | 19 | assert.strictEqual(ac.signal.aborted, true); | |
| 20 | - assert.strictEqual(getEventListeners(ac.signal, 'abort').length, 0); | ||
| 20 | + assert.strictEqual(listenerCount(ac.signal, 'abort'), 0); | ||
| 21 | 21 | }); | |
| 22 | 22 | ||
| 23 | 23 | test('Aborted with gc cleanup', async () => { | |
@@ -31,7 +31,7 @@ test('Aborted with gc cleanup', async () => { | |||
| 31 | 31 | globalThis.gc(); | |
| 32 | 32 | ac.abort(); | |
| 33 | 33 | assert.strictEqual(ac.signal.aborted, true); | |
| 34 | - assert.strictEqual(getEventListeners(ac.signal, 'abort').length, 0); | ||
| 34 | + assert.strictEqual(listenerCount(ac.signal, 'abort'), 0); | ||
| 35 | 35 | resolve(); | |
| 36 | 36 | })); | |
| 37 | 37 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -3,7 +3,7 @@ | |||
| 3 | 3 | const common = require('../common'); | |
| 4 | 4 | const assert = require('assert'); | |
| 5 | 5 | const { execFile, execFileSync } = require('child_process'); | |
| 6 | - const { getEventListeners } = require('events'); | ||
| 6 | + const { listenerCount } = require('events'); | ||
| 7 | 7 | const { getSystemErrorName } = require('util'); | |
| 8 | 8 | const fixtures = require('../common/fixtures'); | |
| 9 | 9 | const os = require('os'); | |
@@ -106,7 +106,7 @@ common.expectWarning( | |||
| 106 | 106 | const { signal } = ac; | |
| 107 | 107 | ||
| 108 | 108 | const callback = common.mustCall((err) => { | |
| 109 | - assert.strictEqual(getEventListeners(ac.signal).length, 0); | ||
| 109 | + assert.strictEqual(listenerCount(ac.signal, 'abort'), 0); | ||
| 110 | 110 | assert.strictEqual(err, null); | |
| 111 | 111 | }); | |
| 112 | 112 | execFile(process.execPath, [fixture, 0], { signal }, callback); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -4,7 +4,7 @@ const { mustCall } = require('../common'); | |||
| 4 | 4 | const assert = require('assert'); | |
| 5 | 5 | const fixtures = require('../common/fixtures'); | |
| 6 | 6 | const { fork } = require('child_process'); | |
| 7 | - const { getEventListeners } = require('events'); | ||
| 7 | + const { listenerCount } = require('events'); | ||
| 8 | 8 | ||
| 9 | 9 | { | |
| 10 | 10 | // Verify default signal | |
@@ -43,8 +43,8 @@ const { getEventListeners } = require('events'); | |||
| 43 | 43 | timeout: 6, | |
| 44 | 44 | signal, | |
| 45 | 45 | }); | |
| 46 | - assert.strictEqual(getEventListeners(signal, 'abort').length, 1); | ||
| 46 | + assert.strictEqual(listenerCount(signal, 'abort'), 1); | ||
| 47 | 47 | cp.on('exit', mustCall(() => { | |
| 48 | - assert.strictEqual(getEventListeners(signal, 'abort').length, 0); | ||
| 48 | + assert.strictEqual(listenerCount(signal, 'abort'), 0); | ||
| 49 | 49 | })); | |
| 50 | 50 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -4,7 +4,7 @@ const { mustCall } = require('../common'); | |||
| 4 | 4 | const assert = require('assert'); | |
| 5 | 5 | const fixtures = require('../common/fixtures'); | |
| 6 | 6 | const { spawn } = require('child_process'); | |
| 7 | - const { getEventListeners } = require('events'); | ||
| 7 | + const { listenerCount } = require('events'); | ||
| 8 | 8 | ||
| 9 | 9 | const aliveForeverFile = 'child-process-stay-alive-forever.js'; | |
| 10 | 10 | { | |
@@ -43,8 +43,8 @@ const aliveForeverFile = 'child-process-stay-alive-forever.js'; | |||
| 43 | 43 | timeout: 6, | |
| 44 | 44 | signal, | |
| 45 | 45 | }); | |
| 46 | - assert.strictEqual(getEventListeners(signal, 'abort').length, 1); | ||
| 46 | + assert.strictEqual(listenerCount(signal, 'abort'), 1); | ||
| 47 | 47 | cp.on('exit', mustCall(() => { | |
| 48 | - assert.strictEqual(getEventListeners(signal, 'abort').length, 0); | ||
| 48 | + assert.strictEqual(listenerCount(signal, 'abort'), 0); | ||
| 49 | 49 | })); | |
| 50 | 50 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2,7 +2,7 @@ | |||
| 2 | 2 | // Flags: --no-warnings | |
| 3 | 3 | ||
| 4 | 4 | const common = require('../common'); | |
| 5 | - const { once, EventEmitter, getEventListeners } = require('events'); | ||
| 5 | + const { once, EventEmitter, listenerCount } = require('events'); | ||
| 6 | 6 | const assert = require('assert'); | |
| 7 | 7 | ||
| 8 | 8 | async function onceAnEvent() { | |
@@ -72,7 +72,7 @@ async function catchesErrorsWithAbortSignal() { | |||
| 72 | 72 | try { | |
| 73 | 73 | const promise = once(ee, 'myevent', { signal }); | |
| 74 | 74 | assert.strictEqual(ee.listenerCount('error'), 1); | |
| 75 | - assert.strictEqual(getEventListeners(signal, 'abort').length, 1); | ||
| 75 | + assert.strictEqual(listenerCount(signal, 'abort'), 1); | ||
| 76 | 76 | ||
| 77 | 77 | await promise; | |
| 78 | 78 | } catch (e) { | |
@@ -81,7 +81,7 @@ async function catchesErrorsWithAbortSignal() { | |||
| 81 | 81 | assert.strictEqual(err, expected); | |
| 82 | 82 | assert.strictEqual(ee.listenerCount('error'), 0); | |
| 83 | 83 | assert.strictEqual(ee.listenerCount('myevent'), 0); | |
| 84 | - assert.strictEqual(getEventListeners(signal, 'abort').length, 0); | ||
| 84 | + assert.strictEqual(listenerCount(signal, 'abort'), 0); | ||
| 85 | 85 | } | |
| 86 | 86 | ||
| 87 | 87 | async function stopListeningAfterCatchingError() { | |
@@ -191,9 +191,9 @@ async function abortSignalAfterEvent() { | |||
| 191 | 191 | ac.abort(); | |
| 192 | 192 | }); | |
| 193 | 193 | const promise = once(ee, 'foo', { signal: ac.signal }); | |
| 194 | - assert.strictEqual(getEventListeners(ac.signal, 'abort').length, 1); | ||
| 194 | + assert.strictEqual(listenerCount(ac.signal, 'abort'), 1); | ||
| 195 | 195 | await promise; | |
| 196 | - assert.strictEqual(getEventListeners(ac.signal, 'abort').length, 0); | ||
| 196 | + assert.strictEqual(listenerCount(ac.signal, 'abort'), 0); | ||
| 197 | 197 | } | |
| 198 | 198 | ||
| 199 | 199 | async function abortSignalRemoveListener() { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -9,7 +9,7 @@ const { | |||
| 9 | 9 | ||
| 10 | 10 | const assert = require('assert'); | |
| 11 | 11 | ||
| 12 | - const { once } = require('events'); | ||
| 12 | + const { listenerCount, once } = require('events'); | ||
| 13 | 13 | ||
| 14 | 14 | const { inspect } = require('util'); | |
| 15 | 15 | const { setTimeout: delay } = require('timers/promises'); | |
@@ -141,10 +141,13 @@ let asyncTest = Promise.resolve(); | |||
| 141 | 141 | ||
| 142 | 142 | eventTarget.addEventListener('foo', ev1); | |
| 143 | 143 | eventTarget.addEventListener('foo', ev2, { once: true }); | |
| 144 | + assert.strictEqual(listenerCount(eventTarget, 'foo'), 2); | ||
| 144 | 145 | assert.ok(eventTarget.dispatchEvent(new Event('foo'))); | |
| 146 | + assert.strictEqual(listenerCount(eventTarget, 'foo'), 1); | ||
| 145 | 147 | eventTarget.dispatchEvent(new Event('foo')); | |
| 146 | 148 | ||
| 147 | 149 | eventTarget.removeEventListener('foo', ev1); | |
| 150 | + assert.strictEqual(listenerCount(eventTarget, 'foo'), 0); | ||
| 148 | 151 | eventTarget.dispatchEvent(new Event('foo')); | |
| 149 | 152 | } | |
| 150 | 153 | { | |
| Back | FazBrowse Home | New Git URL |
0 commit comments