microevent.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // Generated by CoffeeScript 1.4.0
  2. (function() {
  3. var MicroEvent, nextTick,
  4. __slice = [].slice;
  5. nextTick = typeof WEB !== "undefined" && WEB !== null ? function(fn) {
  6. return setTimeout(fn, 0);
  7. } : process['nextTick'];
  8. MicroEvent = (function() {
  9. function MicroEvent() {}
  10. MicroEvent.prototype.on = function(event, fct) {
  11. var _base;
  12. this._events || (this._events = {});
  13. (_base = this._events)[event] || (_base[event] = []);
  14. this._events[event].push(fct);
  15. return this;
  16. };
  17. MicroEvent.prototype.removeListener = function(event, fct) {
  18. var i, listeners, _base,
  19. _this = this;
  20. this._events || (this._events = {});
  21. listeners = ((_base = this._events)[event] || (_base[event] = []));
  22. i = 0;
  23. while (i < listeners.length) {
  24. if (listeners[i] === fct) {
  25. listeners[i] = void 0;
  26. }
  27. i++;
  28. }
  29. nextTick(function() {
  30. var x;
  31. return _this._events[event] = (function() {
  32. var _i, _len, _ref, _results;
  33. _ref = this._events[event];
  34. _results = [];
  35. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  36. x = _ref[_i];
  37. if (x) {
  38. _results.push(x);
  39. }
  40. }
  41. return _results;
  42. }).call(_this);
  43. });
  44. return this;
  45. };
  46. MicroEvent.prototype.emit = function() {
  47. var args, event, fn, _i, _len, _ref, _ref1;
  48. event = arguments[0], args = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
  49. if (!((_ref = this._events) != null ? _ref[event] : void 0)) {
  50. return this;
  51. }
  52. _ref1 = this._events[event];
  53. for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
  54. fn = _ref1[_i];
  55. if (fn) {
  56. fn.apply(this, args);
  57. }
  58. }
  59. return this;
  60. };
  61. return MicroEvent;
  62. })();
  63. MicroEvent.mixin = function(obj) {
  64. var proto;
  65. proto = obj.prototype || obj;
  66. proto.on = MicroEvent.prototype.on;
  67. proto.removeListener = MicroEvent.prototype.removeListener;
  68. proto.emit = MicroEvent.prototype.emit;
  69. return obj;
  70. };
  71. if (typeof WEB === "undefined" || WEB === null) {
  72. module.exports = MicroEvent;
  73. }
  74. }).call(this);