Skip to content

Commit

Permalink
events: deprecate static listenerCount function
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
thefourtheye committed Aug 19, 2015
1 parent d98eed5 commit 8f58fb9
Show file tree
Hide file tree
Showing 13 changed files with 56 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@ function main(conf) {
var n = conf.n | 0;

var ee = new EventEmitter();
var listenerCount = EventEmitter.listenerCount;

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

bench.start();
for (var i = 0; i < n; i += 1) {
var r = listenerCount(ee, 'dummy');
var r = ee.listenerCount('dummy');
}
bench.end(n);
}
7 changes: 7 additions & 0 deletions doc/api/events.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,17 @@ Execute each of the listeners in order with the supplied arguments.
Returns `true` if event had listeners, `false` otherwise.


### emitter.listenerCount(type)

* `type` {Value} The type of event

Returns the number of listeners listening to the `type` of event.

### Class Method: EventEmitter.listenerCount(emitter, event)

Return the number of listeners for a given event.

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

### Event: 'newListener'

Expand Down
2 changes: 1 addition & 1 deletion lib/_http_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ function socketOnData(d) {
var bodyHead = d.slice(bytesParsed, d.length);

var eventName = req.method === 'CONNECT' ? 'connect' : 'upgrade';
if (EventEmitter.listenerCount(req, eventName) > 0) {
if (req.listenerCount(eventName) > 0) {
req.upgradeOrConnect = true;

// detach the socket
Expand Down
4 changes: 2 additions & 2 deletions lib/_http_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ function connectionListener(socket) {
parser = null;

var eventName = req.method === 'CONNECT' ? 'connect' : 'upgrade';
if (EventEmitter.listenerCount(self, eventName) > 0) {
if (self.listenerCount(eventName) > 0) {
debug('SERVER have listener for %s', eventName);
var bodyHead = d.slice(bytesParsed, d.length);

Expand Down Expand Up @@ -467,7 +467,7 @@ function connectionListener(socket) {
(req.httpVersionMajor == 1 && req.httpVersionMinor == 1) &&
continueExpression.test(req.headers['expect'])) {
res._expect_continue = true;
if (EventEmitter.listenerCount(self, 'checkContinue') > 0) {
if (self.listenerCount('checkContinue') > 0) {
self.emit('checkContinue', req, res);
} else {
res.writeContinue();
Expand Down
4 changes: 2 additions & 2 deletions lib/_stream_readable.js
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,7 @@ Readable.prototype.pipe = function(dest, pipeOpts) {
debug('onerror', er);
unpipe();
dest.removeListener('error', onerror);
if (EE.listenerCount(dest, 'error') === 0)
if (dest.listenerCount('error') === 0)
dest.emit('error', er);
}
// This is a brutally ugly hack to make sure that our error handler
Expand Down Expand Up @@ -582,7 +582,7 @@ function pipeOnDrain(src) {
debug('pipeOnDrain', state.awaitDrain);
if (state.awaitDrain)
state.awaitDrain--;
if (state.awaitDrain === 0 && EE.listenerCount(src, 'data')) {
if (state.awaitDrain === 0 && src.listenerCount('data')) {
state.flowing = true;
flow(src);
}
Expand Down
9 changes: 4 additions & 5 deletions lib/_tls_wrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ const crypto = require('crypto');
const net = require('net');
const tls = require('tls');
const util = require('util');
const listenerCount = require('events').listenerCount;
const common = require('_tls_common');
const StreamWrap = require('_stream_wrap').StreamWrap;
const Buffer = require('buffer').Buffer;
Expand Down Expand Up @@ -116,7 +115,7 @@ function requestOCSP(self, hello, ctx, cb) {
if (ctx.context)
ctx = ctx.context;

if (listenerCount(self.server, 'OCSPRequest') === 0) {
if (self.server.listenerCount('OCSPRequest') === 0) {
return cb(null);
} else {
self.server.emit('OCSPRequest',
Expand Down Expand Up @@ -396,11 +395,11 @@ TLSSocket.prototype._init = function(socket, wrap) {
ssl.handshakes = 0;

if (this.server) {
if (listenerCount(this.server, 'resumeSession') > 0 ||
listenerCount(this.server, 'newSession') > 0) {
if (this.server.listenerCount('resumeSession') > 0 ||
this.server.listenerCount('newSession') > 0) {
ssl.enableSessionCallbacks();
}
if (listenerCount(this.server, 'OCSPRequest') > 0)
if (this.server.listenerCount('OCSPRequest') > 0)
ssl.enableCertCb();
}
} else {
Expand Down
22 changes: 13 additions & 9 deletions lib/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -395,19 +395,23 @@ EventEmitter.prototype.listeners = function listeners(type) {
};

EventEmitter.listenerCount = function(emitter, type) {
var evlistener;
var ret = 0;
var events = emitter._events;
return emitter.listenerCount(type);
};

EventEmitter.prototype.listenerCount = function listenerCount(type) {
const events = this._events;

if (events) {
evlistener = events[type];
if (typeof evlistener === 'function')
ret = 1;
else if (evlistener)
ret = evlistener.length;
const evlistener = events[type];

if (typeof evlistener === 'function') {
return 1;
} else if (evlistener) {
return evlistener.length;
}
}

return ret;
return 0;
};

// About 1.5x faster than the two-arg version of Array#splice().
Expand Down
2 changes: 1 addition & 1 deletion lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -1354,7 +1354,7 @@ fs.unwatchFile = function(filename, listener) {
stat.removeAllListeners('change');
}

if (EventEmitter.listenerCount(stat, 'change') === 0) {
if (stat.listenerCount('change') === 0) {
stat.stop();
statWatchers.delete(filename);
}
Expand Down
8 changes: 4 additions & 4 deletions lib/readline.js
Original file line number Diff line number Diff line change
Expand Up @@ -683,7 +683,7 @@ Interface.prototype._ttyWrite = function(s, key) {

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

case 'z':
if (process.platform == 'win32') break;
if (EventEmitter.listenerCount(this, 'SIGTSTP') > 0) {
if (this.listenerCount('SIGTSTP') > 0) {
this.emit('SIGTSTP');
} else {
process.once('SIGCONT', (function(self) {
Expand Down Expand Up @@ -907,7 +907,7 @@ function emitKeypressEvents(stream) {
stream[ESCAPE_DECODER].next();

function onData(b) {
if (EventEmitter.listenerCount(stream, 'keypress') > 0) {
if (stream.listenerCount('keypress') > 0) {
var r = stream[KEYPRESS_DECODER].write(b);
if (r) {
for (var i = 0; i < r.length; i++) {
Expand Down Expand Up @@ -936,7 +936,7 @@ function emitKeypressEvents(stream) {
}
}

if (EventEmitter.listenerCount(stream, 'keypress') > 0) {
if (stream.listenerCount('keypress') > 0) {
stream.on('data', onData);
} else {
stream.on('newListener', onNewListener);
Expand Down
2 changes: 1 addition & 1 deletion lib/stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ Stream.prototype.pipe = function(dest, options) {
// don't leave dangling pipes when there are errors.
function onerror(er) {
cleanup();
if (EE.listenerCount(this, 'error') === 0) {
if (this.listenerCount('error') === 0) {
throw er; // Unhandled stream error in pipe.
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -802,8 +802,7 @@
});

process.on('removeListener', function(type, listener) {
if (signalWraps.hasOwnProperty(type) &&
NativeModule.require('events').listenerCount(this, type) === 0) {
if (signalWraps.hasOwnProperty(type) && this.listenerCount(type) === 0) {
signalWraps[type].close();
delete signalWraps[type];
}
Expand Down
18 changes: 18 additions & 0 deletions test/parallel/test-event-emitter-listener-count.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict';

const common = require('../common');
const assert = require('assert');
const EventEmitter = require('events');

const emitter = new EventEmitter();
emitter.on('foo', function() {});
emitter.on('foo', function() {});
emitter.on('baz', function() {});
// Allow any type
emitter.on(123, function() {});

assert.strictEqual(EventEmitter.listenerCount(emitter, 'foo'), 2);
assert.strictEqual(emitter.listenerCount('foo'), 2);
assert.strictEqual(emitter.listenerCount('bar'), 0);
assert.strictEqual(emitter.listenerCount('baz'), 1);
assert.strictEqual(emitter.listenerCount(123), 1);
2 changes: 1 addition & 1 deletion test/parallel/test-event-emitter-subclass.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,4 @@ var ee2 = new MyEE2();

ee1.on('x', function() {});

assert.equal(EventEmitter.listenerCount(ee2, 'x'), 0);
assert.equal(ee2.listenerCount('x'), 0);

0 comments on commit 8f58fb9

Please sign in to comment.