fixing inheritance bug in event emitter

This commit is contained in:
Joe Walker 2011-04-13 13:46:50 +01:00
Родитель e56374bbd4
Коммит ade5a22f5b
1 изменённых файлов: 20 добавлений и 8 удалений

Просмотреть файл

@ -43,27 +43,34 @@ var EventEmitter = {};
EventEmitter._emit =
EventEmitter._dispatchEvent = function(eventName, e) {
this._eventRegistry = this._eventRegistry || {};
if (!this.hasOwnProperty('_eventRegistry')) {
return;
}
var listeners = this._eventRegistry[eventName];
if (!listeners || !listeners.length) return;
if (!listeners || !listeners.length) {
return;
}
var e = e || {};
e.type = eventName;
for (var i=0; i<listeners.length; i++) {
for (var i = 0; i < listeners.length; i++) {
listeners[i](e);
}
};
EventEmitter.on =
EventEmitter.addEventListener = function(eventName, callback) {
this._eventRegistry = this._eventRegistry || {};
if (!this.hasOwnProperty('_eventRegistry')) {
this._eventRegistry = {};
}
var listeners = this._eventRegistry[eventName];
if (!listeners) {
var listeners = this._eventRegistry[eventName] = [];
var listeners = this._eventRegistry[eventName] = [];
}
if (listeners.indexOf(callback) == -1) {
listeners.push(callback);
}
@ -71,12 +78,15 @@ EventEmitter.addEventListener = function(eventName, callback) {
EventEmitter.removeListener =
EventEmitter.removeEventListener = function(eventName, callback) {
this._eventRegistry = this._eventRegistry || {};
if (!this.hasOwnProperty('_eventRegistry')) {
return;
}
var listeners = this._eventRegistry[eventName];
if (!listeners) {
return;
return;
}
var index = listeners.indexOf(callback);
if (index !== -1) {
listeners.splice(index, 1);
@ -84,7 +94,9 @@ EventEmitter.removeEventListener = function(eventName, callback) {
};
EventEmitter.removeAllListeners = function(eventName) {
if (this._eventRegistry) this._eventRegistry[eventName] = [];
if (this.hasOwnProperty('_eventRegistry')) {
this._eventRegistry[eventName] = [];
}
};
exports.EventEmitter = EventEmitter;