123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- /**
- * EvEmitter v1.0.3
- * Lil' event emitter
- * MIT License
- */
- /* jshint unused: true, undef: true, strict: true */
- ( function( global, factory ) {
- // universal module definition
- /* jshint strict: false */ /* globals define, module, window */
- if ( typeof define == 'function' && define.amd ) {
- // AMD - RequireJS
- define( factory );
- } else if ( typeof module == 'object' && module.exports ) {
- // CommonJS - Browserify, Webpack
- module.exports = factory();
- } else {
- // Browser globals
- global.EvEmitter = factory();
- }
- }( typeof window != 'undefined' ? window : this, function() {
- "use strict";
- function EvEmitter() {}
- var proto = EvEmitter.prototype;
- proto.on = function( eventName, listener ) {
- if ( !eventName || !listener ) {
- return;
- }
- // set events hash
- var events = this._events = this._events || {};
- // set listeners array
- var listeners = events[ eventName ] = events[ eventName ] || [];
- // only add once
- if ( listeners.indexOf( listener ) == -1 ) {
- listeners.push( listener );
- }
- return this;
- };
- proto.once = function( eventName, listener ) {
- if ( !eventName || !listener ) {
- return;
- }
- // add event
- this.on( eventName, listener );
- // set once flag
- // set onceEvents hash
- var onceEvents = this._onceEvents = this._onceEvents || {};
- // set onceListeners object
- var onceListeners = onceEvents[ eventName ] = onceEvents[ eventName ] || {};
- // set flag
- onceListeners[ listener ] = true;
- return this;
- };
- proto.off = function( eventName, listener ) {
- var listeners = this._events && this._events[ eventName ];
- if ( !listeners || !listeners.length ) {
- return;
- }
- var index = listeners.indexOf( listener );
- if ( index != -1 ) {
- listeners.splice( index, 1 );
- }
- return this;
- };
- proto.emitEvent = function( eventName, args ) {
- var listeners = this._events && this._events[ eventName ];
- if ( !listeners || !listeners.length ) {
- return;
- }
- var i = 0;
- var listener = listeners[i];
- args = args || [];
- // once stuff
- var onceListeners = this._onceEvents && this._onceEvents[ eventName ];
- while ( listener ) {
- var isOnce = onceListeners && onceListeners[ listener ];
- if ( isOnce ) {
- // remove listener
- // remove before trigger to prevent recursion
- this.off( eventName, listener );
- // unset once flag
- delete onceListeners[ listener ];
- }
- // trigger listener
- listener.apply( this, args );
- // get next listener
- i += isOnce ? 0 : 1;
- listener = listeners[i];
- }
- return this;
- };
- return EvEmitter;
- }));
|