2011-03-10 09:54:52 +01:00
|
|
|
// Copyright Joyent, Inc. and other Node contributors.
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
// copy of this software and associated documentation files (the
|
|
|
|
// "Software"), to deal in the Software without restriction, including
|
|
|
|
// without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
// distribute, sublicense, and/or sell copies of the Software, and to permit
|
|
|
|
// persons to whom the Software is furnished to do so, subject to the
|
|
|
|
// following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included
|
|
|
|
// in all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
|
|
|
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
|
|
|
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|
|
|
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
|
|
|
// USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
|
2010-07-15 23:32:45 +02:00
|
|
|
var isArray = Array.isArray;
|
2010-04-23 02:22:03 +02:00
|
|
|
|
2011-07-19 10:46:38 +02:00
|
|
|
function EventEmitter() { }
|
|
|
|
exports.EventEmitter = EventEmitter;
|
|
|
|
|
2011-01-01 03:32:52 +01:00
|
|
|
// By default EventEmitters will print a warning if more than
|
|
|
|
// 10 listeners are added to it. This is a useful default which
|
|
|
|
// helps finding memory leaks.
|
|
|
|
//
|
|
|
|
// Obviously not all Emitters should be limited to 10. This function allows
|
|
|
|
// that to be increased. Set to zero for unlimited.
|
|
|
|
var defaultMaxListeners = 10;
|
|
|
|
EventEmitter.prototype.setMaxListeners = function(n) {
|
2011-03-07 22:16:00 +01:00
|
|
|
if (!this._events) this._events = {};
|
2011-10-23 13:19:45 +02:00
|
|
|
this._maxListeners = n;
|
2011-01-01 03:32:52 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2011-07-09 02:08:52 +02:00
|
|
|
EventEmitter.prototype.emit = function() {
|
|
|
|
var type = arguments[0];
|
2010-04-23 02:31:35 +02:00
|
|
|
// If there is no 'error' event listener then throw.
|
|
|
|
if (type === 'error') {
|
2010-04-23 02:22:03 +02:00
|
|
|
if (!this._events || !this._events.error ||
|
2010-07-15 23:32:45 +02:00
|
|
|
(isArray(this._events.error) && !this._events.error.length))
|
2010-04-23 02:22:03 +02:00
|
|
|
{
|
|
|
|
if (arguments[1] instanceof Error) {
|
2010-10-02 08:22:34 +02:00
|
|
|
throw arguments[1]; // Unhandled 'error' event
|
2010-04-23 02:22:03 +02:00
|
|
|
} else {
|
2010-04-26 07:29:55 +02:00
|
|
|
throw new Error("Uncaught, unspecified 'error' event.");
|
2010-04-23 02:22:03 +02:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!this._events) return false;
|
2010-09-16 13:08:09 +02:00
|
|
|
var handler = this._events[type];
|
|
|
|
if (!handler) return false;
|
2010-04-23 02:22:03 +02:00
|
|
|
|
2010-09-16 13:08:09 +02:00
|
|
|
if (typeof handler == 'function') {
|
2010-11-28 07:03:14 +01:00
|
|
|
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;
|
2010-04-23 02:31:35 +02:00
|
|
|
// slower
|
2010-11-28 07:03:14 +01:00
|
|
|
default:
|
2011-07-09 02:08:52 +02:00
|
|
|
var l = arguments.length;
|
|
|
|
var args = new Array(l - 1);
|
|
|
|
for (var i = 1; i < l; i++) args[i - 1] = arguments[i];
|
2010-11-28 07:03:14 +01:00
|
|
|
handler.apply(this, args);
|
2010-04-23 02:31:35 +02:00
|
|
|
}
|
2010-04-23 02:22:03 +02:00
|
|
|
return true;
|
|
|
|
|
2010-09-16 13:08:09 +02:00
|
|
|
} else if (isArray(handler)) {
|
2011-07-09 02:08:52 +02:00
|
|
|
var l = arguments.length;
|
|
|
|
var args = new Array(l - 1);
|
|
|
|
for (var i = 1; i < l; i++) args[i - 1] = arguments[i];
|
2010-04-23 02:22:03 +02:00
|
|
|
|
2010-09-16 13:08:09 +02:00
|
|
|
var listeners = handler.slice();
|
2010-04-23 02:22:03 +02:00
|
|
|
for (var i = 0, l = listeners.length; i < l; i++) {
|
|
|
|
listeners[i].apply(this, args);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2010-09-16 00:47:28 +02:00
|
|
|
// EventEmitter is defined in src/node_events.cc
|
|
|
|
// EventEmitter.prototype.emit() is also defined there.
|
2010-12-02 02:43:30 +01:00
|
|
|
EventEmitter.prototype.addListener = function(type, listener) {
|
2010-04-21 03:13:07 +02:00
|
|
|
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 02:43:30 +01:00
|
|
|
this.emit('newListener', type, listener);
|
2010-04-21 03:13:07 +02:00
|
|
|
|
|
|
|
if (!this._events[type]) {
|
|
|
|
// Optimize the case of one listener. Don't need the extra array object.
|
|
|
|
this._events[type] = listener;
|
2010-07-15 23:32:45 +02:00
|
|
|
} else if (isArray(this._events[type])) {
|
2011-01-01 03:32:52 +01:00
|
|
|
|
2011-05-12 08:18:21 +02:00
|
|
|
// If we've already got an array, just append.
|
|
|
|
this._events[type].push(listener);
|
|
|
|
|
2011-01-01 03:32:52 +01:00
|
|
|
// Check for listener leak
|
|
|
|
if (!this._events[type].warned) {
|
|
|
|
var m;
|
2011-10-23 13:19:45 +02:00
|
|
|
if (this._maxListeners !== undefined) {
|
|
|
|
m = this._maxListeners;
|
2011-01-01 03:32:52 +01:00
|
|
|
} else {
|
|
|
|
m = defaultMaxListeners;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m && m > 0 && this._events[type].length > m) {
|
|
|
|
this._events[type].warned = true;
|
|
|
|
console.error('(node) warning: possible EventEmitter memory ' +
|
|
|
|
'leak detected. %d listeners added. ' +
|
|
|
|
'Use emitter.setMaxListeners() to increase limit.',
|
|
|
|
this._events[type].length);
|
|
|
|
console.trace();
|
|
|
|
}
|
|
|
|
}
|
2010-04-21 03:13:07 +02:00
|
|
|
} else {
|
|
|
|
// Adding the second element, need to change to array.
|
|
|
|
this._events[type] = [this._events[type], listener];
|
|
|
|
}
|
|
|
|
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
2010-09-16 00:47:28 +02:00
|
|
|
EventEmitter.prototype.on = EventEmitter.prototype.addListener;
|
2010-04-23 02:22:03 +02:00
|
|
|
|
2010-12-02 02:43:30 +01:00
|
|
|
EventEmitter.prototype.once = function(type, listener) {
|
2011-03-15 14:01:24 +01:00
|
|
|
if ('function' !== typeof listener) {
|
|
|
|
throw new Error('.once only takes instances of Function');
|
|
|
|
}
|
|
|
|
|
2010-10-12 22:52:26 +02:00
|
|
|
var self = this;
|
2011-03-18 21:02:14 +01:00
|
|
|
function g() {
|
2010-10-12 22:52:26 +02:00
|
|
|
self.removeListener(type, g);
|
|
|
|
listener.apply(this, arguments);
|
2011-03-18 21:02:14 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
g.listener = listener;
|
|
|
|
self.on(type, g);
|
2010-12-07 17:44:21 +01:00
|
|
|
|
|
|
|
return this;
|
2010-10-12 22:52:26 +02:00
|
|
|
};
|
|
|
|
|
2010-12-02 02:43:30 +01:00
|
|
|
EventEmitter.prototype.removeListener = function(type, listener) {
|
2010-04-21 03:13:07 +02:00
|
|
|
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-15 23:32:45 +02:00
|
|
|
if (isArray(list)) {
|
2011-03-18 21:02:14 +01:00
|
|
|
var position = -1;
|
|
|
|
for (var i = 0, length = list.length; i < length; i++) {
|
|
|
|
if (list[i] === listener ||
|
|
|
|
(list[i].listener && list[i].listener === listener))
|
|
|
|
{
|
|
|
|
position = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (position < 0) return this;
|
|
|
|
list.splice(position, 1);
|
2010-06-29 03:33:21 +02:00
|
|
|
if (list.length == 0)
|
|
|
|
delete this._events[type];
|
2011-03-18 21:02:14 +01:00
|
|
|
} else if (list === listener ||
|
|
|
|
(list.listener && list.listener === listener))
|
|
|
|
{
|
2010-06-29 03:33:21 +02:00
|
|
|
delete this._events[type];
|
2010-04-21 03:13:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
2010-12-02 02:43:30 +01:00
|
|
|
EventEmitter.prototype.removeAllListeners = function(type) {
|
2011-04-07 16:57:33 +02:00
|
|
|
if (arguments.length === 0) {
|
|
|
|
this._events = {};
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2010-04-21 03:13:07 +02:00
|
|
|
// does not use listeners(), so no side effect of creating _events[type]
|
2010-05-14 13:13:27 +02:00
|
|
|
if (type && this._events && this._events[type]) this._events[type] = null;
|
|
|
|
return this;
|
2010-04-21 03:13:07 +02:00
|
|
|
};
|
|
|
|
|
2010-12-02 02:43:30 +01:00
|
|
|
EventEmitter.prototype.listeners = function(type) {
|
2010-04-21 03:13:07 +02:00
|
|
|
if (!this._events) this._events = {};
|
|
|
|
if (!this._events[type]) this._events[type] = [];
|
2010-07-15 23:32:45 +02:00
|
|
|
if (!isArray(this._events[type])) {
|
2010-04-21 03:13:07 +02:00
|
|
|
this._events[type] = [this._events[type]];
|
|
|
|
}
|
|
|
|
return this._events[type];
|
2010-10-12 22:52:26 +02:00
|
|
|
};
|