ev-emitter.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /**
  2. * EvEmitter v1.1.0
  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, window */
  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. }( typeof window != 'undefined' ? window : 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. // copy over to avoid interference if .off() in listener
  70. listeners = listeners.slice(0);
  71. args = args || [];
  72. // once stuff
  73. var onceListeners = this._onceEvents && this._onceEvents[ eventName ];
  74. for ( var i=0; i < listeners.length; i++ ) {
  75. var listener = listeners[i]
  76. var isOnce = onceListeners && onceListeners[ listener ];
  77. if ( isOnce ) {
  78. // remove listener
  79. // remove before trigger to prevent recursion
  80. this.off( eventName, listener );
  81. // unset once flag
  82. delete onceListeners[ listener ];
  83. }
  84. // trigger listener
  85. listener.apply( this, args );
  86. }
  87. return this;
  88. };
  89. proto.allOff = function() {
  90. delete this._events;
  91. delete this._onceEvents;
  92. };
  93. return EvEmitter;
  94. }));