ev-emitter.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /**
  2. * EvEmitter v1.0.2
  3. * Lil' event emitter
  4. * MIT License
  5. */
  6. /* jshint unused: true, undef: true, strict: true */
  7. ( function( global, factory ) {
  8. // universal module definition
  9. /* jshint strict: false */ /* globals define, module */
  10. if ( typeof define == 'function' && define.amd ) {
  11. // AMD - RequireJS
  12. define( factory );
  13. } else if ( typeof module == 'object' && module.exports ) {
  14. // CommonJS - Browserify, Webpack
  15. module.exports = factory();
  16. } else {
  17. // Browser globals
  18. global.EvEmitter = factory();
  19. }
  20. }( this, function() {
  21. "use strict";
  22. function EvEmitter() {}
  23. var proto = EvEmitter.prototype;
  24. proto.on = function( eventName, listener ) {
  25. if ( !eventName || !listener ) {
  26. return;
  27. }
  28. // set events hash
  29. var events = this._events = this._events || {};
  30. // set listeners array
  31. var listeners = events[ eventName ] = events[ eventName ] || [];
  32. // only add once
  33. if ( listeners.indexOf( listener ) == -1 ) {
  34. listeners.push( listener );
  35. }
  36. return this;
  37. };
  38. proto.once = function( eventName, listener ) {
  39. if ( !eventName || !listener ) {
  40. return;
  41. }
  42. // add event
  43. this.on( eventName, listener );
  44. // set once flag
  45. // set onceEvents hash
  46. var onceEvents = this._onceEvents = this._onceEvents || {};
  47. // set onceListeners object
  48. var onceListeners = onceEvents[ eventName ] = onceEvents[ eventName ] || {};
  49. // set flag
  50. onceListeners[ listener ] = true;
  51. return this;
  52. };
  53. proto.off = function( eventName, listener ) {
  54. var listeners = this._events && this._events[ eventName ];
  55. if ( !listeners || !listeners.length ) {
  56. return;
  57. }
  58. var index = listeners.indexOf( listener );
  59. if ( index != -1 ) {
  60. listeners.splice( index, 1 );
  61. }
  62. return this;
  63. };
  64. proto.emitEvent = function( eventName, args ) {
  65. var listeners = this._events && this._events[ eventName ];
  66. if ( !listeners || !listeners.length ) {
  67. return;
  68. }
  69. var i = 0;
  70. var listener = listeners[i];
  71. args = args || [];
  72. // once stuff
  73. var onceListeners = this._onceEvents && this._onceEvents[ eventName ];
  74. while ( listener ) {
  75. var isOnce = onceListeners && onceListeners[ listener ];
  76. if ( isOnce ) {
  77. // remove listener
  78. // remove before trigger to prevent recursion
  79. this.off( eventName, listener );
  80. // unset once flag
  81. delete onceListeners[ listener ];
  82. }
  83. // trigger listener
  84. listener.apply( this, args );
  85. // get next listener
  86. i += isOnce ? 0 : 1;
  87. listener = listeners[i];
  88. }
  89. return this;
  90. };
  91. return EvEmitter;
  92. }));