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