events.js 15 KB

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