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.

287 lines
7.5 KiB

  1. // Generated by CoffeeScript 1.4.0
  2. (function() {
  3. var Event, EventEmitter, EventEmitterEnhanced, EventSystem, balUtilFlow, balUtilTypes, debug,
  4. __hasProp = {}.hasOwnProperty,
  5. __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; },
  6. __slice = [].slice;
  7. EventEmitter = require('events').EventEmitter;
  8. balUtilFlow = require(__dirname + '/flow');
  9. balUtilTypes = require(__dirname + '/types');
  10. debug = false;
  11. EventEmitterEnhanced = (function(_super) {
  12. __extends(EventEmitterEnhanced, _super);
  13. function EventEmitterEnhanced() {
  14. return EventEmitterEnhanced.__super__.constructor.apply(this, arguments);
  15. }
  16. EventEmitterEnhanced.prototype.emitAsync = function(eventName, data, next) {
  17. var listener, listeners, tasks, _i, _len;
  18. listeners = this.listeners(eventName);
  19. tasks = new balUtilFlow.Group(next);
  20. for (_i = 0, _len = listeners.length; _i < _len; _i++) {
  21. listener = listeners[_i];
  22. tasks.push({
  23. listener: listener
  24. }, function(complete) {
  25. return balUtilFlow.fireWithOptionalCallback(this.listener, [data, complete]);
  26. });
  27. }
  28. tasks.async();
  29. return this;
  30. };
  31. EventEmitterEnhanced.prototype.emitSync = function(eventName, data, next) {
  32. var listener, listeners, tasks, _i, _len;
  33. listeners = this.listeners(eventName);
  34. tasks = new balUtilFlow.Group(next);
  35. for (_i = 0, _len = listeners.length; _i < _len; _i++) {
  36. listener = listeners[_i];
  37. tasks.push({
  38. listener: listener
  39. }, function(complete) {
  40. return balUtilFlow.fireWithOptionalCallback(this.listener, [data, complete]);
  41. });
  42. }
  43. tasks.sync();
  44. return this;
  45. };
  46. return EventEmitterEnhanced;
  47. })(EventEmitter);
  48. Event = (function() {
  49. Event.prototype.name = null;
  50. Event.prototype.locked = false;
  51. Event.prototype.finished = false;
  52. function Event(_arg) {
  53. this.name = _arg.name;
  54. }
  55. return Event;
  56. })();
  57. EventSystem = (function(_super) {
  58. __extends(EventSystem, _super);
  59. function EventSystem() {
  60. return EventSystem.__super__.constructor.apply(this, arguments);
  61. }
  62. EventSystem.prototype._eventSystemEvents = null;
  63. EventSystem.prototype.event = function(eventName) {
  64. var _base;
  65. this._eventSystemEvents || (this._eventSystemEvents = {});
  66. return (_base = this._eventSystemEvents)[eventName] || (_base[eventName] = new Event(eventName));
  67. };
  68. EventSystem.prototype.lock = function(eventName, next) {
  69. var event,
  70. _this = this;
  71. event = this.event(eventName);
  72. if (event.locked === false) {
  73. event.locked = true;
  74. try {
  75. this.emit(eventName + ':locked');
  76. } catch (err) {
  77. next(err);
  78. return this;
  79. } finally {
  80. next();
  81. }
  82. } else {
  83. this.onceUnlocked(eventName, function(err) {
  84. if (err) {
  85. return next(err);
  86. }
  87. return _this.lock(eventName, next);
  88. });
  89. }
  90. return this;
  91. };
  92. EventSystem.prototype.unlock = function(eventName, next) {
  93. var event;
  94. event = this.event(eventName);
  95. event.locked = false;
  96. try {
  97. this.emit(eventName + ':unlocked');
  98. } catch (err) {
  99. next(err);
  100. return this;
  101. } finally {
  102. next();
  103. }
  104. return this;
  105. };
  106. EventSystem.prototype.start = function(eventName, next) {
  107. var _this = this;
  108. this.lock(eventName, function(err) {
  109. var event;
  110. if (err) {
  111. return next(err);
  112. }
  113. event = _this.event(eventName);
  114. event.finished = false;
  115. try {
  116. return _this.emit(eventName + ':started');
  117. } catch (err) {
  118. next(err);
  119. return _this;
  120. } finally {
  121. next();
  122. }
  123. });
  124. return this;
  125. };
  126. EventSystem.prototype.finish = function() {
  127. var args;
  128. args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
  129. return this.finished.apply(this, args);
  130. };
  131. EventSystem.prototype.finished = function(eventName, next) {
  132. var event,
  133. _this = this;
  134. event = this.event(eventName);
  135. event.finished = true;
  136. this.unlock(eventName, function(err) {
  137. if (err) {
  138. return next(err);
  139. }
  140. try {
  141. return _this.emit(eventName + ':finished');
  142. } catch (err) {
  143. next(err);
  144. return _this;
  145. } finally {
  146. next();
  147. }
  148. });
  149. return this;
  150. };
  151. EventSystem.prototype.onceUnlocked = function(eventName, next) {
  152. var event;
  153. if (debug) {
  154. console.log("onceUnlocked " + eventName);
  155. }
  156. event = this.event(eventName);
  157. if (event.locked) {
  158. this.once(eventName + ':unlocked', next);
  159. } else {
  160. next();
  161. }
  162. return this;
  163. };
  164. EventSystem.prototype.onceFinished = function(eventName, next) {
  165. var event;
  166. event = this.event(eventName);
  167. if (event.finished) {
  168. next();
  169. } else {
  170. this.once(eventName + ':finished', next);
  171. }
  172. return this;
  173. };
  174. EventSystem.prototype.whenFinished = function(eventName, next) {
  175. var event;
  176. event = this.event(eventName);
  177. if (event.finished) {
  178. next();
  179. }
  180. this.on(eventName + ':finished', next);
  181. return this;
  182. };
  183. EventSystem.prototype.when = function() {
  184. var args;
  185. args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
  186. return this.on.apply(this, args);
  187. };
  188. EventSystem.prototype.block = function(eventNames, next) {
  189. var done, err, eventName, total, _i, _len;
  190. if (!balUtilTypes.isArray(eventNames)) {
  191. if (balUtilTypes.isString(eventNames)) {
  192. eventNames = eventNames.split(/[,\s]+/g);
  193. } else {
  194. err = new Error('Unknown eventNames type');
  195. return next(err);
  196. }
  197. }
  198. total = eventNames.length;
  199. done = 0;
  200. for (_i = 0, _len = eventNames.length; _i < _len; _i++) {
  201. eventName = eventNames[_i];
  202. this.lock(eventName, function(err) {
  203. if (err) {
  204. done = total;
  205. return next(err);
  206. }
  207. done++;
  208. if (done === total) {
  209. return next();
  210. }
  211. });
  212. }
  213. return this;
  214. };
  215. EventSystem.prototype.unblock = function(eventNames, next) {
  216. var done, err, eventName, total, _i, _len;
  217. if (!balUtilTypes.isArray(eventNames)) {
  218. if (balUtilTypes.isString(eventNames)) {
  219. eventNames = eventNames.split(/[,\s]+/g);
  220. } else {
  221. err = new Error('Unknown eventNames type');
  222. return next(err);
  223. }
  224. }
  225. total = eventNames.length;
  226. done = 0;
  227. for (_i = 0, _len = eventNames.length; _i < _len; _i++) {
  228. eventName = eventNames[_i];
  229. this.unlock(eventName, function(err) {
  230. if (err) {
  231. done = total;
  232. return next(err);
  233. }
  234. done++;
  235. if (done === total) {
  236. return next();
  237. }
  238. });
  239. }
  240. return this;
  241. };
  242. return EventSystem;
  243. })(EventEmitterEnhanced);
  244. module.exports = {
  245. EventEmitterEnhanced: EventEmitterEnhanced,
  246. Event: Event,
  247. EventSystem: EventSystem
  248. };
  249. }).call(this);