[AS3] Application wide event broadcasting

Sometimes you need the ability to send events “through” the entire application. This can be useful, especially when you have a module-based architecture.

Imagine following situation: Multiple modules, one of them is periodically checking some server state. If this module detects an important change, maybe the user has logged out on the server side, or whatever, the other modules need to know about that change. So we need a component that broadcasts this change and anybody who is interested in that change listens for it. The change is broadcasted via Flash-Events and for the broadcasting itself, there is already the EventDispatcher. So the idea is to build a class that is simply holding a private static instance of an EventDispatcher and provides a dispatchEvent and addEventListener function, just like the EventDispatcher itself does.

package com.firstrowria
{
    import flash.events.Event;
    import flash.events.EventDispatcher;

    public class GlobalEventBroadcaster
    {

        private static var dispatcher:EventDispatcher = new EventDispatcher();

        public static function addEventListener(type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false):void
        {
            dispatcher.addEventListener(type, listener, useCapture, priority, useWeakReference);
        }

        public static function removeEventListener(type:String, listener:Function, useCapture:Boolean = false):void
        {
            if (dispatcher)
                dispatcher.removeEventListener(type, listener, useCapture);
        }

        public static function dispatchEvent(event:Event):void
        {
            if (dispatcher)
                dispatcher.dispatchEvent(event);
        }

    }
}

That’s it, dispatch and listen to your own Events as you need it. Since this is a very simple approach there is a lot of space for improvements. Just a few ideas:

  • Channel support. Could be a huge improvement when you have a lot of listeners already
  • Store the current listeners in an array so the removement of all listeners at once for a specific event is possible
  • Check if there are listeners for a specific event, if not, do not dispatch the Event since this is ridiculous slow and totally useless
  • Remove the possibility for weak references in the addEventListener function. More info on this topic? See Joe Berkovitz’s blog entry why weak references can be dangerous

[AS3] Introducing the OddFormatter class

If you need a formatter that supports the European, British and even American odd format, have a look at the following piece of code. If you have no idea about the different formats you might check the Odd Article on Wikipedia first.

Please note that for the UK format, the odd is always converted to the lowest possible term. Wikipedia says that 3/2 is usually represented as 6/4, 10/3 is 100/30, but I’m pretty sure that this is not commonly used, at least for the online bookmakers where the odds are automatically converted in the different formats.

You can also download the source here.

As usual, this code is free, use it wherever you want ;)

package com.firstrowria.formatters
{
   public class OddFormatter
   {
      public static const EU_FORMAT:uint = 0;
      public static const US_FORMAT:uint = 1;
      public static const UK_FORMAT:uint = 2;

      public static function format(v:Number, f:uint = 0, c:String = “,”, p:int = 2):String
      {
         var formattedValue:String = “”;

         if (f == UK_FORMAT)
         {
            var hundretValue:Number = Math.round(v * 100) – 100;
            var ggt:int = ggt(hundretValue, 100);
            formattedValue = (hundretValue / ggt) + “/” + (100 / ggt);
         }
         else if (f == US_FORMAT)
         {
            if (v >= 2)
               formattedValue = “+” + Math.round(100 * (v – 1));
            else if (v == 1)
               formattedValue = “-0″;
            else
               formattedValue = “” + Math.round(100 / (1 – v));
         }
         else if (f == EU_FORMAT)
         {
            formattedValue = v.toFixed(p)
            if (c != “.”)
               formattedValue = formattedValue.replace(”.”, c);
         }

         return formattedValue;
      }

      private static function ggt(u:int, v:int):int
      {
         return ((u > 0) ? ggt(v % u, u) : v);
      }
   }
}