Skip to content

Commit 3e6a6fc

Browse files
thefourtheyervagg
authored andcommitted
events: deprecate static listenerCount function
As per the discussion in #734, this patch deprecates the usage of `EventEmitter.listenerCount` static function in the docs, and introduces the `listenerCount` function in the prototype of `EventEmitter` itself. PR-URL: #2349 Reviewed-By: Trevor Norris <trev.norris@gmail.com> Reviewed-By: Brian White <mscdex@mscdex.net>
1 parent 8576324 commit 3e6a6fc

13 files changed

Lines changed: 56 additions & 30 deletions

benchmark/events/ee-listener-count.js renamed to benchmark/events/ee-listener-count-on-prototype.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,13 @@ function main(conf) {
77
varn=conf.n|0;
88

99
varee=newEventEmitter();
10-
varlistenerCount=EventEmitter.listenerCount;
1110

1211
for(vark=0;k<10;k+=1)
1312
ee.on('dummy',function(){});
1413

1514
bench.start();
1615
for(vari=0;i<n;i+=1){
17-
varr=listenerCount(ee,'dummy');
16+
varr=ee.listenerCount('dummy');
1817
}
1918
bench.end(n);
2019
}

‎doc/api/events.markdown‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,10 +137,17 @@ Execute each of the listeners in order with the supplied arguments.
137137
Returns `true` if event had listeners, `false` otherwise.
138138

139139

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+
140146
### Class Method: EventEmitter.listenerCount(emitter, event)
141147

142148
Return the number of listeners for a given event.
143149

150+
_Note: This is deprecated. Use `emitter.listenerCount` instead._
144151

145152
### Event: 'newListener'
146153

