Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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. Object.defineProperty(EventEmitter, 'defaultMaxListeners', {
  60. enumerable: true,
  61. get: function() {
  62. return defaultMaxListeners;
  63. },
  64. set: function(arg) {
  65. if (typeof arg !== 'number' || arg < 0 || NumberIsNaN(arg)) {
  66. throw new RangeError('The value of "defaultMaxListeners" is out of range. It must be a non-negative number. Received ' + arg + '.');
  67. }
  68. defaultMaxListeners = arg;
  69. }
  70. });
  71. EventEmitter.init = function() {
  72. if (this._events === undefined ||
  73. this._events === Object.getPrototypeOf(this)._events) {
  74. this._events = Object.create(null);
  75. this._eventsCount = 0;
  76. }
  77. this._maxListeners = this._maxListeners || undefined;
  78. };
  79. // Obviously not all Emitters should be limited to 10. This function allows
  80. // that to be increased. Set to zero for unlimited.
  81. EventEmitter.prototype.setMaxListeners = function setMaxListeners(n) {
  82. if (typeof n !== 'number' || n < 0 || NumberIsNaN(n)) {
  83. throw new RangeError('The value of "n" is out of range. It must be a non-negative number. Received ' + n + '.');
  84. }
  85. this._maxListeners = n;
  86. return this;
  87. };
  88. function $getMaxListeners(that) {
  89. if (that._maxListeners === undefined)
  90. return EventEmitter.defaultMaxListeners;
  91. return that._maxListeners;
  92. }
  93. EventEmitter.prototype.getMaxListeners = function getMaxListeners() {
  94. return $getMaxListeners(this);
  95. };
  96. EventEmitter.prototype.emit = function emit(type) {
  97. var args = [];
  98. for (var i = 1; i < arguments.length; i++) args.push(arguments[i]);
  99. var doError = (type === 'error');
  100. var events = this._events;
  101. if (events !== undefined)
  102. doError = (doError && events.error === undefined);
  103. else if (!doError)
  104. return false;
  105. // If there is no 'error' event listener then throw.
  106. if (doError) {
  107. var er;
  108. if (args.length > 0)
  109. er = args[0];
  110. if (er instanceof Error) {
  111. // Note: The comments on the `throw` lines are intentional, they show
  112. // up in Node's output if this results in an unhandled exception.
  113. throw er; // Unhandled 'error' event
  114. }
  115. // At least give some kind of context to the user
  116. var err = new Error('Unhandled error.' + (er ? ' (' + er.message + ')' : ''));
  117. err.context = er;
  118. throw err; // Unhandled 'error' event
  119. }
  120. var handler = events[type];
  121. if (handler === undefined)
  122. return false;
  123. if (typeof handler === 'function') {
  124. ReflectApply(handler, this, args);
  125. } else {
  126. var len = handler.length;
  127. var listeners = arrayClone(handler, len);
  128. for (var i = 0; i < len; ++i)
  129. ReflectApply(listeners[i], this, args);
  130. }
  131. return true;
  132. };
  133. function _addListener(target, type, listener, prepend) {
  134. var m;
  135. var events;
  136. var existing;
  137. if (typeof listener !== 'function') {
  138. throw new TypeError('The "listener" argument must be of type Function. Received type ' + typeof listener);
  139. }
  140. events = target._events;
  141. if (events === undefined) {
  142. events = target._events = Object.create(null);
  143. target._eventsCount = 0;
  144. } else {
  145. // To avoid recursion in the case that type === "newListener"! Before
  146. // adding it to the listeners, first emit "newListener".
  147. if (events.newListener !== undefined) {
  148. target.emit('newListener', type,
  149. listener.listener ? listener.listener : listener);
  150. // Re-assign `events` because a newListener handler could have caused the
  151. // this._events to be assigned to a new object
  152. events = target._events;
  153. }
  154. existing = events[type];
  155. }
  156. if (existing === undefined) {
  157. // Optimize the case of one listener. Don't need the extra array object.
  158. existing = events[type] = listener;
  159. ++target._eventsCount;
  160. } else {
  161. if (typeof existing === 'function') {
  162. // Adding the second element, need to change to array.
  163. existing = events[type] =
  164. prepend ? [listener, existing] : [existing, listener];
  165. // If we've already got an array, just append.
  166. } else if (prepend) {
  167. existing.unshift(listener);
  168. } else {
  169. existing.push(listener);
  170. }
  171. // Check for listener leak
  172. m = $getMaxListeners(target);
  173. if (m > 0 && existing.length > m && !existing.warned) {
  174. existing.warned = true;
  175. // No error code for this since it is a Warning
  176. // eslint-disable-next-line no-restricted-syntax
  177. var w = new Error('Possible EventEmitter memory leak detected. ' +
  178. existing.length + ' ' + String(type) + ' listeners ' +
  179. 'added. Use emitter.setMaxListeners() to ' +
  180. 'increase limit');
  181. w.name = 'MaxListenersExceededWarning';
  182. w.emitter = target;
  183. w.type = type;
  184. w.count = existing.length;
  185. ProcessEmitWarning(w);
  186. }
  187. }
  188. return target;
  189. }
  190. EventEmitter.prototype.addListener = function addListener(type, listener) {
  191. return _addListener(this, type, listener, false);
  192. };
  193. EventEmitter.prototype.on = EventEmitter.prototype.addListener;
  194. EventEmitter.prototype.prependListener =
  195. function prependListener(type, listener) {
  196. return _addListener(this, type, listener, true);
  197. };
  198. function onceWrapper() {
  199. var args = [];
  200. for (var i = 0; i < arguments.length; i++) args.push(arguments[i]);
  201. if (!this.fired) {
  202. this.target.removeListener(this.type, this.wrapFn);
  203. this.fired = true;
  204. ReflectApply(this.listener, this.target, args);
  205. }
  206. }
  207. function _onceWrap(target, type, listener) {
  208. var state = { fired: false, wrapFn: undefined, target: target, type: type, listener: listener };
  209. var wrapped = onceWrapper.bind(state);
  210. wrapped.listener = listener;
  211. state.wrapFn = wrapped;
  212. return wrapped;
  213. }
  214. EventEmitter.prototype.once = function once(type, listener) {
  215. if (typeof listener !== 'function') {
  216. throw new TypeError('The "listener" argument must be of type Function. Received type ' + typeof listener);
  217. }
  218. this.on(type, _onceWrap(this, type, listener));
  219. return this;
  220. };
  221. EventEmitter.prototype.prependOnceListener =
  222. function prependOnceListener(type, listener) {
  223. if (typeof listener !== 'function') {
  224. throw new TypeError('The "listener" argument must be of type Function. Received type ' + typeof listener);
  225. }
  226. this.prependListener(type, _onceWrap(this, type, listener));
  227. return this;
  228. };
  229. // Emits a 'removeListener' event if and only if the listener was removed.
  230. EventEmitter.prototype.removeListener =
  231. function removeListener(type, listener) {
  232. var list, events, position, i, originalListener;
  233. if (typeof listener !== 'function') {
  234. throw new TypeError('The "listener" argument must be of type Function. Received type ' + typeof listener);
  235. }
  236. events = this._events;
  237. if (events === undefined)
  238. return this;
  239. list = events[type];
  240. if (list === undefined)
  241. return this;
  242. if (list === listener || list.listener === listener) {
  243. if (--this._eventsCount === 0)
  244. this._events = Object.create(null);
  245. else {
  246. delete events[type];
  247. if (events.removeListener)
  248. this.emit('removeListener', type, list.listener || listener);
  249. }
  250. } else if (typeof list !== 'function') {
  251. position = -1;
  252. for (i = list.length - 1; i >= 0; i--) {
  253. if (list[i] === listener || list[i].listener === listener) {
  254. originalListener = list[i].listener;
  255. position = i;
  256. break;
  257. }
  258. }
  259. if (position < 0)
  260. return this;
  261. if (position === 0)
  262. list.shift();
  263. else {
  264. spliceOne(list, position);
  265. }
  266. if (list.length === 1)
  267. events[type] = list[0];
  268. if (events.removeListener !== undefined)
  269. this.emit('removeListener', type, originalListener || listener);
  270. }
  271. return this;
  272. };
  273. EventEmitter.prototype.off = EventEmitter.prototype.removeListener;
  274. EventEmitter.prototype.removeAllListeners =
  275. function removeAllListeners(type) {
  276. var listeners, events, i;
  277. events = this._events;
  278. if (events === undefined)
  279. return this;
  280. // not listening for removeListener, no need to emit
  281. if (events.removeListener === undefined) {
  282. if (arguments.length === 0) {
  283. this._events = Object.create(null);
  284. this._eventsCount = 0;
  285. } else if (events[type] !== undefined) {
  286. if (--this._eventsCount === 0)
  287. this._events = Object.create(null);
  288. else
  289. delete events[type];
  290. }
  291. return this;
  292. }
  293. // emit removeListener for all listeners on all events
  294. if (arguments.length === 0) {
  295. var keys = Object.keys(events);
  296. var key;
  297. for (i = 0; i < keys.length; ++i) {
  298. key = keys[i];
  299. if (key === 'removeListener') continue;
  300. this.removeAllListeners(key);
  301. }
  302. this.removeAllListeners('removeListener');
  303. this._events = Object.create(null);
  304. this._eventsCount = 0;
  305. return this;
  306. }
  307. listeners = events[type];
  308. if (typeof listeners === 'function') {
  309. this.removeListener(type, listeners);
  310. } else if (listeners !== undefined) {
  311. // LIFO order
  312. for (i = listeners.length - 1; i >= 0; i--) {
  313. this.removeListener(type, listeners[i]);
  314. }
  315. }
  316. return this;
  317. };
  318. function _listeners(target, type, unwrap) {
  319. var events = target._events;
  320. if (events === undefined)
  321. return [];
  322. var evlistener = events[type];
  323. if (evlistener === undefined)
  324. return [];
  325. if (typeof evlistener === 'function')
  326. return unwrap ? [evlistener.listener || evlistener] : [evlistener];
  327. return unwrap ?
  328. unwrapListeners(evlistener) : arrayClone(evlistener, evlistener.length);
  329. }
  330. EventEmitter.prototype.listeners = function listeners(type) {
  331. return _listeners(this, type, true);
  332. };
  333. EventEmitter.prototype.rawListeners = function rawListeners(type) {
  334. return _listeners(this, type, false);
  335. };
  336. EventEmitter.listenerCount = function(emitter, type) {
  337. if (typeof emitter.listenerCount === 'function') {
  338. return emitter.listenerCount(type);
  339. } else {
  340. return listenerCount.call(emitter, type);
  341. }
  342. };
  343. EventEmitter.prototype.listenerCount = listenerCount;
  344. function listenerCount(type) {
  345. var events = this._events;
  346. if (events !== undefined) {
  347. var evlistener = events[type];
  348. if (typeof evlistener === 'function') {
  349. return 1;
  350. } else if (evlistener !== undefined) {
  351. return evlistener.length;
  352. }
  353. }
  354. return 0;
  355. }
  356. EventEmitter.prototype.eventNames = function eventNames() {
  357. return this._eventsCount > 0 ? ReflectOwnKeys(this._events) : [];
  358. };
  359. function arrayClone(arr, n) {
  360. var copy = new Array(n);
  361. for (var i = 0; i < n; ++i)
  362. copy[i] = arr[i];
  363. return copy;
  364. }
  365. function spliceOne(list, index) {
  366. for (; index + 1 < list.length; index++)
  367. list[index] = list[index + 1];
  368. list.pop();
  369. }
  370. function unwrapListeners(arr) {
  371. var ret = new Array(arr.length);
  372. for (var i = 0; i < ret.length; ++i) {
  373. ret[i] = arr[i].listener || arr[i];
  374. }
  375. return ret;
  376. }