Skip to content
This repository was archived by the owner on Apr 22, 2023. It is now read-only.

Commit 75305f3

Browse files
trevnorrisisaacs
authored andcommitted
events: add check for listeners length
Ability to return just the length of listeners for a given type, using EventEmitter.listenerCount(emitter, event). This will be a lot cheaper than creating a copy of the listeners array just to check its length.
1 parent 7707acd commit 75305f3

File tree

7 files changed

+32
-14
lines changed

7 files changed

+32
-14
lines changed

doc/api/events.markdown

+6
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,12 @@ Returns an array of listeners for the specified event.
8787

8888
Execute each of the listeners in order with the supplied arguments.
8989

90+
91+
### Class Method: EventEmitter.listenerCount(emitter, event)
92+
93+
Return the number of listeners for a given event.
94+
95+
9096
### Event: 'newListener'
9197

9298
* `event` {String} The event name

lib/_stream_readable.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
module.exports = Readable;
2323
Readable.ReadableState = ReadableState;
2424

25+
var EE = require('events').EventEmitter;
2526
var Stream = require('stream');
2627
var util = require('util');
2728
var StringDecoder;
@@ -451,7 +452,7 @@ Readable.prototype.pipe = function(dest, pipeOpts) {
451452
// however, don't suppress the throwing behavior for this.
452453
function onerror(er) {
453454
unpipe();
454-
if (dest.listeners('error').length === 0)
455+
if (EE.listenerCount(dest, 'error') === 0)
455456
dest.emit('error', er);
456457
}
457458
dest.once('error', onerror);
@@ -537,7 +538,7 @@ function flow(src) {
537538
state.flowing = false;
538539

539540
// if there were data event listeners added, then switch to old mode.
540-
if (src.listeners('data').length)
541+
if (EE.listenerCount(src, 'data') > 0)
541542
emitDataEvents(src);
542543
return;
543544
}

lib/events.js

+11
Original file line numberDiff line numberDiff line change
@@ -286,3 +286,14 @@ EventEmitter.prototype.listeners = function(type) {
286286
}
287287
return this._events[type].slice(0);
288288
};
289+
290+
EventEmitter.listenerCount = function(emitter, type) {
291+
var ret;
292+
if (!emitter._events || !emitter._events[type])
293+
ret = 0;
294+
else if (typeof emitter._events[type] === 'function')
295+
ret = 1;
296+
else
297+
ret = emitter._events[type].length;
298+
return ret;
299+
};

lib/fs.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1161,7 +1161,7 @@ fs.unwatchFile = function(filename, listener) {
11611161
stat.removeAllListeners('change');
11621162
}
11631163

1164-
if (stat.listeners('change').length === 0) {
1164+
if (EventEmitter.listenerCount(stat, 'change') === 0) {
11651165
stat.stop();
11661166
statWatchers[filename] = undefined;
11671167
}

lib/http.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1523,7 +1523,7 @@ function socketOnData(d, start, end) {
15231523
var bodyHead = d.slice(start + bytesParsed, end);
15241524

15251525
var eventName = req.method === 'CONNECT' ? 'connect' : 'upgrade';
1526-
if (req.listeners(eventName).length) {
1526+
if (EventEmitter.listenerCount(req, eventName) > 0) {
15271527
req.upgradeOrConnect = true;
15281528

15291529
// detach the socket
@@ -1874,7 +1874,7 @@ function connectionListener(socket) {
18741874
var bodyHead = d.slice(start + bytesParsed, end);
18751875

18761876
var eventName = req.method === 'CONNECT' ? 'connect' : 'upgrade';
1877-
if (self.listeners(eventName).length) {
1877+
if (EventEmitter.listenerCount(self, eventName) > 0) {
18781878
self.emit(eventName, req, req.socket, bodyHead);
18791879
} else {
18801880
// Got upgrade header or CONNECT method, but have no handler.
@@ -1958,7 +1958,7 @@ function connectionListener(socket) {
19581958
(req.httpVersionMajor == 1 && req.httpVersionMinor == 1) &&
19591959
continueExpression.test(req.headers['expect'])) {
19601960
res._expect_continue = true;
1961-
if (self.listeners('checkContinue').length) {
1961+
if (EventEmitter.listenerCount(self, 'checkContinue') > 0) {
19621962
self.emit('checkContinue', req, res);
19631963
} else {
19641964
res.writeContinue();

lib/readline.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -616,7 +616,7 @@ Interface.prototype._ttyWrite = function(s, key) {
616616

617617
switch (key.name) {
618618
case 'c':
619-
if (this.listeners('SIGINT').length) {
619+
if (EventEmitter.listenerCount(this, 'SIGINT') > 0) {
620620
this.emit('SIGINT');
621621
} else {
622622
// This readline instance is finished
@@ -673,7 +673,7 @@ Interface.prototype._ttyWrite = function(s, key) {
673673

674674
case 'z':
675675
if (process.platform == 'win32') break;
676-
if (this.listeners('SIGTSTP').length) {
676+
if (EventEmitter.listenerCount(this, 'SIGTSTP') > 0) {
677677
this.emit('SIGTSTP');
678678
} else {
679679
process.once('SIGCONT', (function(self) {
@@ -829,7 +829,7 @@ function emitKeypressEvents(stream) {
829829
stream._keypressDecoder = new StringDecoder('utf8');
830830

831831
function onData(b) {
832-
if (stream.listeners('keypress').length > 0) {
832+
if (EventEmitter.listenerCount(stream, 'keypress') > 0) {
833833
var r = stream._keypressDecoder.write(b);
834834
if (r) emitKey(stream, r);
835835
} else {
@@ -846,7 +846,7 @@ function emitKeypressEvents(stream) {
846846
}
847847
}
848848

849-
if (stream.listeners('keypress').length > 0) {
849+
if (EventEmitter.listenerCount(stream, 'keypress') > 0) {
850850
stream.on('data', onData);
851851
} else {
852852
stream.on('newListener', onNewListener);

lib/stream.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@
2121

2222
module.exports = Stream;
2323

24-
var events = require('events');
24+
var EE = require('events').EventEmitter;
2525
var util = require('util');
2626

27-
util.inherits(Stream, events.EventEmitter);
27+
util.inherits(Stream, EE);
2828
Stream.Readable = require('_stream_readable');
2929
Stream.Writable = require('_stream_writable');
3030
Stream.Duplex = require('_stream_duplex');
@@ -40,7 +40,7 @@ Stream.Stream = Stream;
4040
// part of this class) is overridden in the Readable class.
4141

4242
function Stream() {
43-
events.EventEmitter.call(this);
43+
EE.call(this);
4444
}
4545

4646
Stream.prototype.pipe = function(dest, options) {
@@ -90,7 +90,7 @@ Stream.prototype.pipe = function(dest, options) {
9090
// don't leave dangling pipes when there are errors.
9191
function onerror(er) {
9292
cleanup();
93-
if (this.listeners('error').length === 0) {
93+
if (EE.listenerCount(this, 'error') === 0) {
9494
throw er; // Unhandled stream error in pipe.
9595
}
9696
}

0 commit comments

Comments
 (0)