‎lib/_http_client.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ function socketOnData(d) {
322322
varbodyHead=d.slice(bytesParsed,d.length);
323323

324324
vareventName=req.method==='CONNECT' ? 'connect' : 'upgrade';
325-
if(EventEmitter.listenerCount(req,eventName)>0){
325+
if(req.listenerCount(eventName)>0){
326326
req.upgradeOrConnect=true;
327327

328328
// detach the socket

‎lib/_http_server.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ function connectionListener(socket) {
343343
parser=null;
344344

345345
vareventName=req.method==='CONNECT' ? 'connect' : 'upgrade';
346-
if(EventEmitter.listenerCount(self,eventName)>0){
346+
if(self.listenerCount(eventName)>0){
347347
debug('SERVER have listener for %s',eventName);
348348
varbodyHead=d.slice(bytesParsed,d.length);
349349

@@ -467,7 +467,7 @@ function connectionListener(socket) {
467467
(req.httpVersionMajor==1&&req.httpVersionMinor==1)&&
468468
continueExpression.test(req.headers['expect'])){
469469
res._expect_continue=true;
470-
if(EventEmitter.listenerCount(self,'checkContinue')>0){
470+
if(self.listenerCount('checkContinue')>0){
471471
self.emit('checkContinue',req,res);
472472
}else{
473473
res.writeContinue();

‎lib/_stream_readable.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,7 @@ Readable.prototype.pipe = function(dest, pipeOpts) {
533533
debug('onerror',er);
534534
unpipe();
535535
dest.removeListener('error',onerror);
536-
if(EE.listenerCount(dest,'error')===0)
536+
if(dest.listenerCount('error')===0)
537537
dest.emit('error',er);
538538
}
539539
// This is a brutally ugly hack to make sure that our error handler
@@ -582,7 +582,7 @@ function pipeOnDrain(src) {
582582
debug('pipeOnDrain',state.awaitDrain);
583583
if(state.awaitDrain)
584584
state.awaitDrain--;
585-
if(state.awaitDrain===0&&EE.listenerCount(src,'data')){
585+
if(state.awaitDrain===0&&src.listenerCount('data')){
586586
state.flowing=true;
587587
flow(src);
588588
}

‎lib/_tls_wrap.js‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ const crypto = require('crypto');
55
constnet=require('net');
66
consttls=require('tls');
77
constutil=require('util');
8-
constlistenerCount=require('events').listenerCount;
98
constcommon=require('_tls_common');
109
constStreamWrap=require('_stream_wrap').StreamWrap;
1110
constBuffer=require('buffer').Buffer;
@@ -116,7 +115,7 @@ function requestOCSP(self, hello, ctx, cb) {
116115
if(ctx.context)
117116
ctx=ctx.context;
118117

119-
if(listenerCount(self.server,'OCSPRequest')===0){
118+
if(self.server.listenerCount('OCSPRequest')===0){
120119
returncb(null);
121120
}else{
122121
self.server.emit('OCSPRequest',
@@ -396,11 +395,11 @@ TLSSocket.prototype._init = function(socket, wrap) {
396395
ssl.handshakes=0;
397396

398397
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){
401400
ssl.enableSessionCallbacks();
402401
}
403-
if(listenerCount(this.server,'OCSPRequest')>0)
402+
if(this.server.listenerCount('OCSPRequest')>0)
404403
ssl.enableCertCb();
405404
}
406405
}else{

‎lib/events.js‎

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -395,19 +395,23 @@ EventEmitter.prototype.listeners = function listeners(type) {
395395
};
396396

397397
EventEmitter.listenerCount=function(emitter,type){
398-
varevlistener;
399-
varret=0;
400-
varevents=emitter._events;
398+
returnemitter.listenerCount(type);
399+
};
400+
401+
EventEmitter.prototype.listenerCount=functionlistenerCount(type){
402+
constevents=this._events;
401403

402404
if(events){
403-
evlistener=events[type];
404-
if(typeofevlistener==='function')
405-
ret=1;
406-
elseif(evlistener)
407-
ret=evlistener.length;
405+
constevlistener=events[type];
406+
407+
if(typeofevlistener==='function'){
408+
return1;
409+
}elseif(evlistener){
410+
returnevlistener.length;
411+
}
408412
}
409413

410-
returnret;
414+
return0;
411415
};
412416

413417
// About 1.5x faster than the two-arg version of Array#splice().

‎lib/fs.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1354,7 +1354,7 @@ fs.unwatchFile = function(filename, listener) {
13541354
stat.removeAllListeners('change');
13551355
}
13561356

1357-
if(EventEmitter.listenerCount(stat,'change')===0){
1357+
if(stat.listenerCount('change')===0){
13581358
stat.stop();
13591359
statWatchers.delete(filename);
13601360
}

‎lib/readline.js‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -683,7 +683,7 @@ Interface.prototype._ttyWrite = function(s, key) {
683683

684684
switch(key.name){
685685
case'c':
686-
if(EventEmitter.listenerCount(this,'SIGINT')>0){
686+
if(this.listenerCount('SIGINT')>0){
687687
this.emit('SIGINT');
688688
}else{
689689
// This readline instance is finished
@@ -746,7 +746,7 @@ Interface.prototype._ttyWrite = function(s, key) {
746746

747747
case'z':
748748
if(process.platform=='win32')break;
749-
if(EventEmitter.listenerCount(this,'SIGTSTP')>0){
749+
if(this.listenerCount('SIGTSTP')>0){
750750
this.emit('SIGTSTP');
751751
}else{
752752
process.once('SIGCONT',(function(self){
@@ -907,7 +907,7 @@ function emitKeypressEvents(stream) {
907907
stream[ESCAPE_DECODER].next();
908908

909909
functiononData(b){
910-
if(EventEmitter.listenerCount(stream,'keypress')>0){
910+
if(stream.listenerCount('keypress')>0){
911911
varr=stream[KEYPRESS_DECODER].write(b);
912912
if(r){
913913
for(vari=0;i<r.length;i++){
@@ -936,7 +936,7 @@ function emitKeypressEvents(stream) {
936936
}
937937
}
938938

939-
if(EventEmitter.listenerCount(stream,'keypress')>0){
939+
if(stream.listenerCount('keypress')>0){
940940
stream.on('data',onData);
941941
}else{
942942
stream.on('newListener',onNewListener);

‎lib/stream.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ Stream.prototype.pipe = function(dest, options) {
7070
// don't leave dangling pipes when there are errors.
7171
functiononerror(er){
7272
cleanup();
73-
if(EE.listenerCount(this,'error')===0){
73+
if(this.listenerCount('error')===0){
7474
thrower;// Unhandled stream error in pipe.
7575
}
7676
}

0 commit comments

Comments
 (0)