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);
}
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'
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
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);
(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();
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
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);
}
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;
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',
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 {
};
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().
stat.removeAllListeners('change');
}
- if (EventEmitter.listenerCount(stat, 'change') === 0) {
+ if (stat.listenerCount('change') === 0) {
stat.stop();
statWatchers.delete(filename);
}
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
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) {
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++) {
}
}
- if (EventEmitter.listenerCount(stream, 'keypress') > 0) {
+ if (stream.listenerCount('keypress') > 0) {
stream.on('data', onData);
} else {
stream.on('newListener', onNewListener);
// 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.
}
}
});
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];
}
--- /dev/null
+'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);
ee1.on('x', function() {});
-assert.equal(EventEmitter.listenerCount(ee2, 'x'), 0);
+assert.equal(ee2.listenerCount('x'), 0);