<commit_before>package neash.events;


import neash.events.IEventDispatcher;
import neash.events.Event;
import neash.utils.WeakRef;


class EventDispatcher implements IEventDispatcher
{
   
   /** @private */ private var nmeEventMap:EventMap;
   /** @private */ private var nmeTarget:IEventDispatcher;
   
   
   public function new(?target:IEventDispatcher):Void
   {
      nmeTarget = target == null ? this : target;
      nmeEventMap = null;
   }
   
   
   public function addEventListener(type:String, listener:Function, useCapture:Bool = false, priority:Int = 0, useWeakReference:Bool = false):Void
   {
      if (nmeEventMap == null)
         nmeEventMap = new EventMap();
      var list = nmeEventMap.get(type);
      if (list == null)
      {
         list = new ListenerList();
         nmeEventMap.set(type, list);
      }
      
      var l = new Listener(listener, useCapture, priority);
      list.push(new WeakRef<Listener>(l, useWeakReference));
   }
   
   
   /**
   * Creates and dispatches a typical Event.COMPLETE
   * @private
   */
   public function DispatchCompleteEvent()
   {
      var evt = new Event(Event.COMPLETE);
      dispatchEvent(evt);
   }
   
   
   public function dispatchEvent(event:Event):Bool
   {
      if (nmeEventMap == null)
         return false;
      if (event.target == null)
         event.target = nmeTarget;
      
      if (event.currentTarget == null)
         event.currentTarget = nmeTarget;
      
      var list = nmeEventMap.get(event.type);
      var capture = event.eventPhase == EventPhase.CAPTURING_PHASE;
      
      if (list != null)
      {
         var idx = 0;
         while(idx<list.length)
         {
            var list_item = list[idx];
            var listener = list_item!=null ? list_item.get() : null;
            
            if (listener == null)
            {
               // Lost reference - so we can remove listener. No need to move idx...
               list.splice(idx, 1);
            }
            else
            {
               if (listener.mUseCapture == capture)
               {
                  listener.dispatchEvent(event);
                  if (event.nmeGetIsCancelledNow())
                     return true;
               }
               idx++;
            }
         }
         
         return true;
      }
      
      return false;
   }
   
   
   /**
   * Creates and dispatches a typical IOErrorEvent.IO_ERROR
   * @private
   */
   public function DispatchIOErrorEvent()
   {
      var evt = new IOErrorEvent(IOErrorEvent.IO_ERROR);
      dispatchEvent(evt);
   }
   
   
   public function hasEventListener(type:String):Bool
   {
      if (nmeEventMap == null)
         return false;
      var h = nmeEventMap.get(type);
      if (h != null) {
         for (item in h) {
            if (item != null) return true;
         }
      }
      return false;
   }
   
   
   public function removeEventListener(type:String, listener:Function, capture:Bool = false):Void
   {
      if (nmeEventMap == null)
         return;
      
      if (!nmeEventMap.exists(type)) return;
      
      var list = nmeEventMap.get(type);
      for (i in 0...list.length)
      {
         if (list[i]!=null)
         {
            var li = list[i].get();
            if (li.Is(listener, capture))<commit_msg>Prevent crash when referencing null listeners #fixed NME-116<commit_after>package neash.events;


import neash.events.IEventDispatcher;
import neash.events.Event;
import neash.utils.WeakRef;


class EventDispatcher implements IEventDispatcher
{
   
   /** @private */ private var nmeEventMap:EventMap;
   /** @private */ private var nmeTarget:IEventDispatcher;
   
   
   public function new(?target:IEventDispatcher):Void
   {
      nmeTarget = target == null ? this : target;
      nmeEventMap = null;
   }
   
   
   public function addEventListener(type:String, listener:Function, useCapture:Bool = false, priority:Int = 0, useWeakReference:Bool = false):Void
   {
      if (nmeEventMap == null)
         nmeEventMap = new EventMap();
      var list = nmeEventMap.get(type);
      if (list == null)
      {
         list = new ListenerList();
         nmeEventMap.set(type, list);
      }
      
      var l = new Listener(listener, useCapture, priority);
      list.push(new WeakRef<Listener>(l, useWeakReference));
   }
   
   
   /**
   * Creates and dispatches a typical Event.COMPLETE
   * @private
   */
   public function DispatchCompleteEvent()
   {
      var evt = new Event(Event.COMPLETE);
      dispatchEvent(evt);
   }
   
   
   public function dispatchEvent(event:Event):Bool
   {
      if (nmeEventMap == null)
         return false;
      if (event.target == null)
         event.target = nmeTarget;
      
      if (event.currentTarget == null)
         event.currentTarget = nmeTarget;
      
      var list = nmeEventMap.get(event.type);
      var capture = event.eventPhase == EventPhase.CAPTURING_PHASE;
      
      if (list != null)
      {
         var idx = 0;
         while(idx<list.length)
         {
            var list_item = list[idx];
            var listener = list_item!=null ? list_item.get() : null;
            
            if (listener == null)
            {
               // Lost reference - so we can remove listener. No need to move idx...
               list.splice(idx, 1);
            }
            else
            {
               if (listener.mUseCapture == capture)
               {
                  listener.dispatchEvent(event);
                  if (event.nmeGetIsCancelledNow())
                     return true;
               }
               idx++;
            }
         }
         
         return true;
      }
      
      return false;
   }
   
   
   /**
   * Creates and dispatches a typical IOErrorEvent.IO_ERROR
   * @private
   */
   public function DispatchIOErrorEvent()
   {
      var evt = new IOErrorEvent(IOErrorEvent.IO_ERROR);
      dispatchEvent(evt);
   }
   
   
   public function hasEventListener(type:String):Bool
   {
      if (nmeEventMap == null)
         return false;
      var h = nmeEventMap.get(type);
      if (h != null) {
         for (item in h) {
            if (item != null) return true;
         }
      }
      return false;
   }
   
   
   public function removeEventListener(type:String, listener:Function, capture:Bool = false):Void
   {
      if (nmeEventMap == null)
         return;
      
      if (!nmeEventMap.exists(type)) return;
      
      var list = nmeEventMap.get(type);
      for (i in 0...list.length)
      {
         if (list[i]!=null)
         {
            var li = list[i].get();
            if (li != null && li.Is(listener, capture))
{'fraction_non_alphanumeric': 0.10352588147036759, 'fraction_numerical': 0.0016504126031507878, 'mean_word_length': 1.7466007416563658, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6251575', 'n_tokens_mistral': 1976, 'n_tokens_neox': 1948, 'n_words': 448}