GeezerVison is showing github.com — events: deprecate static listenerCount function · nodejs/node@3e6a6fc
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: benchmark/events/ee-listener-count-on-prototype.js +1-2Lines changed: 1 addition & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -7,14 +7,13 @@ function main(conf) { | 7 | 7 | var n = conf.n | 0; | 8 | 8 |
| 9 | 9 | var ee = new EventEmitter(); | 10 | | - var listenerCount = EventEmitter.listenerCount; | 11 | 10 |
| 12 | 11 | for (var k = 0; k < 10; k += 1) | 13 | 12 | ee.on('dummy', function() {}); | 14 | 13 |
| 15 | 14 | bench.start(); | 16 | 15 | for (var i = 0; i < n; i += 1) { | 17 | | - var r = listenerCount(ee, 'dummy'); | | 16 | + var r = ee.listenerCount('dummy'); | 18 | 17 | } | 19 | 18 | bench.end(n); | 20 | 19 | } |
Collapse file Copy file name to clipboardExpand all lines: doc/api/events.markdown +7Lines changed: 7 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -137,10 +137,17 @@ Execute each of the listeners in order with the supplied arguments. | 137 | 137 | Returns `true` if event had listeners, `false` otherwise. | 138 | 138 |
| 139 | 139 |
| | 140 | +### emitter.listenerCount(type) | | 141 | + | | 142 | +* `type` {Value} The type of event | | 143 | + | | 144 | +Returns the number of listeners listening to the `type` of event. | | 145 | + | 140 | 146 | ### Class Method: EventEmitter.listenerCount(emitter, event) | 141 | 147 |
| 142 | 148 | Return the number of listeners for a given event. | 143 | 149 |
| | 150 | +_Note: This is deprecated. Use `emitter.listenerCount` instead._ | 144 | 151 |
| 145 | 152 | ### Event: 'newListener' | 146 | 153 |
| |
Collapse file Copy file name to clipboardExpand all lines: lib/_http_client.js +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -322,7 +322,7 @@ function socketOnData(d) { | 322 | 322 | var bodyHead = d.slice(bytesParsed, d.length); | 323 | 323 |
| 324 | 324 | var eventName = req.method === 'CONNECT' ? 'connect' : 'upgrade'; | 325 | | - if (EventEmitter.listenerCount(req, eventName) > 0) { | | 325 | + if (req.listenerCount(eventName) > 0) { | 326 | 326 | req.upgradeOrConnect = true; | 327 | 327 |
| 328 | 328 | // detach the socket | |
Collapse file Copy file name to clipboardExpand all lines: lib/_http_server.js +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -343,7 +343,7 @@ function connectionListener(socket) { | 343 | 343 | parser = null; | 344 | 344 |
| 345 | 345 | var eventName = req.method === 'CONNECT' ? 'connect' : 'upgrade'; | 346 | | - if (EventEmitter.listenerCount(self, eventName) > 0) { | | 346 | + if (self.listenerCount(eventName) > 0) { | 347 | 347 | debug('SERVER have listener for %s', eventName); | 348 | 348 | var bodyHead = d.slice(bytesParsed, d.length); | 349 | 349 |
| @@ -467,7 +467,7 @@ function connectionListener(socket) { | 467 | 467 | (req.httpVersionMajor == 1 && req.httpVersionMinor == 1) && | 468 | 468 | continueExpression.test(req.headers['expect'])) { | 469 | 469 | res._expect_continue = true; | 470 | | - if (EventEmitter.listenerCount(self, 'checkContinue') > 0) { | | 470 | + if (self.listenerCount('checkContinue') > 0) { | 471 | 471 | self.emit('checkContinue', req, res); | 472 | 472 | } else { | 473 | 473 | res.writeContinue(); | |
Collapse file Copy file name to clipboardExpand all lines: lib/_stream_readable.js +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -533,7 +533,7 @@ Readable.prototype.pipe = function(dest, pipeOpts) { | 533 | 533 | debug('onerror', er); | 534 | 534 | unpipe(); | 535 | 535 | dest.removeListener('error', onerror); | 536 | | - if (EE.listenerCount(dest, 'error') === 0) | | 536 | + if (dest.listenerCount('error') === 0) | 537 | 537 | dest.emit('error', er); | 538 | 538 | } | 539 | 539 | // This is a brutally ugly hack to make sure that our error handler | @@ -582,7 +582,7 @@ function pipeOnDrain(src) { | 582 | 582 | debug('pipeOnDrain', state.awaitDrain); | 583 | 583 | if (state.awaitDrain) | 584 | 584 | state.awaitDrain--; | 585 | | - if (state.awaitDrain === 0 && EE.listenerCount(src, 'data')) { | | 585 | + if (state.awaitDrain === 0 && src.listenerCount('data')) { | 586 | 586 | state.flowing = true; | 587 | 587 | flow(src); | 588 | 588 | } | |
Collapse file Copy file name to clipboardExpand all lines: lib/_tls_wrap.js +4-5Lines changed: 4 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -5,7 +5,6 @@ const crypto = require('crypto'); | 5 | 5 | const net = require('net'); | 6 | 6 | const tls = require('tls'); | 7 | 7 | const util = require('util'); | 8 | | -const listenerCount = require('events').listenerCount; | 9 | 8 | const common = require('_tls_common'); | 10 | 9 | const StreamWrap = require('_stream_wrap').StreamWrap; | 11 | 10 | const Buffer = require('buffer').Buffer; | @@ -116,7 +115,7 @@ function requestOCSP(self, hello, ctx, cb) { | 116 | 115 | if (ctx.context) | 117 | 116 | ctx = ctx.context; | 118 | 117 |
| 119 | | - if (listenerCount(self.server, 'OCSPRequest') === 0) { | | 118 | + if (self.server.listenerCount('OCSPRequest') === 0) { | 120 | 119 | return cb(null); | 121 | 120 | } else { | 122 | 121 | self.server.emit('OCSPRequest', | @@ -396,11 +395,11 @@ TLSSocket.prototype._init = function(socket, wrap) { | 396 | 395 | ssl.handshakes = 0; | 397 | 396 |
| 398 | 397 | if (this.server) { | 399 | | - if (listenerCount(this.server, 'resumeSession') > 0 || | 400 | | - listenerCount(this.server, 'newSession') > 0) { | | 398 | + if (this.server.listenerCount('resumeSession') > 0 || | | 399 | + this.server.listenerCount('newSession') > 0) { | 401 | 400 | ssl.enableSessionCallbacks(); | 402 | 401 | } | 403 | | - if (listenerCount(this.server, 'OCSPRequest') > 0) | | 402 | + if (this.server.listenerCount('OCSPRequest') > 0) | 404 | 403 | ssl.enableCertCb(); | 405 | 404 | } | 406 | 405 | } else { | |
Collapse file Copy file name to clipboardExpand all lines: lib/events.js +13-9Lines changed: 13 additions & 9 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -395,19 +395,23 @@ EventEmitter.prototype.listeners = function listeners(type) { | 395 | 395 | }; | 396 | 396 |
| 397 | 397 | EventEmitter.listenerCount = function(emitter, type) { | 398 | | - var evlistener; | 399 | | - var ret = 0; | 400 | | - var events = emitter._events; | | 398 | + return emitter.listenerCount(type); | | 399 | +}; | | 400 | + | | 401 | +EventEmitter.prototype.listenerCount = function listenerCount(type) { | | 402 | + const events = this._events; | 401 | 403 |
| 402 | 404 | if (events) { | 403 | | - evlistener = events[type]; | 404 | | - if (typeof evlistener === 'function') | 405 | | - ret = 1; | 406 | | - else if (evlistener) | 407 | | - ret = evlistener.length; | | 405 | + const evlistener = events[type]; | | 406 | + | | 407 | + if (typeof evlistener === 'function') { | | 408 | + return 1; | | 409 | + } else if (evlistener) { | | 410 | + return evlistener.length; | | 411 | + } | 408 | 412 | } | 409 | 413 |
| 410 | | - return ret; | | 414 | + return 0; | 411 | 415 | }; | 412 | 416 |
| 413 | 417 | // About 1.5x faster than the two-arg version of Array#splice(). | |
Collapse file Copy file name to clipboardExpand all lines: lib/fs.js +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -1354,7 +1354,7 @@ fs.unwatchFile = function(filename, listener) { | 1354 | 1354 | stat.removeAllListeners('change'); | 1355 | 1355 | } | 1356 | 1356 |
| 1357 | | - if (EventEmitter.listenerCount(stat, 'change') === 0) { | | 1357 | + if (stat.listenerCount('change') === 0) { | 1358 | 1358 | stat.stop(); | 1359 | 1359 | statWatchers.delete(filename); | 1360 | 1360 | } | |
Collapse file Copy file name to clipboardExpand all lines: lib/readline.js +4-4Lines changed: 4 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -683,7 +683,7 @@ Interface.prototype._ttyWrite = function(s, key) { | 683 | 683 |
| 684 | 684 | switch (key.name) { | 685 | 685 | case 'c': | 686 | | - if (EventEmitter.listenerCount(this, 'SIGINT') > 0) { | | 686 | + if (this.listenerCount('SIGINT') > 0) { | 687 | 687 | this.emit('SIGINT'); | 688 | 688 | } else { | 689 | 689 | // This readline instance is finished | @@ -746,7 +746,7 @@ Interface.prototype._ttyWrite = function(s, key) { | 746 | 746 |
| 747 | 747 | case 'z': | 748 | 748 | if (process.platform == 'win32') break; | 749 | | - if (EventEmitter.listenerCount(this, 'SIGTSTP') > 0) { | | 749 | + if (this.listenerCount('SIGTSTP') > 0) { | 750 | 750 | this.emit('SIGTSTP'); | 751 | 751 | } else { | 752 | 752 | process.once('SIGCONT', (function(self) { | @@ -907,7 +907,7 @@ function emitKeypressEvents(stream) { | 907 | 907 | stream[ESCAPE_DECODER].next(); | 908 | 908 |
| 909 | 909 | function onData(b) { | 910 | | - if (EventEmitter.listenerCount(stream, 'keypress') > 0) { | | 910 | + if (stream.listenerCount('keypress') > 0) { | 911 | 911 | var r = stream[KEYPRESS_DECODER].write(b); | 912 | 912 | if (r) { | 913 | 913 | for (var i = 0; i < r.length; i++) { | @@ -936,7 +936,7 @@ function emitKeypressEvents(stream) { | 936 | 936 | } | 937 | 937 | } | 938 | 938 |
| 939 | | - if (EventEmitter.listenerCount(stream, 'keypress') > 0) { | | 939 | + if (stream.listenerCount('keypress') > 0) { | 940 | 940 | stream.on('data', onData); | 941 | 941 | } else { | 942 | 942 | stream.on('newListener', onNewListener); | |
Collapse file Copy file name to clipboardExpand all lines: lib/stream.js +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -70,7 +70,7 @@ Stream.prototype.pipe = function(dest, options) { | 70 | 70 | // don't leave dangling pipes when there are errors. | 71 | 71 | function onerror(er) { | 72 | 72 | cleanup(); | 73 | | - if (EE.listenerCount(this, 'error') === 0) { | | 73 | + if (this.listenerCount('error') === 0) { | 74 | 74 | throw er; // Unhandled stream error in pipe. | 75 | 75 | } | 76 | 76 | } | |
|