You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

446 lines
13 KiB

4 years ago
  1. // Copyright Joyent, Inc. and other Node contributors.
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a
  4. // copy of this software and associated documentation files (the
  5. // "Software"), to deal in the Software without restriction, including
  6. // without limitation the rights to use, copy, modify, merge, publish,
  7. // distribute, sublicense, and/or sell copies of the Software, and to permit
  8. // persons to whom the Software is furnished to do so, subject to the
  9. // following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included
  12. // in all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  15. // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  16. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
  17. // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  18. // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  19. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  20. // USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. 'use strict';
  22. var R = typeof Reflect === 'object' ? Reflect : null
  23. var ReflectApply = R && typeof R.apply === 'function'
  24. ? R.apply
  25. : function ReflectApply(target, receiver, args) {
  26. return Function.prototype.apply.call(target, receiver, args);
  27. }
  28. var ReflectOwnKeys
  29. if (R && typeof R.ownKeys === 'function') {
  30. ReflectOwnKeys = R.ownKeys
  31. } else if (Object.getOwnPropertySymbols) {
  32. ReflectOwnKeys = function ReflectOwnKeys(target) {
  33. return Object.getOwnPropertyNames(target)
  34. .concat(Object.getOwnPropertySymbols(target));
  35. };
  36. } else {
  37. ReflectOwnKeys = function ReflectOwnKeys(target) {
  38. return Object.getOwnPropertyNames(target);
  39. };
  40. }
  41. function ProcessEmitWarning(warning) {
  42. if (console && console.warn) console.warn(warning);
  43. }
  44. var NumberIsNaN = Number.isNaN || function NumberIsNaN(value) {
  45. return value !== value;
  46. }
  47. function EventEmitter() {
  48. EventEmitter.init.call(this);
  49. }
  50. module.exports = EventEmitter;
  51. // Backwards-compat with node 0.10.x
  52. EventEmitter.EventEmitter = EventEmitter;
  53. EventEmitter.prototype._events = undefined;
  54. EventEmitter.prototype._eventsCount = 0;
  55. EventEmitter.prototype._maxListeners = undefined;
  56. // By default EventEmitters will print a warning if more than 10 listeners are
  57. // added to it. This is a useful default which helps finding memory leaks.
  58. var defaultMaxListeners = 10;
  59. function checkListener(listener) {
  60. if (typeof listener !== 'function') {
  61. throw new TypeError('The "listener" argument must be of type Function. Received type ' + typeof listener);
  62. }
  63. }
  64. Object.defineProperty(EventEmitter, 'defaultMaxListeners', {
  65. enumerable: true,
  66. get: function() {
  67. return defaultMaxListeners;
  68. },
  69. set: function(arg) {
  70. if (typeof arg !== 'number' || arg < 0 || NumberIsNaN(arg)) {
  71. throw new RangeError('The value of "defaultMaxListeners" is out of range. It must be a non-negative number. Received ' + arg + '.');
  72. }
  73. defaultMaxListeners = arg;
  74. }
  75. });
  76. EventEmitter.init = function() {
  77. if (this._events === undefined ||
  78. this._events === Object.getPrototypeOf(this)._events) {
  79. this._events = Object.create(null);
  80. this._eventsCount = 0;
  81. }
  82. this._maxListeners = this._maxListeners || undefined;
  83. };
  84. // Obviously not all Emitters should be limited to 10. This function allows
  85. // that to be increased. Set to zero for unlimited.
  86. EventEmitter.prototype.setMaxListeners = function setMaxListeners(n) {
  87. if (typeof n !== 'number' || n < 0 || NumberIsNaN(n)) {
  88. throw new RangeError('The value of "n" is out of range. It must be a non-negative number. Received ' + n + '.');
  89. }
  90. this._maxListeners = n;
  91. return this;
  92. };
  93. function _getMaxListeners(that) {
  94. if (that._maxListeners === undefined)
  95. return EventEmitter.defaultMaxListeners;
  96. return that._maxListeners;
  97. }
  98. EventEmitter.prototype.getMaxListeners = function getMaxListeners() {
  99. return _getMaxListeners(this);
  100. };
  101. EventEmitter.prototype.emit = function emit(type) {
  102. var args = [];
  103. for (var i = 1; i < arguments.length; i++) args.push(arguments[i]);
  104. var doError = (type === 'error');
  105. var events = this._events;
  106. if (events !== undefined)
  107. doError = (doError && events.error === undefined);
  108. else if (!doError)
  109. return false;
  110. // If there is no 'error' event listener then throw.
  111. if (doError) {
  112. var er;
  113. if (args.length > 0)
  114. er = args[0];
  115. if (er instanceof Error) {
  116. // Note: The comments on the `throw` lines are intentional, they show
  117. // up in Node's output if this results in an unhandled exception.
  118. throw er; // Unhandled 'error' event
  119. }
  120. // At least give some kind of context to the user
  121. var err = new Error('Unhandled error.' + (er ? ' (' + er.message + ')' : ''));
  122. err.context = er;
  123. throw err; // Unhandled 'error' event
  124. }
  125. var handler = events[type];
  126. if (handler === undefined)
  127. return false;
  128. if (typeof handler === 'function') {
  129. ReflectApply(handler, this, args);
  130. } else {
  131. var len = handler.length;
  132. var listeners = arrayClone(handler, len);
  133. for (var i = 0; i < len; ++i)
  134. ReflectApply(listeners[i], this, args);
  135. }
  136. return true;
  137. };
  138. function _addListener(target, type, listener, prepend) {
  139. var m;
  140. var events;
  141. var existing;
  142. checkListener(listener);
  143. events = target._events;
  144. if (events === undefined) {
  145. events = target._events = Object.create(null);
  146. target._eventsCount = 0;
  147. } else {
  148. // To avoid recursion in the case that type === "newListener"! Before
  149. // adding it to the listeners, first emit "newListener".
  150. if (events.newListener !== undefined) {
  151. target.emit('newListener', type,
  152. listener.listener ? listener.listener : listener);
  153. // Re-assign `events` because a newListener handler could have caused the
  154. // this._events to be assigned to a new object
  155. events = target._events;
  156. }
  157. existing = events[type];
  158. }
  159. if (existing === undefined) {
  160. // Optimize the case of one listener. Don't need the extra array object.
  161. existing = events[type] = listener;
  162. ++target._eventsCount;
  163. } else {
  164. if (typeof existing === 'function') {
  165. // Adding the second element, need to change to array.
  166. existing = events[type] =
  167. prepend ? [listener, existing] : [existing, listener];
  168. // If we've already got an array, just append.
  169. } else if (prepend) {
  170. existing.unshift(listener);
  171. } else {
  172. existing.push(listener);
  173. }
  174. // Check for listener leak
  175. m = _getMaxListeners(target);
  176. if (m > 0 && existing.length > m && !existing.warned) {
  177. existing.warned = true;
  178. // No error code for this since it is a Warning
  179. // eslint-disable-next-line no-restricted-syntax
  180. var w = new Error('Possible EventEmitter memory leak detected. ' +
  181. existing.length + ' ' + String(type) + ' listeners ' +
  182. 'added. Use emitter.setMaxListeners() to ' +
  183. 'increase limit');
  184. w.name = 'MaxListenersExceededWarning';
  185. w.emitter = target;
  186. w.type = type;
  187. w.count = existing.length;
  188. ProcessEmitWarning(w);
  189. }
  190. }
  191. return target;
  192. }
  193. EventEmitter.prototype.addListener = function addListener(type, listener) {
  194. return _addListener(this, type, listener, false);
  195. };
  196. EventEmitter.prototype.on = EventEmitter.prototype.addListener;
  197. EventEmitter.prototype.prependListener =
  198. function prependListener(type, listener) {
  199. return _addListener(this, type, listener, true);
  200. };
  201. function onceWrapper() {
  202. if (!this.fired) {
  203. this.target.removeListener(this.type, this.wrapFn);
  204. this.fired = true;
  205. if (arguments.length === 0)
  206. return this.listener.call(this.target);
  207. return this.listener.apply(this.target, arguments);
  208. }
  209. }
  210. function _onceWrap(target, type, listener) {
  211. var state = { fired: false, wrapFn: undefined, target: target, type: type, listener: listener };
  212. var wrapped = onceWrapper.bind(state);
  213. wrapped.listener = listener;
  214. state.wrapFn = wrapped;
  215. return wrapped;
  216. }
  217. EventEmitter.prototype.once = function once(type, listener) {
  218. checkListener(listener);
  219. this.on(type, _onceWrap(this, type, listener));
  220. return this;
  221. };
  222. EventEmitter.prototype.prependOnceListener =
  223. function prependOnceListener(type, listener) {
  224. checkListener(listener);
  225. this.prependListener(type, _onceWrap(this, type, listener));
  226. return this;
  227. };
  228. // Emits a 'removeListener' event if and only if the listener was removed.
  229. EventEmitter.prototype.removeListener =
  230. function removeListener(type, listener) {
  231. var list, events, position, i, originalListener;
  232. checkListener(listener);
  233. events = this._events;
  234. if (events === undefined)
  235. return this;
  236. list = events[type];
  237. if (list === undefined)
  238. return this;
  239. if (list === listener || list.listener === listener) {
  240. if (--this._eventsCount === 0)
  241. this._events = Object.create(null);
  242. else {
  243. delete events[type];
  244. if (events.removeListener)
  245. this.emit('removeListener', type, list.listener || listener);
  246. }
  247. } else if (typeof list !== 'function') {
  248. position = -1;
  249. for (i = list.length - 1; i >= 0; i--) {
  250. if (list[i] === listener || list[i].listener === listener) {
  251. originalListener = list[i].listener;
  252. position = i;
  253. break;
  254. }
  255. }
  256. if (position < 0)
  257. return this;
  258. if (position === 0)
  259. list.shift();
  260. else {
  261. spliceOne(list, position);
  262. }
  263. if (list.length === 1)
  264. events[type] = list[0];
  265. if (events.removeListener !== undefined)
  266. this.emit('removeListener', type, originalListener || listener);
  267. }
  268. return this;
  269. };
  270. EventEmitter.prototype.off = EventEmitter.prototype.removeListener;
  271. EventEmitter.prototype.removeAllListeners =
  272. function removeAllListeners(type) {
  273. var listeners, events, i;
  274. events = this._events;
  275. if (events === undefined)
  276. return this;
  277. // not listening for removeListener, no need to emit
  278. if (events.removeListener === undefined) {
  279. if (arguments.length === 0) {
  280. this._events = Object.create(null);
  281. this._eventsCount = 0;
  282. } else if (events[type] !== undefined) {
  283. if (--this._eventsCount === 0)
  284. this._events = Object.create(null);
  285. else
  286. delete events[type];
  287. }
  288. return this;
  289. }
  290. // emit removeListener for all listeners on all events
  291. if (arguments.length === 0) {
  292. var keys = Object.keys(events);
  293. var key;
  294. for (i = 0; i < keys.length; ++i) {
  295. key = keys[i];
  296. if (key === 'removeListener') continue;
  297. this.removeAllListeners(key);
  298. }
  299. this.removeAllListeners('removeListener');
  300. this._events = Object.create(null);
  301. this._eventsCount = 0;
  302. return this;
  303. }
  304. listeners = events[type];
  305. if (typeof listeners === 'function') {
  306. this.removeListener(type, listeners);
  307. } else if (listeners !== undefined) {
  308. // LIFO order
  309. for (i = listeners.length - 1; i >= 0; i--) {
  310. this.removeListener(type, listeners[i]);
  311. }
  312. }
  313. return this;
  314. };
  315. function _listeners(target, type, unwrap) {
  316. var events = target._events;
  317. if (events === undefined)
  318. return [];
  319. var evlistener = events[type];
  320. if (evlistener === undefined)
  321. return [];
  322. if (typeof evlistener === 'function')
  323. return unwrap ? [evlistener.listener || evlistener] : [evlistener];
  324. return unwrap ?
  325. unwrapListeners(evlistener) : arrayClone(evlistener, evlistener.length);
  326. }
  327. EventEmitter.prototype.listeners = function listeners(type) {
  328. return _listeners(this, type, true);
  329. };
  330. EventEmitter.prototype.rawListeners = function rawListeners(type) {
  331. return _listeners(this, type, false);
  332. };
  333. EventEmitter.listenerCount = function(emitter, type) {
  334. if (typeof emitter.listenerCount === 'function') {
  335. return emitter.listenerCount(type);
  336. } else {
  337. return listenerCount.call(emitter, type);
  338. }
  339. };
  340. EventEmitter.prototype.listenerCount = listenerCount;
  341. function listenerCount(type) {
  342. var events = this._events;
  343. if (events !== undefined) {
  344. var evlistener = events[type];
  345. if (typeof evlistener === 'function') {
  346. return 1;
  347. } else if (evlistener !== undefined) {
  348. return evlistener.length;
  349. }
  350. }
  351. return 0;
  352. }
  353. EventEmitter.prototype.eventNames = function eventNames() {
  354. return this._eventsCount > 0 ? ReflectOwnKeys(this._events) : [];
  355. };
  356. function arrayClone(arr, n) {
  357. var copy = new Array(n);
  358. for (var i = 0; i < n; ++i)
  359. copy[i] = arr[i];
  360. return copy;
  361. }
  362. function spliceOne(list, index) {
  363. for (; index + 1 < list.length; index++)
  364. list[index] = list[index + 1];
  365. list.pop();
  366. }
  367. function unwrapListeners(arr) {
  368. var ret = new Array(arr.length);
  369. for (var i = 0; i < ret.length; ++i) {
  370. ret[i] = arr[i].listener || arr[i];
  371. }
  372. return ret;
  373. }