events.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. (function() {
  2. var nodeEnv = typeof require !== 'undefined' && typeof process !== 'undefined';
  3. var __module = nodeEnv ? module : {exports:{}};
  4. var __filename = 'preview-scripts/__node_modules/events/events.js';
  5. var __require = nodeEnv ? function (request) {
  6. return cc.require(request);
  7. } : function (request) {
  8. return __quick_compile_project__.require(request, __filename);
  9. };
  10. function __define (exports, require, module) {
  11. if (!nodeEnv) {__quick_compile_project__.registerModule(__filename, module);}// Copyright Joyent, Inc. and other Node contributors.
  12. //
  13. // Permission is hereby granted, free of charge, to any person obtaining a
  14. // copy of this software and associated documentation files (the
  15. // "Software"), to deal in the Software without restriction, including
  16. // without limitation the rights to use, copy, modify, merge, publish,
  17. // distribute, sublicense, and/or sell copies of the Software, and to permit
  18. // persons to whom the Software is furnished to do so, subject to the
  19. // following conditions:
  20. //
  21. // The above copyright notice and this permission notice shall be included
  22. // in all copies or substantial portions of the Software.
  23. //
  24. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  25. // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
  27. // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  28. // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  29. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  30. // USE OR OTHER DEALINGS IN THE SOFTWARE.
  31. function EventEmitter() {
  32. this._events = this._events || {};
  33. this._maxListeners = this._maxListeners || undefined;
  34. }
  35. module.exports = EventEmitter;
  36. // Backwards-compat with node 0.10.x
  37. EventEmitter.EventEmitter = EventEmitter;
  38. EventEmitter.prototype._events = undefined;
  39. EventEmitter.prototype._maxListeners = undefined;
  40. // By default EventEmitters will print a warning if more than 10 listeners are
  41. // added to it. This is a useful default which helps finding memory leaks.
  42. EventEmitter.defaultMaxListeners = 10;
  43. // Obviously not all Emitters should be limited to 10. This function allows
  44. // that to be increased. Set to zero for unlimited.
  45. EventEmitter.prototype.setMaxListeners = function(n) {
  46. if (!isNumber(n) || n < 0 || isNaN(n))
  47. throw TypeError('n must be a positive number');
  48. this._maxListeners = n;
  49. return this;
  50. };
  51. EventEmitter.prototype.emit = function(type) {
  52. var er, handler, len, args, i, listeners;
  53. if (!this._events)
  54. this._events = {};
  55. // If there is no 'error' event listener then throw.
  56. if (type === 'error') {
  57. if (!this._events.error ||
  58. (isObject(this._events.error) && !this._events.error.length)) {
  59. er = arguments[1];
  60. if (er instanceof Error) {
  61. throw er; // Unhandled 'error' event
  62. } else {
  63. // At least give some kind of context to the user
  64. var err = new Error('Uncaught, unspecified "error" event. (' + er + ')');
  65. err.context = er;
  66. throw err;
  67. }
  68. }
  69. }
  70. handler = this._events[type];
  71. if (isUndefined(handler))
  72. return false;
  73. if (isFunction(handler)) {
  74. switch (arguments.length) {
  75. // fast cases
  76. case 1:
  77. handler.call(this);
  78. break;
  79. case 2:
  80. handler.call(this, arguments[1]);
  81. break;
  82. case 3:
  83. handler.call(this, arguments[1], arguments[2]);
  84. break;
  85. // slower
  86. default:
  87. args = Array.prototype.slice.call(arguments, 1);
  88. handler.apply(this, args);
  89. }
  90. } else if (isObject(handler)) {
  91. args = Array.prototype.slice.call(arguments, 1);
  92. listeners = handler.slice();
  93. len = listeners.length;
  94. for (i = 0; i < len; i++)
  95. listeners[i].apply(this, args);
  96. }
  97. return true;
  98. };
  99. EventEmitter.prototype.addListener = function(type, listener) {
  100. var m;
  101. if (!isFunction(listener))
  102. throw TypeError('listener must be a function');
  103. if (!this._events)
  104. this._events = {};
  105. // To avoid recursion in the case that type === "newListener"! Before
  106. // adding it to the listeners, first emit "newListener".
  107. if (this._events.newListener)
  108. this.emit('newListener', type,
  109. isFunction(listener.listener) ?
  110. listener.listener : listener);
  111. if (!this._events[type])
  112. // Optimize the case of one listener. Don't need the extra array object.
  113. this._events[type] = listener;
  114. else if (isObject(this._events[type]))
  115. // If we've already got an array, just append.
  116. this._events[type].push(listener);
  117. else
  118. // Adding the second element, need to change to array.
  119. this._events[type] = [this._events[type], listener];
  120. // Check for listener leak
  121. if (isObject(this._events[type]) && !this._events[type].warned) {
  122. if (!isUndefined(this._maxListeners)) {
  123. m = this._maxListeners;
  124. } else {
  125. m = EventEmitter.defaultMaxListeners;
  126. }
  127. if (m && m > 0 && this._events[type].length > m) {
  128. this._events[type].warned = true;
  129. console.error('(node) warning: possible EventEmitter memory ' +
  130. 'leak detected. %d listeners added. ' +
  131. 'Use emitter.setMaxListeners() to increase limit.',
  132. this._events[type].length);
  133. if (typeof console.trace === 'function') {
  134. // not supported in IE 10
  135. console.trace();
  136. }
  137. }
  138. }
  139. return this;
  140. };
  141. EventEmitter.prototype.on = EventEmitter.prototype.addListener;
  142. EventEmitter.prototype.once = function(type, listener) {
  143. if (!isFunction(listener))
  144. throw TypeError('listener must be a function');
  145. var fired = false;
  146. function g() {
  147. this.removeListener(type, g);
  148. if (!fired) {
  149. fired = true;
  150. listener.apply(this, arguments);
  151. }
  152. }
  153. g.listener = listener;
  154. this.on(type, g);
  155. return this;
  156. };
  157. // emits a 'removeListener' event iff the listener was removed
  158. EventEmitter.prototype.removeListener = function(type, listener) {
  159. var list, position, length, i;
  160. if (!isFunction(listener))
  161. throw TypeError('listener must be a function');
  162. if (!this._events || !this._events[type])
  163. return this;
  164. list = this._events[type];
  165. length = list.length;
  166. position = -1;
  167. if (list === listener ||
  168. (isFunction(list.listener) && list.listener === listener)) {
  169. delete this._events[type];
  170. if (this._events.removeListener)
  171. this.emit('removeListener', type, listener);
  172. } else if (isObject(list)) {
  173. for (i = length; i-- > 0;) {
  174. if (list[i] === listener ||
  175. (list[i].listener && list[i].listener === listener)) {
  176. position = i;
  177. break;
  178. }
  179. }
  180. if (position < 0)
  181. return this;
  182. if (list.length === 1) {
  183. list.length = 0;
  184. delete this._events[type];
  185. } else {
  186. list.splice(position, 1);
  187. }
  188. if (this._events.removeListener)
  189. this.emit('removeListener', type, listener);
  190. }
  191. return this;
  192. };
  193. EventEmitter.prototype.removeAllListeners = function(type) {
  194. var key, listeners;
  195. if (!this._events)
  196. return this;
  197. // not listening for removeListener, no need to emit
  198. if (!this._events.removeListener) {
  199. if (arguments.length === 0)
  200. this._events = {};
  201. else if (this._events[type])
  202. delete this._events[type];
  203. return this;
  204. }
  205. // emit removeListener for all listeners on all events
  206. if (arguments.length === 0) {
  207. for (key in this._events) {
  208. if (key === 'removeListener') continue;
  209. this.removeAllListeners(key);
  210. }
  211. this.removeAllListeners('removeListener');
  212. this._events = {};
  213. return this;
  214. }
  215. listeners = this._events[type];
  216. if (isFunction(listeners)) {
  217. this.removeListener(type, listeners);
  218. } else if (listeners) {
  219. // LIFO order
  220. while (listeners.length)
  221. this.removeListener(type, listeners[listeners.length - 1]);
  222. }
  223. delete this._events[type];
  224. return this;
  225. };
  226. EventEmitter.prototype.listeners = function(type) {
  227. var ret;
  228. if (!this._events || !this._events[type])
  229. ret = [];
  230. else if (isFunction(this._events[type]))
  231. ret = [this._events[type]];
  232. else
  233. ret = this._events[type].slice();
  234. return ret;
  235. };
  236. EventEmitter.prototype.listenerCount = function(type) {
  237. if (this._events) {
  238. var evlistener = this._events[type];
  239. if (isFunction(evlistener))
  240. return 1;
  241. else if (evlistener)
  242. return evlistener.length;
  243. }
  244. return 0;
  245. };
  246. EventEmitter.listenerCount = function(emitter, type) {
  247. return emitter.listenerCount(type);
  248. };
  249. function isFunction(arg) {
  250. return typeof arg === 'function';
  251. }
  252. function isNumber(arg) {
  253. return typeof arg === 'number';
  254. }
  255. function isObject(arg) {
  256. return typeof arg === 'object' && arg !== null;
  257. }
  258. function isUndefined(arg) {
  259. return arg === void 0;
  260. }
  261. }
  262. if (nodeEnv) {
  263. __define(__module.exports, __require, __module);
  264. }
  265. else {
  266. __quick_compile_project__.registerModuleFunc(__filename, function () {
  267. __define(__module.exports, __require, __module);
  268. });
  269. }
  270. })();