node/lib/events.js

131 lines
3.6 KiB
JavaScript
Raw Normal View History

2010-09-16 06:47:28 +08:00
var EventEmitter = exports.EventEmitter = process.EventEmitter;
2010-03-16 04:47:50 +08:00
2010-07-16 05:32:45 +08:00
var isArray = Array.isArray;
2010-12-02 09:43:30 +08:00
EventEmitter.prototype.emit = function(type) {
// If there is no 'error' event listener then throw.
if (type === 'error') {
if (!this._events || !this._events.error ||
2010-07-16 05:32:45 +08:00
(isArray(this._events.error) && !this._events.error.length))
{
if (arguments[1] instanceof Error) {
throw arguments[1]; // Unhandled 'error' event
} else {
2010-04-26 13:29:55 +08:00
throw new Error("Uncaught, unspecified 'error' event.");
}
return false;
}
}
if (!this._events) return false;
2010-09-16 19:08:09 +08:00
var handler = this._events[type];
if (!handler) return false;
2010-09-16 19:08:09 +08:00
if (typeof handler == 'function') {
switch (arguments.length) {
// fast cases
case 1:
handler.call(this);
break;
case 2:
handler.call(this, arguments[1]);
break;
case 3:
handler.call(this, arguments[1], arguments[2]);
break;
// slower
default:
var args = Array.prototype.slice.call(arguments, 1);
handler.apply(this, args);
}
return true;
2010-09-16 19:08:09 +08:00
} else if (isArray(handler)) {
var args = Array.prototype.slice.call(arguments, 1);
2010-09-16 19:08:09 +08:00
var listeners = handler.slice();
for (var i = 0, l = listeners.length; i < l; i++) {
listeners[i].apply(this, args);
}
return true;
} else {
return false;
}
};
2010-09-16 06:47:28 +08:00
// EventEmitter is defined in src/node_events.cc
// EventEmitter.prototype.emit() is also defined there.
2010-12-02 09:43:30 +08:00
EventEmitter.prototype.addListener = function(type, listener) {
if ('function' !== typeof listener) {
throw new Error('addListener only takes instances of Function');
}
if (!this._events) this._events = {};
// To avoid recursion in the case that type == "newListeners"! Before
// adding it to the listeners, first emit "newListeners".
2010-12-02 09:43:30 +08:00
this.emit('newListener', type, listener);
if (!this._events[type]) {
// Optimize the case of one listener. Don't need the extra array object.
this._events[type] = listener;
2010-07-16 05:32:45 +08:00
} else if (isArray(this._events[type])) {
// If we've already got an array, just append.
this._events[type].push(listener);
} else {
// Adding the second element, need to change to array.
this._events[type] = [this._events[type], listener];
}
return this;
};
2010-09-16 06:47:28 +08:00
EventEmitter.prototype.on = EventEmitter.prototype.addListener;
2010-12-02 09:43:30 +08:00
EventEmitter.prototype.once = function(type, listener) {
2010-10-13 04:52:26 +08:00
var self = this;
2010-12-02 09:43:30 +08:00
self.on(type, function g() {
2010-10-13 04:52:26 +08:00
self.removeListener(type, g);
listener.apply(this, arguments);
});
};
2010-12-02 09:43:30 +08:00
EventEmitter.prototype.removeListener = function(type, listener) {
if ('function' !== typeof listener) {
throw new Error('removeListener only takes instances of Function');
}
// does not use listeners(), so no side effect of creating _events[type]
if (!this._events || !this._events[type]) return this;
var list = this._events[type];
2010-07-16 05:32:45 +08:00
if (isArray(list)) {
var i = list.indexOf(listener);
if (i < 0) return this;
list.splice(i, 1);
if (list.length == 0)
delete this._events[type];
} else if (this._events[type] === listener) {
delete this._events[type];
}
return this;
};
2010-12-02 09:43:30 +08:00
EventEmitter.prototype.removeAllListeners = function(type) {
// does not use listeners(), so no side effect of creating _events[type]
if (type && this._events && this._events[type]) this._events[type] = null;
return this;
};
2010-12-02 09:43:30 +08:00
EventEmitter.prototype.listeners = function(type) {
if (!this._events) this._events = {};
if (!this._events[type]) this._events[type] = [];
2010-07-16 05:32:45 +08:00
if (!isArray(this._events[type])) {
this._events[type] = [this._events[type]];
}
return this._events[type];
2010-10-13 04:52:26 +08:00
};