lib: fix TypeError with EventEmitter#on() abuse
Commit 2931348 added EventEmitter#getMaxListeners() but introduced a regression when people abuse EventEmitter.prototype.on.call() to call EventEmitter#on() on a non-EE object. Add a workaround for that. Fixes: https://github.com/iojs/io.js/issues/523 PR-URL: https://github.com/iojs/io.js/pull/527 Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
This commit is contained in:
parent
77d68070da
commit
ee9cd004d8
@ -46,11 +46,14 @@ EventEmitter.prototype.setMaxListeners = function setMaxListeners(n) {
|
|||||||
return this;
|
return this;
|
||||||
};
|
};
|
||||||
|
|
||||||
EventEmitter.prototype.getMaxListeners = function getMaxListeners() {
|
function $getMaxListeners(that) {
|
||||||
if (!util.isUndefined(this._maxListeners))
|
if (util.isUndefined(that._maxListeners))
|
||||||
return this._maxListeners;
|
|
||||||
else
|
|
||||||
return EventEmitter.defaultMaxListeners;
|
return EventEmitter.defaultMaxListeners;
|
||||||
|
return that._maxListeners;
|
||||||
|
}
|
||||||
|
|
||||||
|
EventEmitter.prototype.getMaxListeners = function getMaxListeners() {
|
||||||
|
return $getMaxListeners(this);
|
||||||
};
|
};
|
||||||
|
|
||||||
EventEmitter.prototype.emit = function emit(type) {
|
EventEmitter.prototype.emit = function emit(type) {
|
||||||
@ -151,7 +154,7 @@ EventEmitter.prototype.addListener = function addListener(type, listener) {
|
|||||||
|
|
||||||
// Check for listener leak
|
// Check for listener leak
|
||||||
if (util.isObject(this._events[type]) && !this._events[type].warned) {
|
if (util.isObject(this._events[type]) && !this._events[type].warned) {
|
||||||
var m = this.getMaxListeners();
|
var m = $getMaxListeners(this);
|
||||||
if (m && m > 0 && this._events[type].length > m) {
|
if (m && m > 0 && this._events[type].length > m) {
|
||||||
this._events[type].warned = true;
|
this._events[type].warned = true;
|
||||||
console.error('(node) warning: possible EventEmitter memory ' +
|
console.error('(node) warning: possible EventEmitter memory ' +
|
||||||
|
@ -11,3 +11,8 @@ assert.strictEqual(emitter.getMaxListeners(), 0);
|
|||||||
|
|
||||||
emitter.setMaxListeners(3);
|
emitter.setMaxListeners(3);
|
||||||
assert.strictEqual(emitter.getMaxListeners(), 3);
|
assert.strictEqual(emitter.getMaxListeners(), 3);
|
||||||
|
|
||||||
|
// https://github.com/iojs/io.js/issues/523 - second call should not throw.
|
||||||
|
var recv = {};
|
||||||
|
EventEmitter.prototype.on.call(recv, 'event', function() {});
|
||||||
|
EventEmitter.prototype.on.call(recv, 'event', function() {});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user