2014-11-22 16:59:48 +01:00
|
|
|
'use strict';
|
|
|
|
|
2012-06-17 11:50:40 +02:00
|
|
|
var domain;
|
2010-04-23 02:22:03 +02:00
|
|
|
|
2016-04-07 02:33:28 +02:00
|
|
|
// This constructor is used to store event handlers. Instantiating this is
|
|
|
|
// faster than explicitly calling `Object.create(null)` to get a "clean" empty
|
|
|
|
// object (tested with v8 v4.9).
|
|
|
|
function EventHandlers() {}
|
|
|
|
EventHandlers.prototype = Object.create(null);
|
|
|
|
|
2012-04-07 01:26:18 +02:00
|
|
|
function EventEmitter() {
|
2013-12-12 23:59:40 +01:00
|
|
|
EventEmitter.init.call(this);
|
2012-04-07 01:26:18 +02:00
|
|
|
}
|
2013-05-13 21:04:06 +02:00
|
|
|
module.exports = EventEmitter;
|
|
|
|
|
|
|
|
// Backwards-compat with node 0.10.x
|
|
|
|
EventEmitter.EventEmitter = EventEmitter;
|
|
|
|
|
|
|
|
EventEmitter.usingDomains = false;
|
2011-07-19 10:46:38 +02:00
|
|
|
|
2013-05-25 22:03:02 +02:00
|
|
|
EventEmitter.prototype.domain = undefined;
|
|
|
|
EventEmitter.prototype._events = undefined;
|
|
|
|
EventEmitter.prototype._maxListeners = undefined;
|
2012-05-04 17:45:27 +02: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.
|
2015-12-30 09:20:33 +01:00
|
|
|
var defaultMaxListeners = 10;
|
|
|
|
|
|
|
|
Object.defineProperty(EventEmitter, 'defaultMaxListeners', {
|
|
|
|
enumerable: true,
|
|
|
|
get: function() {
|
|
|
|
return defaultMaxListeners;
|
|
|
|
},
|
|
|
|
set: function(arg) {
|
|
|
|
// force global console to be compiled.
|
|
|
|
// see https://github.com/nodejs/node/issues/4467
|
|
|
|
console;
|
|
|
|
defaultMaxListeners = arg;
|
|
|
|
}
|
|
|
|
});
|
2012-05-04 17:45:27 +02:00
|
|
|
|
2013-12-12 23:59:40 +01:00
|
|
|
EventEmitter.init = function() {
|
|
|
|
this.domain = null;
|
|
|
|
if (EventEmitter.usingDomains) {
|
|
|
|
// if there is an active domain, then attach to it.
|
|
|
|
domain = domain || require('domain');
|
|
|
|
if (domain.active && !(this instanceof domain.Domain)) {
|
|
|
|
this.domain = domain.active;
|
|
|
|
}
|
|
|
|
}
|
2014-04-04 00:11:56 +02:00
|
|
|
|
2015-02-11 23:00:12 +01:00
|
|
|
if (!this._events || this._events === Object.getPrototypeOf(this)._events) {
|
2016-04-07 02:33:28 +02:00
|
|
|
this._events = new EventHandlers();
|
2015-02-11 23:00:12 +01:00
|
|
|
this._eventsCount = 0;
|
|
|
|
}
|
2014-04-04 00:11:56 +02:00
|
|
|
|
2013-12-12 23:59:40 +01:00
|
|
|
this._maxListeners = this._maxListeners || undefined;
|
|
|
|
};
|
2012-05-04 17:45:27 +02:00
|
|
|
|
2011-01-01 03:32:52 +01:00
|
|
|
// Obviously not all Emitters should be limited to 10. This function allows
|
|
|
|
// that to be increased. Set to zero for unlimited.
|
2014-05-05 16:48:51 +02:00
|
|
|
EventEmitter.prototype.setMaxListeners = function setMaxListeners(n) {
|
2015-01-29 02:05:53 +01:00
|
|
|
if (typeof n !== 'number' || n < 0 || isNaN(n))
|
2015-10-14 23:10:25 +02:00
|
|
|
throw new TypeError('"n" argument must be a positive number');
|
2011-10-23 13:19:45 +02:00
|
|
|
this._maxListeners = n;
|
2013-05-01 00:26:14 +02:00
|
|
|
return this;
|
2011-01-01 03:32:52 +01:00
|
|
|
};
|
|
|
|
|
2015-01-20 19:30:50 +01:00
|
|
|
function $getMaxListeners(that) {
|
2015-01-29 02:05:53 +01:00
|
|
|
if (that._maxListeners === undefined)
|
2014-12-05 14:50:05 +01:00
|
|
|
return EventEmitter.defaultMaxListeners;
|
2015-01-20 19:30:50 +01:00
|
|
|
return that._maxListeners;
|
|
|
|
}
|
|
|
|
|
|
|
|
EventEmitter.prototype.getMaxListeners = function getMaxListeners() {
|
|
|
|
return $getMaxListeners(this);
|
2014-12-05 14:50:05 +01:00
|
|
|
};
|
|
|
|
|
2015-02-05 21:35:33 +01:00
|
|
|
// These standalone emit* functions are used to optimize calling of event
|
|
|
|
// handlers for fast cases because emit() itself often has a variable number of
|
|
|
|
// arguments and can be deoptimized because of that. These functions always have
|
|
|
|
// the same number of arguments and thus do not get deoptimized, so the code
|
|
|
|
// inside them can execute faster.
|
|
|
|
function emitNone(handler, isFn, self) {
|
|
|
|
if (isFn)
|
|
|
|
handler.call(self);
|
|
|
|
else {
|
|
|
|
var len = handler.length;
|
|
|
|
var listeners = arrayClone(handler, len);
|
|
|
|
for (var i = 0; i < len; ++i)
|
|
|
|
listeners[i].call(self);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
function emitOne(handler, isFn, self, arg1) {
|
|
|
|
if (isFn)
|
|
|
|
handler.call(self, arg1);
|
|
|
|
else {
|
|
|
|
var len = handler.length;
|
|
|
|
var listeners = arrayClone(handler, len);
|
|
|
|
for (var i = 0; i < len; ++i)
|
|
|
|
listeners[i].call(self, arg1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
function emitTwo(handler, isFn, self, arg1, arg2) {
|
|
|
|
if (isFn)
|
|
|
|
handler.call(self, arg1, arg2);
|
|
|
|
else {
|
|
|
|
var len = handler.length;
|
|
|
|
var listeners = arrayClone(handler, len);
|
|
|
|
for (var i = 0; i < len; ++i)
|
|
|
|
listeners[i].call(self, arg1, arg2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
function emitThree(handler, isFn, self, arg1, arg2, arg3) {
|
|
|
|
if (isFn)
|
|
|
|
handler.call(self, arg1, arg2, arg3);
|
|
|
|
else {
|
|
|
|
var len = handler.length;
|
|
|
|
var listeners = arrayClone(handler, len);
|
|
|
|
for (var i = 0; i < len; ++i)
|
|
|
|
listeners[i].call(self, arg1, arg2, arg3);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-10 19:12:09 +01:00
|
|
|
function emitMany(handler, isFn, self, args) {
|
|
|
|
if (isFn)
|
|
|
|
handler.apply(self, args);
|
|
|
|
else {
|
|
|
|
var len = handler.length;
|
|
|
|
var listeners = arrayClone(handler, len);
|
|
|
|
for (var i = 0; i < len; ++i)
|
|
|
|
listeners[i].apply(self, args);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-05 16:48:51 +02:00
|
|
|
EventEmitter.prototype.emit = function emit(type) {
|
2015-02-10 19:12:09 +01:00
|
|
|
var er, handler, len, args, i, events, domain;
|
2015-02-05 21:35:33 +01:00
|
|
|
var needDomainExit = false;
|
2015-02-11 23:00:12 +01:00
|
|
|
var doError = (type === 'error');
|
2013-02-26 20:20:19 +01:00
|
|
|
|
2015-02-05 21:35:33 +01:00
|
|
|
events = this._events;
|
2015-02-11 23:00:12 +01:00
|
|
|
if (events)
|
|
|
|
doError = (doError && events.error == null);
|
|
|
|
else if (!doError)
|
|
|
|
return false;
|
2015-02-05 21:35:33 +01:00
|
|
|
|
|
|
|
domain = this.domain;
|
2013-03-03 00:11:23 +01:00
|
|
|
|
2010-04-23 02:31:35 +02:00
|
|
|
// If there is no 'error' event listener then throw.
|
2015-02-11 23:00:12 +01:00
|
|
|
if (doError) {
|
2013-10-30 00:35:32 +01:00
|
|
|
er = arguments[1];
|
2015-02-05 21:35:33 +01:00
|
|
|
if (domain) {
|
2013-10-30 00:35:32 +01:00
|
|
|
if (!er)
|
2015-10-14 23:10:25 +02:00
|
|
|
er = new Error('Uncaught, unspecified "error" event');
|
2013-10-30 00:35:32 +01:00
|
|
|
er.domainEmitter = this;
|
2015-02-05 21:35:33 +01:00
|
|
|
er.domain = domain;
|
2013-10-30 00:35:32 +01:00
|
|
|
er.domainThrown = false;
|
2015-02-05 21:35:33 +01:00
|
|
|
domain.emit('error', er);
|
2013-10-30 00:35:32 +01:00
|
|
|
} else if (er instanceof Error) {
|
|
|
|
throw er; // Unhandled 'error' event
|
|
|
|
} else {
|
2015-05-07 23:09:31 +02:00
|
|
|
// At least give some kind of context to the user
|
|
|
|
var err = new Error('Uncaught, unspecified "error" event. (' + er + ')');
|
|
|
|
err.context = er;
|
|
|
|
throw err;
|
2010-04-23 02:22:03 +02:00
|
|
|
}
|
2013-10-30 00:35:32 +01:00
|
|
|
return false;
|
2010-04-23 02:22:03 +02:00
|
|
|
}
|
|
|
|
|
2015-02-05 21:35:33 +01:00
|
|
|
handler = events[type];
|
2013-02-26 20:20:19 +01:00
|
|
|
|
2015-02-05 21:35:33 +01:00
|
|
|
if (!handler)
|
2013-02-26 20:20:19 +01:00
|
|
|
return false;
|
|
|
|
|
2015-02-05 21:35:33 +01:00
|
|
|
if (domain && this !== process) {
|
|
|
|
domain.enter();
|
|
|
|
needDomainExit = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
var isFn = typeof handler === 'function';
|
|
|
|
len = arguments.length;
|
|
|
|
switch (len) {
|
|
|
|
// fast cases
|
|
|
|
case 1:
|
|
|
|
emitNone(handler, isFn, this);
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
emitOne(handler, isFn, this, arguments[1]);
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
emitTwo(handler, isFn, this, arguments[1], arguments[2]);
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
emitThree(handler, isFn, this, arguments[1], arguments[2], arguments[3]);
|
|
|
|
break;
|
|
|
|
// slower
|
|
|
|
default:
|
|
|
|
args = new Array(len - 1);
|
|
|
|
for (i = 1; i < len; i++)
|
|
|
|
args[i - 1] = arguments[i];
|
2015-02-10 19:12:09 +01:00
|
|
|
emitMany(handler, isFn, this, args);
|
2010-04-23 02:22:03 +02:00
|
|
|
}
|
2013-02-26 20:20:19 +01:00
|
|
|
|
2015-02-05 21:35:33 +01:00
|
|
|
if (needDomainExit)
|
|
|
|
domain.exit();
|
2014-01-09 20:11:40 +01:00
|
|
|
|
2013-02-26 20:20:19 +01:00
|
|
|
return true;
|
2010-04-23 02:22:03 +02:00
|
|
|
};
|
|
|
|
|
2016-04-04 04:55:22 +02:00
|
|
|
function _addListener(target, type, listener, prepend) {
|
2013-02-26 19:20:44 +01:00
|
|
|
var m;
|
2015-02-05 21:35:33 +01:00
|
|
|
var events;
|
|
|
|
var existing;
|
2013-02-26 19:20:44 +01:00
|
|
|
|
2015-01-29 02:05:53 +01:00
|
|
|
if (typeof listener !== 'function')
|
2015-10-14 23:10:25 +02:00
|
|
|
throw new TypeError('"listener" argument must be a function');
|
2015-02-05 21:35:33 +01:00
|
|
|
|
2016-04-04 04:55:22 +02:00
|
|
|
events = target._events;
|
2015-02-11 23:00:12 +01:00
|
|
|
if (!events) {
|
2016-04-04 04:55:22 +02:00
|
|
|
events = target._events = new EventHandlers();
|
|
|
|
target._eventsCount = 0;
|
2015-02-11 23:00:12 +01:00
|
|
|
} else {
|
2015-02-05 21:35:33 +01:00
|
|
|
// To avoid recursion in the case that type === "newListener"! Before
|
|
|
|
// adding it to the listeners, first emit "newListener".
|
|
|
|
if (events.newListener) {
|
2016-04-04 04:55:22 +02:00
|
|
|
target.emit('newListener', type,
|
|
|
|
listener.listener ? listener.listener : listener);
|
2015-02-11 23:00:12 +01:00
|
|
|
|
|
|
|
// Re-assign `events` because a newListener handler could have caused the
|
|
|
|
// this._events to be assigned to a new object
|
2016-04-04 04:55:22 +02:00
|
|
|
events = target._events;
|
2015-02-05 21:35:33 +01:00
|
|
|
}
|
|
|
|
existing = events[type];
|
|
|
|
}
|
2010-04-21 03:13:07 +02:00
|
|
|
|
2015-02-11 23:00:12 +01:00
|
|
|
if (!existing) {
|
2010-04-21 03:13:07 +02:00
|
|
|
// Optimize the case of one listener. Don't need the extra array object.
|
2015-02-05 21:35:33 +01:00
|
|
|
existing = events[type] = listener;
|
2016-04-04 04:55:22 +02:00
|
|
|
++target._eventsCount;
|
2015-02-11 23:00:12 +01:00
|
|
|
} else {
|
|
|
|
if (typeof existing === 'function') {
|
|
|
|
// Adding the second element, need to change to array.
|
2016-04-04 04:55:22 +02:00
|
|
|
existing = events[type] = prepend ? [listener, existing] :
|
|
|
|
[existing, listener];
|
2015-02-11 23:00:12 +01:00
|
|
|
} else {
|
|
|
|
// If we've already got an array, just append.
|
2016-04-04 04:55:22 +02:00
|
|
|
if (prepend) {
|
|
|
|
existing.unshift(listener);
|
|
|
|
} else {
|
|
|
|
existing.push(listener);
|
|
|
|
}
|
2015-02-11 23:00:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check for listener leak
|
|
|
|
if (!existing.warned) {
|
2016-04-04 04:55:22 +02:00
|
|
|
m = $getMaxListeners(target);
|
2015-02-11 23:00:12 +01:00
|
|
|
if (m && m > 0 && existing.length > m) {
|
|
|
|
existing.warned = true;
|
2016-01-20 20:38:35 +01:00
|
|
|
process.emitWarning('Possible EventEmitter memory leak detected. ' +
|
|
|
|
`${existing.length} ${type} listeners added. ` +
|
|
|
|
'Use emitter.setMaxListeners() to increase limit');
|
2015-02-11 23:00:12 +01:00
|
|
|
}
|
2012-01-08 16:53:17 +01:00
|
|
|
}
|
2010-04-21 03:13:07 +02:00
|
|
|
}
|
|
|
|
|
2016-04-04 04:55:22 +02:00
|
|
|
return target;
|
|
|
|
}
|
|
|
|
|
|
|
|
EventEmitter.prototype.addListener = function addListener(type, listener) {
|
|
|
|
return _addListener(this, type, listener, false);
|
2010-04-21 03:13:07 +02:00
|
|
|
};
|
|
|
|
|
2010-09-16 00:47:28 +02:00
|
|
|
EventEmitter.prototype.on = EventEmitter.prototype.addListener;
|
2010-04-23 02:22:03 +02:00
|
|
|
|
2016-04-04 04:55:22 +02:00
|
|
|
EventEmitter.prototype.prependListener =
|
|
|
|
function prependListener(type, listener) {
|
|
|
|
return _addListener(this, type, listener, true);
|
|
|
|
};
|
2011-03-15 14:01:24 +01:00
|
|
|
|
2016-04-04 04:55:22 +02:00
|
|
|
function _onceWrap(target, type, listener) {
|
2013-11-12 21:19:13 +01:00
|
|
|
var fired = false;
|
2011-03-18 21:02:14 +01:00
|
|
|
function g() {
|
2016-04-04 04:55:22 +02:00
|
|
|
target.removeListener(type, g);
|
2013-11-12 21:19:13 +01:00
|
|
|
if (!fired) {
|
|
|
|
fired = true;
|
2016-04-04 04:55:22 +02:00
|
|
|
listener.apply(target, arguments);
|
2013-11-12 21:19:13 +01:00
|
|
|
}
|
2013-02-27 09:15:28 +01:00
|
|
|
}
|
2011-03-18 21:02:14 +01:00
|
|
|
g.listener = listener;
|
2016-04-04 04:55:22 +02:00
|
|
|
return g;
|
|
|
|
}
|
2010-12-07 17:44:21 +01:00
|
|
|
|
2016-04-04 04:55:22 +02:00
|
|
|
EventEmitter.prototype.once = function once(type, listener) {
|
|
|
|
if (typeof listener !== 'function')
|
|
|
|
throw new TypeError('"listener" argument must be a function');
|
|
|
|
this.on(type, _onceWrap(this, type, listener));
|
2010-12-07 17:44:21 +01:00
|
|
|
return this;
|
2010-10-12 22:52:26 +02:00
|
|
|
};
|
|
|
|
|
2016-04-04 04:55:22 +02:00
|
|
|
EventEmitter.prototype.prependOnceListener =
|
|
|
|
function prependOnceListener(type, listener) {
|
|
|
|
if (typeof listener !== 'function')
|
|
|
|
throw new TypeError('"listener" argument must be a function');
|
|
|
|
this.prependListener(type, _onceWrap(this, type, listener));
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
2012-08-01 00:57:15 +02:00
|
|
|
// emits a 'removeListener' event iff the listener was removed
|
2014-05-05 16:48:51 +02:00
|
|
|
EventEmitter.prototype.removeListener =
|
|
|
|
function removeListener(type, listener) {
|
2016-04-29 04:47:06 +02:00
|
|
|
var list, events, position, i, originalListener;
|
2015-01-15 18:55:29 +01:00
|
|
|
|
2015-01-29 02:05:53 +01:00
|
|
|
if (typeof listener !== 'function')
|
2015-10-14 23:10:25 +02:00
|
|
|
throw new TypeError('"listener" argument must be a function');
|
2015-01-15 18:55:29 +01:00
|
|
|
|
2015-02-05 21:35:33 +01:00
|
|
|
events = this._events;
|
|
|
|
if (!events)
|
|
|
|
return this;
|
|
|
|
|
|
|
|
list = events[type];
|
|
|
|
if (!list)
|
2015-01-15 18:55:29 +01:00
|
|
|
return this;
|
|
|
|
|
2015-02-11 23:00:12 +01:00
|
|
|
if (list === listener || (list.listener && list.listener === listener)) {
|
|
|
|
if (--this._eventsCount === 0)
|
2016-04-07 02:33:28 +02:00
|
|
|
this._events = new EventHandlers();
|
2015-02-11 23:00:12 +01:00
|
|
|
else {
|
|
|
|
delete events[type];
|
|
|
|
if (events.removeListener)
|
2016-04-29 04:47:06 +02:00
|
|
|
this.emit('removeListener', type, list.listener || listener);
|
2015-02-11 23:00:12 +01:00
|
|
|
}
|
2015-02-05 21:35:33 +01:00
|
|
|
} else if (typeof list !== 'function') {
|
2015-02-11 23:00:12 +01:00
|
|
|
position = -1;
|
|
|
|
|
|
|
|
for (i = list.length; i-- > 0;) {
|
2015-01-15 18:55:29 +01:00
|
|
|
if (list[i] === listener ||
|
|
|
|
(list[i].listener && list[i].listener === listener)) {
|
2016-04-29 04:47:06 +02:00
|
|
|
originalListener = list[i].listener;
|
2015-01-15 18:55:29 +01:00
|
|
|
position = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (position < 0)
|
|
|
|
return this;
|
|
|
|
|
|
|
|
if (list.length === 1) {
|
2015-02-11 23:00:12 +01:00
|
|
|
list[0] = undefined;
|
|
|
|
if (--this._eventsCount === 0) {
|
2016-04-07 02:33:28 +02:00
|
|
|
this._events = new EventHandlers();
|
2015-02-11 23:00:12 +01:00
|
|
|
return this;
|
2015-04-28 19:46:14 +02:00
|
|
|
} else {
|
2015-02-11 23:00:12 +01:00
|
|
|
delete events[type];
|
2015-04-28 19:46:14 +02:00
|
|
|
}
|
2015-01-15 18:55:29 +01:00
|
|
|
} else {
|
|
|
|
spliceOne(list, position);
|
|
|
|
}
|
|
|
|
|
2015-02-05 21:35:33 +01:00
|
|
|
if (events.removeListener)
|
2016-04-29 04:47:06 +02:00
|
|
|
this.emit('removeListener', type, originalListener || listener);
|
2011-03-18 21:02:14 +01:00
|
|
|
}
|
|
|
|
|
2013-02-21 08:54:38 +01:00
|
|
|
return this;
|
2015-01-15 18:55:29 +01:00
|
|
|
};
|
2010-04-21 03:13:07 +02:00
|
|
|
|
2014-05-05 16:48:51 +02:00
|
|
|
EventEmitter.prototype.removeAllListeners =
|
|
|
|
function removeAllListeners(type) {
|
2015-02-05 21:35:33 +01:00
|
|
|
var listeners, events;
|
2015-01-15 18:55:29 +01:00
|
|
|
|
2015-02-05 21:35:33 +01:00
|
|
|
events = this._events;
|
|
|
|
if (!events)
|
2015-01-15 18:55:29 +01:00
|
|
|
return this;
|
|
|
|
|
|
|
|
// not listening for removeListener, no need to emit
|
2015-02-05 21:35:33 +01:00
|
|
|
if (!events.removeListener) {
|
2015-02-11 23:00:12 +01:00
|
|
|
if (arguments.length === 0) {
|
2016-04-07 02:33:28 +02:00
|
|
|
this._events = new EventHandlers();
|
2015-02-11 23:00:12 +01:00
|
|
|
this._eventsCount = 0;
|
|
|
|
} else if (events[type]) {
|
|
|
|
if (--this._eventsCount === 0)
|
2016-04-07 02:33:28 +02:00
|
|
|
this._events = new EventHandlers();
|
2015-02-11 23:00:12 +01:00
|
|
|
else
|
|
|
|
delete events[type];
|
|
|
|
}
|
2015-01-15 18:55:29 +01:00
|
|
|
return this;
|
|
|
|
}
|
2012-08-01 01:29:10 +02:00
|
|
|
|
2015-01-15 18:55:29 +01:00
|
|
|
// emit removeListener for all listeners on all events
|
|
|
|
if (arguments.length === 0) {
|
2015-02-05 21:35:33 +01:00
|
|
|
var keys = Object.keys(events);
|
|
|
|
for (var i = 0, key; i < keys.length; ++i) {
|
|
|
|
key = keys[i];
|
2015-01-15 18:55:29 +01:00
|
|
|
if (key === 'removeListener') continue;
|
|
|
|
this.removeAllListeners(key);
|
|
|
|
}
|
|
|
|
this.removeAllListeners('removeListener');
|
2016-04-07 02:33:28 +02:00
|
|
|
this._events = new EventHandlers();
|
2015-02-11 23:00:12 +01:00
|
|
|
this._eventsCount = 0;
|
2015-01-15 18:55:29 +01:00
|
|
|
return this;
|
|
|
|
}
|
2011-04-07 16:57:33 +02:00
|
|
|
|
2015-02-05 21:35:33 +01:00
|
|
|
listeners = events[type];
|
2013-02-21 00:19:05 +01:00
|
|
|
|
2015-01-29 02:05:53 +01:00
|
|
|
if (typeof listeners === 'function') {
|
2015-01-15 18:55:29 +01:00
|
|
|
this.removeListener(type, listeners);
|
2015-02-11 23:00:12 +01:00
|
|
|
} else if (listeners) {
|
2015-01-15 18:55:29 +01:00
|
|
|
// LIFO order
|
2015-02-11 23:00:12 +01:00
|
|
|
do {
|
2015-01-15 18:55:29 +01:00
|
|
|
this.removeListener(type, listeners[listeners.length - 1]);
|
2015-02-11 23:00:12 +01:00
|
|
|
} while (listeners[0]);
|
2015-01-15 18:55:29 +01:00
|
|
|
}
|
2012-08-01 00:57:15 +02:00
|
|
|
|
2015-01-15 18:55:29 +01:00
|
|
|
return this;
|
|
|
|
};
|
2010-04-21 03:13:07 +02:00
|
|
|
|
2014-05-05 16:48:51 +02:00
|
|
|
EventEmitter.prototype.listeners = function listeners(type) {
|
2015-02-05 21:35:33 +01:00
|
|
|
var evlistener;
|
2013-03-04 20:59:55 +01:00
|
|
|
var ret;
|
2015-02-05 21:35:33 +01:00
|
|
|
var events = this._events;
|
|
|
|
|
|
|
|
if (!events)
|
2013-03-04 20:59:55 +01:00
|
|
|
ret = [];
|
2015-02-05 21:35:33 +01:00
|
|
|
else {
|
|
|
|
evlistener = events[type];
|
|
|
|
if (!evlistener)
|
|
|
|
ret = [];
|
|
|
|
else if (typeof evlistener === 'function')
|
2016-03-04 15:00:48 +01:00
|
|
|
ret = [evlistener.listener || evlistener];
|
2015-02-05 21:35:33 +01:00
|
|
|
else
|
2016-03-04 15:00:48 +01:00
|
|
|
ret = unwrapListeners(evlistener);
|
2015-02-05 21:35:33 +01:00
|
|
|
}
|
|
|
|
|
2013-03-04 20:59:55 +01:00
|
|
|
return ret;
|
2010-10-12 22:52:26 +02:00
|
|
|
};
|
2013-02-14 09:48:11 +01:00
|
|
|
|
|
|
|
EventEmitter.listenerCount = function(emitter, type) {
|
2015-09-02 18:23:49 +02:00
|
|
|
if (typeof emitter.listenerCount === 'function') {
|
|
|
|
return emitter.listenerCount(type);
|
|
|
|
} else {
|
|
|
|
return listenerCount.call(emitter, type);
|
|
|
|
}
|
2015-08-11 20:31:50 +02:00
|
|
|
};
|
|
|
|
|
2015-09-02 18:23:49 +02:00
|
|
|
EventEmitter.prototype.listenerCount = listenerCount;
|
|
|
|
function listenerCount(type) {
|
2015-08-11 20:31:50 +02:00
|
|
|
const events = this._events;
|
2015-02-05 21:35:33 +01:00
|
|
|
|
|
|
|
if (events) {
|
2015-08-11 20:31:50 +02:00
|
|
|
const evlistener = events[type];
|
|
|
|
|
|
|
|
if (typeof evlistener === 'function') {
|
|
|
|
return 1;
|
|
|
|
} else if (evlistener) {
|
|
|
|
return evlistener.length;
|
|
|
|
}
|
2015-02-05 21:35:33 +01:00
|
|
|
}
|
|
|
|
|
2015-08-11 20:31:50 +02:00
|
|
|
return 0;
|
2016-01-15 09:53:11 +01:00
|
|
|
}
|
2014-12-20 00:04:35 +01:00
|
|
|
|
2016-03-09 06:29:38 +01:00
|
|
|
EventEmitter.prototype.eventNames = function eventNames() {
|
2016-03-21 12:28:17 +01:00
|
|
|
return this._eventsCount > 0 ? Reflect.ownKeys(this._events) : [];
|
2016-03-09 06:29:38 +01:00
|
|
|
};
|
|
|
|
|
2014-12-20 00:04:35 +01:00
|
|
|
// About 1.5x faster than the two-arg version of Array#splice().
|
|
|
|
function spliceOne(list, index) {
|
|
|
|
for (var i = index, k = i + 1, n = list.length; k < n; i += 1, k += 1)
|
|
|
|
list[i] = list[k];
|
|
|
|
list.pop();
|
|
|
|
}
|
2015-02-05 21:35:33 +01:00
|
|
|
|
2015-03-05 04:21:47 +01:00
|
|
|
function arrayClone(arr, i) {
|
|
|
|
var copy = new Array(i);
|
|
|
|
while (i--)
|
|
|
|
copy[i] = arr[i];
|
|
|
|
return copy;
|
2015-02-05 21:35:33 +01:00
|
|
|
}
|
2016-03-04 15:00:48 +01:00
|
|
|
|
|
|
|
function unwrapListeners(arr) {
|
|
|
|
const ret = new Array(arr.length);
|
|
|
|
for (var i = 0; i < ret.length; ++i) {
|
|
|
|
ret[i] = arr[i].listener || arr[i];
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|