Skip to content

Commit

Permalink
Merge pull request #183 from emschwartz/master
Browse files Browse the repository at this point in the history
Add listenerCount method
  • Loading branch information
RangerMauve committed Aug 4, 2016
2 parents bb6ffd5 + ecc2044 commit 35a56ed
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/eventemitter2.js
Expand Up @@ -694,6 +694,10 @@
return this._events[type];
};

EventEmitter.prototype.listenerCount = function(type) {
return this.listeners(type).length;
};

EventEmitter.prototype.listenersAny = function() {

if(this._all) {
Expand Down
24 changes: 24 additions & 0 deletions test/simple/addListener.js
Expand Up @@ -194,5 +194,29 @@ module.exports = simpleEvents({
test.expect(2);
test.done();

},

'11. listenerCount should return the number of listeners': function (test) {


var emitter = new EventEmitter2({ verbose: true });

test.equal(emitter.listenerCount('test1'), 0, 'Before adding listeners listenerCount is 0')

emitter.on('test1', function () {
test.ok(true, 'The event was raised');
});

test.equal(emitter.listenerCount('test1'), 1, 'After adding a listener listenerCount is 1')

emitter.on('test1', function () {
test.ok(true, 'The event was raised');
});

test.equal(emitter.listeners('test1').length, 2, 'And then there were 2');

test.expect(3);
test.done();

}
});

0 comments on commit 35a56ed

Please sign in to comment.