Skip to content

Commit

Permalink
Features emitter.getMaxListeners() & EventEmitter2.defaultMaxListeners (
Browse files Browse the repository at this point in the history
#247)

* Added waitFor method

* fixed README.md code example for waitFor method

* Closes #153 by using the current global scope instead of window

* added getMaxListeners method and defaultMaxListeners property

* added missing semicolons

* added the ability for defaultMaxListeners to set default max listeners for previously created instances;
added TypeScript typings;
added documentation for getMaxListeners() & defaultMaxListeners;
added documentation for emitter.listeners();
  • Loading branch information
DigitalBrainJS committed Mar 27, 2020
1 parent b6492c0 commit 9ca2fd0
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 8 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,11 @@ Obviously not all Emitters should be limited to 10. This function allows
that to be increased. Set to zero for unlimited.


### emitter.getMaxListeners()

Returns the current max listener value for the EventEmitter which is either set by emitter.setMaxListeners(n) or defaults to EventEmitter2.defaultMaxListeners


### emitter.listeners(event)

Returns an array of listeners for the specified event. This array can be
Expand Down Expand Up @@ -404,3 +409,11 @@ emitter.on('bar', () => {});
console.log(emitter.eventNames());
// Prints: [ 'foo', 'bar' ]
```

### emitter.listeners(eventName)

Returns the array of listeners for the event named eventName.

### EventEmitter2.defaultMaxListeners

Sets default max listeners count globally for all instances, including those created before the change is made.
2 changes: 2 additions & 0 deletions eventemitter2.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,10 @@ export declare class EventEmitter2 {
off(event: string, listener: Listener): this;
removeAllListeners(event?: string | eventNS): this;
setMaxListeners(n: number): void;
getMaxListeners(): number;
eventNames(): string[];
listeners(event: string | string[]): Listener[] // TODO: not in documentation by Willian
listenersAny(): Listener[] // TODO: not in documentation by Willian
waitFor(event: string, options?: WaitForOptions): WaitForThenable<any>
static defaultMaxListeners: number;
}
46 changes: 38 additions & 8 deletions lib/eventemitter2.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@
this._conf = conf;

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

if(conf.maxListeners!==undefined){
this._maxListeners= conf.maxListeners;
}

conf.wildcard && (this.wildcard = conf.wildcard);
conf.newListener && (this._newListener = conf.newListener);
Expand All @@ -35,8 +38,6 @@
if (this.wildcard) {
this.listenerTree = {};
}
} else {
this._maxListeners = defaultMaxListeners;
}
}

Expand Down Expand Up @@ -273,6 +274,10 @@
}
};

EventEmitter.prototype.getMaxListeners = function() {
return this._maxListeners;
};

EventEmitter.prototype.event = '';

EventEmitter.prototype.once = function(event, fn) {
Expand All @@ -290,11 +295,11 @@

EventEmitter.prototype.many = function(event, ttl, fn) {
return this._many(event, ttl, fn, false);
}
};

EventEmitter.prototype.prependMany = function(event, ttl, fn) {
return this._many(event, ttl, fn, true);
}
};

EventEmitter.prototype._many = function(event, ttl, fn, prepend) {
var self = this;
Expand Down Expand Up @@ -560,7 +565,7 @@
}

return this;
}
};

EventEmitter.prototype._on = function(type, listener, prepend) {
if (typeof type === 'function') {
Expand Down Expand Up @@ -612,7 +617,7 @@
}

return this;
}
};

EventEmitter.prototype.off = function(type, listener) {
if (typeof listener !== 'function') {
Expand Down Expand Up @@ -774,7 +779,7 @@

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

EventEmitter.prototype.listenerCount = function(type) {
return this.listeners(type).length;
Expand Down Expand Up @@ -849,6 +854,31 @@
});
};

var prototype= EventEmitter.prototype;

Object.defineProperties(EventEmitter, {
defaultMaxListeners: {
get: function(){
return prototype._maxListeners;
},
set: function (n) {
if (typeof n !== 'number' || n < 0 || Number.isNaN(n)) {
throw TypeError('n must be a non-negative number')
}
prototype._maxListeners = n;
},
enumerable: true
}
});

Object.defineProperties(prototype, {
_maxListeners: {
value: defaultMaxListeners,
writable: true,
configurable: true
}
});

if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(function() {
Expand Down
28 changes: 28 additions & 0 deletions test/simple/reconfigure.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,34 @@ module.exports = simpleEvents({
test.equal(emitter._conf.maxListeners, amount, 'should be ' + amount);

test.expect(2);
test.done();
},

'getMaxListeners': function (test) {
var emitter = new EventEmitter2(),
amount = 10; //default amount

test.equal(emitter.getMaxListeners(), amount, 'should be ' + amount);

amount= 99;

emitter.setMaxListeners(amount);

test.equal(emitter.getMaxListeners(), amount, 'should be ' + amount);

test.done();
},

'defaultMaxListeners': function (test) {
var defaultAmount = 10,
amount = defaultAmount;

test.equal(EventEmitter2.defaultMaxListeners, amount, 'should be ' + amount);
amount = 99;
EventEmitter2.defaultMaxListeners = amount;
test.equal(EventEmitter2.defaultMaxListeners, amount, 'should be ' + amount);
EventEmitter2.defaultMaxListeners = defaultAmount; // rollback

test.done();
}

Expand Down

0 comments on commit 9ca2fd0

Please sign in to comment.