emitter.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. (function() {
  2. var Emitter, Mixin, Signal, Subscription, removeFromArray, subscriptionRemovedPattern, _ref,
  3. __hasProp = {}.hasOwnProperty,
  4. __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
  5. __slice = [].slice;
  6. Mixin = require('mixto');
  7. Signal = null;
  8. Subscription = null;
  9. subscriptionRemovedPattern = /^(last-)?.+-subscription-removed$/;
  10. module.exports = Emitter = (function(_super) {
  11. __extends(Emitter, _super);
  12. function Emitter() {
  13. _ref = Emitter.__super__.constructor.apply(this, arguments);
  14. return _ref;
  15. }
  16. Emitter.prototype.eventHandlersByEventName = null;
  17. Emitter.prototype.eventHandlersByNamespace = null;
  18. Emitter.prototype.subscriptionCounts = null;
  19. Emitter.prototype.pauseCountsByEventName = null;
  20. Emitter.prototype.queuedEventsByEventName = null;
  21. Emitter.prototype.globalPauseCount = null;
  22. Emitter.prototype.globalQueuedEvents = null;
  23. Emitter.prototype.signalsByEventName = null;
  24. Emitter.prototype.on = function(eventNames, handler) {
  25. var eventName, namespace, _base, _base1, _base2, _i, _len, _ref1, _ref2;
  26. _ref1 = eventNames.split(/\s+/);
  27. for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
  28. eventName = _ref1[_i];
  29. if (!(eventName !== '')) {
  30. continue;
  31. }
  32. _ref2 = eventName.split('.'), eventName = _ref2[0], namespace = _ref2[1];
  33. this.emit("" + eventName + "-subscription-will-be-added", handler);
  34. if (this.incrementSubscriptionCount(eventName) === 1) {
  35. this.emit("first-" + eventName + "-subscription-will-be-added", handler);
  36. }
  37. if (this.eventHandlersByEventName == null) {
  38. this.eventHandlersByEventName = {};
  39. }
  40. if ((_base = this.eventHandlersByEventName)[eventName] == null) {
  41. _base[eventName] = [];
  42. }
  43. this.eventHandlersByEventName[eventName].push(handler);
  44. if (namespace) {
  45. if (this.eventHandlersByNamespace == null) {
  46. this.eventHandlersByNamespace = {};
  47. }
  48. if ((_base1 = this.eventHandlersByNamespace)[namespace] == null) {
  49. _base1[namespace] = {};
  50. }
  51. if ((_base2 = this.eventHandlersByNamespace[namespace])[eventName] == null) {
  52. _base2[eventName] = [];
  53. }
  54. this.eventHandlersByNamespace[namespace][eventName].push(handler);
  55. }
  56. this.emit("" + eventName + "-subscription-added", handler);
  57. }
  58. if (Subscription == null) {
  59. Subscription = require('./subscription');
  60. }
  61. return new Subscription(this, eventNames, handler);
  62. };
  63. Emitter.prototype.once = function(eventName, handler) {
  64. var subscription;
  65. return subscription = this.on(eventName, function() {
  66. var args;
  67. args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
  68. subscription.off();
  69. return handler.apply(null, args);
  70. });
  71. };
  72. Emitter.prototype.signal = function(eventName) {
  73. var _base;
  74. if (Signal == null) {
  75. Signal = require('./signal');
  76. }
  77. if (this.signalsByEventName == null) {
  78. this.signalsByEventName = {};
  79. }
  80. return (_base = this.signalsByEventName)[eventName] != null ? (_base = this.signalsByEventName)[eventName] : _base[eventName] = Signal.fromEmitter(this, eventName);
  81. };
  82. Emitter.prototype.behavior = function(eventName, initialValue) {
  83. return this.signal(eventName).toBehavior(initialValue);
  84. };
  85. Emitter.prototype.emit = function(eventName, payload) {
  86. var handler, handlers, queuedEvents, _i, _len, _ref1, _ref2, _ref3;
  87. if (arguments.length > 2 || /\s|\./.test(eventName)) {
  88. return this.emitSlow.apply(this, arguments);
  89. } else {
  90. if (this.globalQueuedEvents != null) {
  91. return this.globalQueuedEvents.push([eventName, payload]);
  92. } else {
  93. if (queuedEvents = (_ref1 = this.queuedEventsByEventName) != null ? _ref1[eventName] : void 0) {
  94. return queuedEvents.push([eventName, payload]);
  95. } else if (handlers = (_ref2 = this.eventHandlersByEventName) != null ? _ref2[eventName] : void 0) {
  96. _ref3 = handlers.slice();
  97. for (_i = 0, _len = _ref3.length; _i < _len; _i++) {
  98. handler = _ref3[_i];
  99. handler(payload);
  100. }
  101. return this.emit("after-" + eventName, payload);
  102. }
  103. }
  104. }
  105. };
  106. Emitter.prototype.emitSlow = function() {
  107. var args, eventName, handlers, namespace, queuedEvents, _ref1, _ref2, _ref3, _ref4, _ref5, _ref6;
  108. eventName = arguments[0], args = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
  109. if (this.globalQueuedEvents) {
  110. return this.globalQueuedEvents.push([eventName].concat(__slice.call(args)));
  111. } else {
  112. _ref1 = eventName.split('.'), eventName = _ref1[0], namespace = _ref1[1];
  113. if (namespace) {
  114. if (queuedEvents = (_ref2 = this.queuedEventsByEventName) != null ? _ref2[eventName] : void 0) {
  115. return queuedEvents.push(["" + eventName + "." + namespace].concat(__slice.call(args)));
  116. } else if (handlers = (_ref3 = this.eventHandlersByNamespace) != null ? (_ref4 = _ref3[namespace]) != null ? _ref4[eventName] : void 0 : void 0) {
  117. (function(func, args, ctor) {
  118. ctor.prototype = func.prototype;
  119. var child = new ctor, result = func.apply(child, args);
  120. return Object(result) === result ? result : child;
  121. })(Array, handlers, function(){}).forEach(function(handler) {
  122. return handler.apply(null, args);
  123. });
  124. return this.emit.apply(this, ["after-" + eventName].concat(__slice.call(args)));
  125. }
  126. } else {
  127. if (queuedEvents = (_ref5 = this.queuedEventsByEventName) != null ? _ref5[eventName] : void 0) {
  128. return queuedEvents.push([eventName].concat(__slice.call(args)));
  129. } else if (handlers = (_ref6 = this.eventHandlersByEventName) != null ? _ref6[eventName] : void 0) {
  130. (function(func, args, ctor) {
  131. ctor.prototype = func.prototype;
  132. var child = new ctor, result = func.apply(child, args);
  133. return Object(result) === result ? result : child;
  134. })(Array, handlers, function(){}).forEach(function(handler) {
  135. return handler.apply(null, args);
  136. });
  137. return this.emit.apply(this, ["after-" + eventName].concat(__slice.call(args)));
  138. }
  139. }
  140. }
  141. };
  142. Emitter.prototype.off = function(eventNames, handler) {
  143. var eventHandlers, eventName, handlers, namespace, namespaceHandlers, _i, _j, _k, _l, _len, _len1, _len2, _len3, _ref1, _ref10, _ref2, _ref3, _ref4, _ref5, _ref6, _ref7, _ref8, _ref9;
  144. if (eventNames) {
  145. _ref1 = eventNames.split(/\s+/);
  146. for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
  147. eventName = _ref1[_i];
  148. if (!(eventName !== '')) {
  149. continue;
  150. }
  151. _ref2 = eventName.split('.'), eventName = _ref2[0], namespace = _ref2[1];
  152. if (eventName === '') {
  153. eventName = void 0;
  154. }
  155. if (namespace) {
  156. if (eventName) {
  157. handlers = (_ref3 = (_ref4 = this.eventHandlersByNamespace) != null ? (_ref5 = _ref4[namespace]) != null ? _ref5[eventName] : void 0 : void 0) != null ? _ref3 : [];
  158. if (handler != null) {
  159. removeFromArray(handlers, handler);
  160. this.off(eventName, handler);
  161. } else {
  162. _ref6 = (function(func, args, ctor) {
  163. ctor.prototype = func.prototype;
  164. var child = new ctor, result = func.apply(child, args);
  165. return Object(result) === result ? result : child;
  166. })(Array, handlers, function(){});
  167. for (_j = 0, _len1 = _ref6.length; _j < _len1; _j++) {
  168. handler = _ref6[_j];
  169. removeFromArray(handlers, handler);
  170. this.off(eventName, handler);
  171. }
  172. }
  173. } else {
  174. namespaceHandlers = (_ref7 = (_ref8 = this.eventHandlersByNamespace) != null ? _ref8[namespace] : void 0) != null ? _ref7 : {};
  175. if (handler != null) {
  176. for (eventName in namespaceHandlers) {
  177. handlers = namespaceHandlers[eventName];
  178. removeFromArray(handlers, handler);
  179. this.off(eventName, handler);
  180. }
  181. } else {
  182. for (eventName in namespaceHandlers) {
  183. handlers = namespaceHandlers[eventName];
  184. _ref9 = (function(func, args, ctor) {
  185. ctor.prototype = func.prototype;
  186. var child = new ctor, result = func.apply(child, args);
  187. return Object(result) === result ? result : child;
  188. })(Array, handlers, function(){});
  189. for (_k = 0, _len2 = _ref9.length; _k < _len2; _k++) {
  190. handler = _ref9[_k];
  191. removeFromArray(handlers, handler);
  192. this.off(eventName, handler);
  193. }
  194. }
  195. }
  196. }
  197. } else {
  198. eventHandlers = (_ref10 = this.eventHandlersByEventName) != null ? _ref10[eventName] : void 0;
  199. if (eventHandlers == null) {
  200. return;
  201. }
  202. if (handler == null) {
  203. for (_l = 0, _len3 = eventHandlers.length; _l < _len3; _l++) {
  204. handler = eventHandlers[_l];
  205. this.off(eventName, handler);
  206. }
  207. return;
  208. }
  209. if (removeFromArray(eventHandlers, handler)) {
  210. this.decrementSubscriptionCount(eventName);
  211. this.emit("" + eventName + "-subscription-removed", handler);
  212. if (this.getSubscriptionCount(eventName) === 0) {
  213. this.emit("last-" + eventName + "-subscription-removed", handler);
  214. delete this.eventHandlersByEventName[eventName];
  215. }
  216. }
  217. }
  218. }
  219. } else {
  220. for (eventName in this.eventHandlersByEventName) {
  221. if (!subscriptionRemovedPattern.test(eventName)) {
  222. this.off(eventName);
  223. }
  224. }
  225. for (eventName in this.eventHandlersByEventName) {
  226. this.off(eventName);
  227. }
  228. return this.eventHandlersByNamespace = {};
  229. }
  230. };
  231. Emitter.prototype.pauseEvents = function(eventNames) {
  232. var eventName, _base, _base1, _i, _len, _ref1, _results;
  233. if (eventNames) {
  234. _ref1 = eventNames.split(/\s+/);
  235. _results = [];
  236. for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
  237. eventName = _ref1[_i];
  238. if (!(eventName !== '')) {
  239. continue;
  240. }
  241. if (this.pauseCountsByEventName == null) {
  242. this.pauseCountsByEventName = {};
  243. }
  244. if (this.queuedEventsByEventName == null) {
  245. this.queuedEventsByEventName = {};
  246. }
  247. if ((_base = this.pauseCountsByEventName)[eventName] == null) {
  248. _base[eventName] = 0;
  249. }
  250. this.pauseCountsByEventName[eventName]++;
  251. _results.push((_base1 = this.queuedEventsByEventName)[eventName] != null ? (_base1 = this.queuedEventsByEventName)[eventName] : _base1[eventName] = []);
  252. }
  253. return _results;
  254. } else {
  255. if (this.globalPauseCount == null) {
  256. this.globalPauseCount = 0;
  257. }
  258. if (this.globalQueuedEvents == null) {
  259. this.globalQueuedEvents = [];
  260. }
  261. return this.globalPauseCount++;
  262. }
  263. };
  264. Emitter.prototype.resumeEvents = function(eventNames) {
  265. var event, eventName, queuedEvents, _i, _j, _len, _len1, _ref1, _ref2, _results, _results1;
  266. if (eventNames) {
  267. _ref1 = eventNames.split(/\s+/);
  268. _results = [];
  269. for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
  270. eventName = _ref1[_i];
  271. if (eventName !== '') {
  272. if (((_ref2 = this.pauseCountsByEventName) != null ? _ref2[eventName] : void 0) > 0 && --this.pauseCountsByEventName[eventName] === 0) {
  273. queuedEvents = this.queuedEventsByEventName[eventName];
  274. this.queuedEventsByEventName[eventName] = null;
  275. _results.push((function() {
  276. var _j, _len1, _results1;
  277. _results1 = [];
  278. for (_j = 0, _len1 = queuedEvents.length; _j < _len1; _j++) {
  279. event = queuedEvents[_j];
  280. _results1.push(this.emit.apply(this, event));
  281. }
  282. return _results1;
  283. }).call(this));
  284. } else {
  285. _results.push(void 0);
  286. }
  287. }
  288. }
  289. return _results;
  290. } else {
  291. for (eventName in this.pauseCountsByEventName) {
  292. this.resumeEvents(eventName);
  293. }
  294. if (this.globalPauseCount > 0 && --this.globalPauseCount === 0) {
  295. queuedEvents = this.globalQueuedEvents;
  296. this.globalQueuedEvents = null;
  297. _results1 = [];
  298. for (_j = 0, _len1 = queuedEvents.length; _j < _len1; _j++) {
  299. event = queuedEvents[_j];
  300. _results1.push(this.emit.apply(this, event));
  301. }
  302. return _results1;
  303. }
  304. }
  305. };
  306. Emitter.prototype.incrementSubscriptionCount = function(eventName) {
  307. var _base;
  308. if (this.subscriptionCounts == null) {
  309. this.subscriptionCounts = {};
  310. }
  311. if ((_base = this.subscriptionCounts)[eventName] == null) {
  312. _base[eventName] = 0;
  313. }
  314. return ++this.subscriptionCounts[eventName];
  315. };
  316. Emitter.prototype.decrementSubscriptionCount = function(eventName) {
  317. var count;
  318. count = --this.subscriptionCounts[eventName];
  319. if (count === 0) {
  320. delete this.subscriptionCounts[eventName];
  321. }
  322. return count;
  323. };
  324. Emitter.prototype.getSubscriptionCount = function(eventName) {
  325. var count, name, total, _ref1, _ref2, _ref3;
  326. if (eventName != null) {
  327. return (_ref1 = (_ref2 = this.subscriptionCounts) != null ? _ref2[eventName] : void 0) != null ? _ref1 : 0;
  328. } else {
  329. total = 0;
  330. _ref3 = this.subscriptionCounts;
  331. for (name in _ref3) {
  332. count = _ref3[name];
  333. total += count;
  334. }
  335. return total;
  336. }
  337. };
  338. Emitter.prototype.hasSubscriptions = function(eventName) {
  339. return this.getSubscriptionCount(eventName) > 0;
  340. };
  341. return Emitter;
  342. })(Mixin);
  343. removeFromArray = function(array, element) {
  344. var index;
  345. index = array.indexOf(element);
  346. if (index > -1) {
  347. array.splice(index, 1);
  348. return true;
  349. } else {
  350. return false;
  351. }
  352. };
  353. }).call(this);