Skip to content

Commit

Permalink
Implement event names function
Browse files Browse the repository at this point in the history
  • Loading branch information
sebakerckhof committed Mar 21, 2017
1 parent 45256b0 commit ab1cdfb
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
12 changes: 12 additions & 0 deletions README.md
Expand Up @@ -328,3 +328,15 @@ Only this method doesn't work [IE](http://caniuse.com/#search=promise).
console.log(results); // [3,2,1,0,undefined]
});
```

### emitter.eventNames()

Returns an array listing the events for which the emitter has registered listeners. The values in the array will be strings.

```javascript
emitter.on('foo', () => {});
emitter.on('bar', () => {});

console.log(emitter.eventNames());
// Prints: [ 'foo', 'bar' ]
```
20 changes: 12 additions & 8 deletions lib/eventemitter2.js
Expand Up @@ -24,7 +24,8 @@
this._conf = conf;

conf.delimiter && (this.delimiter = conf.delimiter);
this._events.maxListeners = conf.maxListeners !== undefined ? conf.maxListeners : defaultMaxListeners;
this._maxListeners = conf.maxListeners !== undefined ? conf.maxListeners : defaultMaxListeners;

conf.wildcard && (this.wildcard = conf.wildcard);
conf.newListener && (this.newListener = conf.newListener);
conf.verboseMemoryLeak && (this.verboseMemoryLeak = conf.verboseMemoryLeak);
Expand All @@ -33,7 +34,7 @@
this.listenerTree = {};
}
} else {
this._events.maxListeners = defaultMaxListeners;
this._maxListeners = defaultMaxListeners;
}
}

Expand Down Expand Up @@ -211,8 +212,8 @@

if (
!tree._listeners.warned &&
this._events.maxListeners > 0 &&
tree._listeners.length > this._events.maxListeners
this._maxListeners > 0 &&
tree._listeners.length > this._maxListeners
) {
tree._listeners.warned = true;
logPossibleMemoryLeak.call(this, tree._listeners.length, name);
Expand All @@ -236,8 +237,7 @@

EventEmitter.prototype.setMaxListeners = function(n) {
if (n !== undefined) {
this._events || init.call(this);
this._events.maxListeners = n;
this._maxListeners = n;
if (!this._conf) this._conf = {};
this._conf.maxListeners = n;
}
Expand Down Expand Up @@ -568,8 +568,8 @@
// Check for listener leak
if (
!this._events[type].warned &&
this._events.maxListeners > 0 &&
this._events[type].length > this._events.maxListeners
this._maxListeners > 0 &&
this._events[type].length > this._maxListeners
) {
this._events[type].warned = true;
logPossibleMemoryLeak.call(this, this._events[type].length, type);
Expand Down Expand Up @@ -734,6 +734,10 @@
return this._events[type];
};

EventEmitter.prototype.eventNames = function(){
return Object.keys(this._events);
}

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

0 comments on commit ab1cdfb

Please sign in to comment.