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;
|
2012-06-17 11:50:40 +02:00
|
|
|
var domain;
|
2010-04-23 02:22:03 +02:00
|
|
|
|
2013-02-20 23:52:46 +01:00
|
|
|
exports.usingDomains = false;
|
|
|
|
|
2012-04-07 01:26:18 +02:00
|
|
|
function EventEmitter() {
|
2012-10-25 16:48:36 +02:00
|
|
|
this.domain = null;
|
2012-04-07 01:26:18 +02:00
|
|
|
if (exports.usingDomains) {
|
|
|
|
// if there is an active domain, then attach to it.
|
2012-06-17 11:50:40 +02:00
|
|
|
domain = domain || require('domain');
|
2012-04-07 01:26:18 +02:00
|
|
|
if (domain.active && !(this instanceof domain.Domain)) {
|
|
|
|
this.domain = domain.active;
|
|
|
|
}
|
|
|
|
}
|
2012-10-25 22:37:08 +02:00
|
|
|
this._events = this._events || null;
|
|
|
|
this._maxListeners = this._maxListeners || defaultMaxListeners;
|
2012-04-07 01:26:18 +02:00
|
|
|
}
|
2011-07-19 10:46:38 +02:00
|
|
|
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) {
|
2013-02-21 00:04:58 +01:00
|
|
|
if (typeof n !== 'number' || n < 0)
|
|
|
|
throw TypeError('n must be a positive number');
|
2011-10-23 13:19:45 +02:00
|
|
|
this._maxListeners = n;
|
2011-01-01 03:32:52 +01:00
|
|
|
};
|
|
|
|
|
domain: Properly exit() on domain disposal
This addresses #4034. There are two problems happening:
1. The domain is not exited automatically when calling dispose() on it.
Then, since the domain is disposed, attempting to exit it again will do
nothing.
2. The active domain is stored on process.domain. Since thrown errors
call `process.emit('uncaughtException', er)`, and the process is an
event emitter with a `.domain` member, it re-enters the domain a second
time before calling the error handler, pushing it onto the stack again.
Thus, if the handler calls `domain.dispose()`, then the domain is now on
the stack twice, and cannot be exited properly. Since the domain is
disposed, any subsequent IO will be no-op'ed, since we've declared that
this context is done and best forgotten.
The solution here is twofold:
1. In EventEmitter.emit, do not enter the domain if `this===process`.
2. Automatically exit the domain when calling `domain.dispose()`.
2012-09-21 00:13:36 +02:00
|
|
|
// non-global reference, for speed.
|
|
|
|
var PROCESS;
|
2011-01-01 03:32:52 +01:00
|
|
|
|
2012-12-10 10:07:23 +01:00
|
|
|
EventEmitter.prototype.emit = function(type) {
|
2010-04-23 02:31:35 +02:00
|
|
|
// If there is no 'error' event listener then throw.
|
|
|
|
if (type === 'error') {
|
2013-01-17 22:20:22 +01:00
|
|
|
if (!this._events || !this._events.error ||
|
2013-02-21 00:04:58 +01:00
|
|
|
(isArray(this._events.error) && !this._events.error.length)) {
|
2012-04-07 01:26:18 +02:00
|
|
|
if (this.domain) {
|
|
|
|
var er = arguments[1];
|
2012-12-26 21:31:27 +01:00
|
|
|
er.domainEmitter = this;
|
2012-04-07 01:26:18 +02:00
|
|
|
er.domain = this.domain;
|
2012-12-26 21:31:27 +01:00
|
|
|
er.domainThrown = false;
|
2012-04-07 01:26:18 +02:00
|
|
|
this.domain.emit('error', er);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
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 {
|
2013-02-21 00:04:58 +01:00
|
|
|
throw TypeError("Uncaught, unspecified 'error' event.");
|
2010-04-23 02:22:03 +02:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!this._events) return false;
|
2013-01-17 22:20:22 +01:00
|
|
|
var handler = this._events[type];
|
2010-09-16 13:08:09 +02:00
|
|
|
if (!handler) return false;
|
2010-04-23 02:22:03 +02:00
|
|
|
|
2010-09-16 13:08:09 +02:00
|
|
|
if (typeof handler == 'function') {
|
2012-04-07 01:26:18 +02:00
|
|
|
if (this.domain) {
|
domain: Properly exit() on domain disposal
This addresses #4034. There are two problems happening:
1. The domain is not exited automatically when calling dispose() on it.
Then, since the domain is disposed, attempting to exit it again will do
nothing.
2. The active domain is stored on process.domain. Since thrown errors
call `process.emit('uncaughtException', er)`, and the process is an
event emitter with a `.domain` member, it re-enters the domain a second
time before calling the error handler, pushing it onto the stack again.
Thus, if the handler calls `domain.dispose()`, then the domain is now on
the stack twice, and cannot be exited properly. Since the domain is
disposed, any subsequent IO will be no-op'ed, since we've declared that
this context is done and best forgotten.
The solution here is twofold:
1. In EventEmitter.emit, do not enter the domain if `this===process`.
2. Automatically exit the domain when calling `domain.dispose()`.
2012-09-21 00:13:36 +02:00
|
|
|
PROCESS = PROCESS || process;
|
|
|
|
if (this !== PROCESS) {
|
|
|
|
this.domain.enter();
|
|
|
|
}
|
2012-04-07 01:26:18 +02:00
|
|
|
}
|
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
|
|
|
}
|
domain: Properly exit() on domain disposal
This addresses #4034. There are two problems happening:
1. The domain is not exited automatically when calling dispose() on it.
Then, since the domain is disposed, attempting to exit it again will do
nothing.
2. The active domain is stored on process.domain. Since thrown errors
call `process.emit('uncaughtException', er)`, and the process is an
event emitter with a `.domain` member, it re-enters the domain a second
time before calling the error handler, pushing it onto the stack again.
Thus, if the handler calls `domain.dispose()`, then the domain is now on
the stack twice, and cannot be exited properly. Since the domain is
disposed, any subsequent IO will be no-op'ed, since we've declared that
this context is done and best forgotten.
The solution here is twofold:
1. In EventEmitter.emit, do not enter the domain if `this===process`.
2. Automatically exit the domain when calling `domain.dispose()`.
2012-09-21 00:13:36 +02:00
|
|
|
if (this.domain && this !== PROCESS) {
|
2012-04-07 01:26:18 +02:00
|
|
|
this.domain.exit();
|
|
|
|
}
|
2010-04-23 02:22:03 +02:00
|
|
|
return true;
|
|
|
|
|
2010-09-16 13:08:09 +02:00
|
|
|
} else if (isArray(handler)) {
|
2012-04-07 01:26:18 +02:00
|
|
|
if (this.domain) {
|
domain: Properly exit() on domain disposal
This addresses #4034. There are two problems happening:
1. The domain is not exited automatically when calling dispose() on it.
Then, since the domain is disposed, attempting to exit it again will do
nothing.
2. The active domain is stored on process.domain. Since thrown errors
call `process.emit('uncaughtException', er)`, and the process is an
event emitter with a `.domain` member, it re-enters the domain a second
time before calling the error handler, pushing it onto the stack again.
Thus, if the handler calls `domain.dispose()`, then the domain is now on
the stack twice, and cannot be exited properly. Since the domain is
disposed, any subsequent IO will be no-op'ed, since we've declared that
this context is done and best forgotten.
The solution here is twofold:
1. In EventEmitter.emit, do not enter the domain if `this===process`.
2. Automatically exit the domain when calling `domain.dispose()`.
2012-09-21 00:13:36 +02:00
|
|
|
PROCESS = PROCESS || process;
|
|
|
|
if (this !== PROCESS) {
|
|
|
|
this.domain.enter();
|
|
|
|
}
|
2012-04-07 01:26:18 +02:00
|
|
|
}
|
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);
|
|
|
|
}
|
domain: Properly exit() on domain disposal
This addresses #4034. There are two problems happening:
1. The domain is not exited automatically when calling dispose() on it.
Then, since the domain is disposed, attempting to exit it again will do
nothing.
2. The active domain is stored on process.domain. Since thrown errors
call `process.emit('uncaughtException', er)`, and the process is an
event emitter with a `.domain` member, it re-enters the domain a second
time before calling the error handler, pushing it onto the stack again.
Thus, if the handler calls `domain.dispose()`, then the domain is now on
the stack twice, and cannot be exited properly. Since the domain is
disposed, any subsequent IO will be no-op'ed, since we've declared that
this context is done and best forgotten.
The solution here is twofold:
1. In EventEmitter.emit, do not enter the domain if `this===process`.
2. Automatically exit the domain when calling `domain.dispose()`.
2012-09-21 00:13:36 +02:00
|
|
|
if (this.domain && this !== PROCESS) {
|
2012-04-07 01:26:18 +02:00
|
|
|
this.domain.exit();
|
|
|
|
}
|
2010-04-23 02:22:03 +02:00
|
|
|
return true;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
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) {
|
2013-02-21 00:04:58 +01:00
|
|
|
throw TypeError('addListener only takes instances of Function');
|
2010-04-21 03:13:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!this._events) this._events = {};
|
|
|
|
|
2012-12-10 10:32:56 +01:00
|
|
|
// To avoid recursion in the case that type == "newListener"! Before
|
|
|
|
// adding it to the listeners, first emit "newListener".
|
2013-01-17 22:20:22 +01:00
|
|
|
if (this._events.newListener) {
|
2012-08-01 01:21:46 +02:00
|
|
|
this.emit('newListener', type, typeof listener.listener === 'function' ?
|
|
|
|
listener.listener : listener);
|
|
|
|
}
|
2010-04-21 03:13:07 +02:00
|
|
|
|
2013-01-17 22:20:22 +01:00
|
|
|
if (!this._events[type]) {
|
2010-04-21 03:13:07 +02:00
|
|
|
// Optimize the case of one listener. Don't need the extra array object.
|
2013-01-17 22:20:22 +01:00
|
|
|
this._events[type] = listener;
|
|
|
|
} 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.
|
2013-01-17 22:20:22 +01:00
|
|
|
this._events[type].push(listener);
|
2011-05-12 08:18:21 +02:00
|
|
|
|
2010-04-21 03:13:07 +02:00
|
|
|
} else {
|
|
|
|
// Adding the second element, need to change to array.
|
2013-01-17 22:20:22 +01:00
|
|
|
this._events[type] = [this._events[type], listener];
|
2012-01-08 16:53:17 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for listener leak
|
2013-01-17 22:20:22 +01:00
|
|
|
if (isArray(this._events[type]) && !this._events[type].warned) {
|
2012-01-08 16:53:17 +01:00
|
|
|
var m;
|
2012-10-25 16:48:36 +02:00
|
|
|
m = this._maxListeners;
|
2012-01-08 16:53:17 +01:00
|
|
|
|
2013-01-17 22:20:22 +01:00
|
|
|
if (m && m > 0 && this._events[type].length > m) {
|
|
|
|
this._events[type].warned = true;
|
2012-01-08 16:53:17 +01:00
|
|
|
console.error('(node) warning: possible EventEmitter memory ' +
|
|
|
|
'leak detected. %d listeners added. ' +
|
|
|
|
'Use emitter.setMaxListeners() to increase limit.',
|
2013-01-17 22:20:22 +01:00
|
|
|
this._events[type].length);
|
2012-01-08 16:53:17 +01:00
|
|
|
console.trace();
|
|
|
|
}
|
2010-04-21 03:13:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2013-02-21 00:04:58 +01:00
|
|
|
throw TypeError('.once only takes instances of Function');
|
2011-03-15 14:01:24 +01:00
|
|
|
}
|
|
|
|
|
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
|
|
|
};
|
|
|
|
|
2012-08-01 00:57:15 +02:00
|
|
|
// emits a 'removeListener' event iff the listener was removed
|
2010-12-02 02:43:30 +01:00
|
|
|
EventEmitter.prototype.removeListener = function(type, listener) {
|
2013-02-21 08:54:38 +01:00
|
|
|
var list, position, length, i;
|
|
|
|
|
|
|
|
if (typeof type !== 'string')
|
|
|
|
throw TypeError('type must be a string');
|
|
|
|
if (typeof listener !== 'function')
|
|
|
|
throw TypeError('listener must be a function');
|
2010-04-21 03:13:07 +02:00
|
|
|
|
2013-02-21 08:54:38 +01:00
|
|
|
if (!this._events || !this._events[type])
|
|
|
|
return this;
|
|
|
|
|
|
|
|
list = this._events[type];
|
|
|
|
length = list.length;
|
|
|
|
position = -1;
|
2010-04-21 03:13:07 +02:00
|
|
|
|
2013-02-21 08:54:38 +01:00
|
|
|
if (list === listener ||
|
|
|
|
(typeof list.listener === 'function' && list.listener === listener)) {
|
|
|
|
this._events[type] = null;
|
|
|
|
if (this._events.removeListener)
|
|
|
|
this.emit('removeListener', type, listener);
|
2010-04-21 03:13:07 +02:00
|
|
|
|
2013-02-21 08:54:38 +01:00
|
|
|
} else if (isArray(list)) {
|
|
|
|
for (i = 0; i < length; i++) {
|
2011-03-18 21:02:14 +01:00
|
|
|
if (list[i] === listener ||
|
2013-02-21 08:54:38 +01:00
|
|
|
(list[i].listener && list[i].listener === listener)) {
|
2011-03-18 21:02:14 +01:00
|
|
|
position = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-21 08:54:38 +01:00
|
|
|
if (position < 0)
|
|
|
|
return this;
|
2012-08-01 01:21:46 +02:00
|
|
|
|
2013-02-21 08:54:38 +01:00
|
|
|
if (list.length === 1) {
|
|
|
|
list.length = 0;
|
|
|
|
this._events[type] = null;
|
|
|
|
} else {
|
|
|
|
list.splice(position, 1);
|
2012-08-01 01:21:46 +02:00
|
|
|
}
|
|
|
|
|
2013-02-21 08:54:38 +01:00
|
|
|
if (this._events.removeListener)
|
2012-08-01 01:21:46 +02:00
|
|
|
this.emit('removeListener', type, listener);
|
2010-04-21 03:13:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
2010-12-02 02:43:30 +01:00
|
|
|
EventEmitter.prototype.removeAllListeners = function(type) {
|
2013-02-21 00:19:05 +01:00
|
|
|
var key, listeners;
|
2012-08-01 00:57:15 +02:00
|
|
|
|
2013-02-21 00:19:05 +01:00
|
|
|
if (arguments.length > 0 && typeof type !== 'string')
|
|
|
|
throw TypeError('type must not be set or must be a string');
|
|
|
|
|
|
|
|
if (!this._events)
|
|
|
|
return this;
|
|
|
|
|
|
|
|
// not listening for removeListener, no need to emit
|
2013-01-17 22:20:22 +01:00
|
|
|
if (!this._events.removeListener) {
|
2013-02-21 00:19:05 +01:00
|
|
|
if (arguments.length === 0)
|
|
|
|
this._events = null;
|
|
|
|
else if (this._events[type])
|
2013-01-17 22:20:22 +01:00
|
|
|
this._events[type] = null;
|
2012-08-01 01:29:10 +02:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2013-02-21 00:19:05 +01:00
|
|
|
// emit removeListener for all listeners on all events
|
2011-04-07 16:57:33 +02:00
|
|
|
if (arguments.length === 0) {
|
2013-02-21 00:19:05 +01:00
|
|
|
for (key in this._events) {
|
2013-01-17 22:20:22 +01:00
|
|
|
if (key === 'removeListener') continue;
|
|
|
|
this.removeAllListeners(key);
|
2012-08-01 00:57:15 +02:00
|
|
|
}
|
|
|
|
this.removeAllListeners('removeListener');
|
2013-02-21 00:19:05 +01:00
|
|
|
this._events = null;
|
2011-04-07 16:57:33 +02:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2013-02-21 00:19:05 +01:00
|
|
|
listeners = this._events[type];
|
|
|
|
|
|
|
|
if (typeof listeners === 'function') {
|
2012-08-01 00:57:15 +02:00
|
|
|
this.removeListener(type, listeners);
|
2013-02-21 00:19:05 +01:00
|
|
|
} else {
|
|
|
|
// LIFO order
|
|
|
|
while (listeners.length)
|
|
|
|
this.removeListener(type, listeners[listeners.length - 1]);
|
2012-08-01 00:57:15 +02:00
|
|
|
}
|
2013-01-17 22:20:22 +01:00
|
|
|
this._events[type] = null;
|
2012-08-01 00:57:15 +02:00
|
|
|
|
2010-05-14 13:13:27 +02:00
|
|
|
return this;
|
2010-04-21 03:13:07 +02:00
|
|
|
};
|
|
|
|
|
2010-12-02 02:43:30 +01:00
|
|
|
EventEmitter.prototype.listeners = function(type) {
|
2013-02-21 00:33:24 +01:00
|
|
|
if (typeof type !== 'string')
|
|
|
|
throw TypeError('event type must be a string');
|
|
|
|
|
|
|
|
if (!this._events || !this._events[type])
|
|
|
|
return [];
|
|
|
|
if (typeof this._events[type] === 'function')
|
2013-01-17 22:20:22 +01:00
|
|
|
return [this._events[type]];
|
2013-02-21 00:33:24 +01:00
|
|
|
return this._events[type].slice();
|
2010-10-12 22:52:26 +02:00
|
|
|
};
|
2013-02-14 09:48:11 +01:00
|
|
|
|
|
|
|
EventEmitter.listenerCount = function(emitter, type) {
|
|
|
|
var ret;
|
|
|
|
if (!emitter._events || !emitter._events[type])
|
|
|
|
ret = 0;
|
|
|
|
else if (typeof emitter._events[type] === 'function')
|
|
|
|
ret = 1;
|
|
|
|
else
|
|
|
|
ret = emitter._events[type].length;
|
|
|
|
return ret;
|
|
|
|
};
|