events: type check setMaxListeners, cleanup throws

setMaxListeners will now make sure a positive number is passed. Also
throwing more definitive Error types.
This commit is contained in:
Trevor Norris 2013-02-20 15:04:58 -08:00 committed by isaacs
parent 8ca43a7f40
commit dd171d24df

View File

@ -46,6 +46,8 @@ exports.EventEmitter = EventEmitter;
// that to be increased. Set to zero for unlimited.
var defaultMaxListeners = 10;
EventEmitter.prototype.setMaxListeners = function(n) {
if (typeof n !== 'number' || n < 0)
throw TypeError('n must be a positive number');
this._maxListeners = n;
};
@ -56,8 +58,7 @@ EventEmitter.prototype.emit = function(type) {
// If there is no 'error' event listener then throw.
if (type === 'error') {
if (!this._events || !this._events.error ||
(isArray(this._events.error) && !this._events.error.length))
{
(isArray(this._events.error) && !this._events.error.length)) {
if (this.domain) {
var er = arguments[1];
er.domainEmitter = this;
@ -70,7 +71,7 @@ EventEmitter.prototype.emit = function(type) {
if (arguments[1] instanceof Error) {
throw arguments[1]; // Unhandled 'error' event
} else {
throw new Error("Uncaught, unspecified 'error' event.");
throw TypeError("Uncaught, unspecified 'error' event.");
}
return false;
}
@ -137,7 +138,7 @@ EventEmitter.prototype.emit = function(type) {
EventEmitter.prototype.addListener = function(type, listener) {
if ('function' !== typeof listener) {
throw new Error('addListener only takes instances of Function');
throw TypeError('addListener only takes instances of Function');
}
if (!this._events) this._events = {};
@ -185,7 +186,7 @@ EventEmitter.prototype.on = EventEmitter.prototype.addListener;
EventEmitter.prototype.once = function(type, listener) {
if ('function' !== typeof listener) {
throw new Error('.once only takes instances of Function');
throw TypeError('.once only takes instances of Function');
}
var self = this;
@ -203,7 +204,7 @@ EventEmitter.prototype.once = function(type, listener) {
// emits a 'removeListener' event iff the listener was removed
EventEmitter.prototype.removeListener = function(type, listener) {
if ('function' !== typeof listener) {
throw new Error('removeListener only takes instances of Function');
throw TypeError('removeListener only takes instances of Function');
}
// does not use listeners(), so no side effect of creating _events[type]