View unanswered posts | View active topics


Reply to topic  [ 7 posts ] 
Signal-slot library for AS3. 
Author Message
Member

Joined: Sun May 11, 2008 3:01 pm
Posts: 860
Post Signal-slot library for AS3.
This tiny library is a result of many optimizations I've attempted with Flash built-in events system. Finally, I decided to abandon all attempts of hacking the built-ins and designed something very simple to replace it.
Here are the main differences:
  • When you invoke a handler (i.e. slot) no additional objects are created
  • All event types are known beforehand, objects cannot dispatch unforeseen events.
  • You can specify the types and number of the arguments the handler (slot) should take.
  • target, currentTarget, stopPropagation, handled are not implemented, it is for the programmer, to extend and add that functionality.
  • There is nothing like useCapture or bubbling in there just because it doesn't deal with display list.

If you need any suggestions as to where this might be useful, and where this won't be useful for certain:
It would be unwise to try and replace the native events, but if you're adding many custom events, then it might be worth looking in here.
Small projects, where event generation ind dispatching will not likely affect the productivity wouldn't be probably the best target either. The main goal of this library is to save on "magic strings" which are event's metadata and event's constants usually used with built-in events, however, if there aren't to many of those, then it is unlikely that you will see any benefits of using signals.

Here's the usage example:
Code:
package signals
{
   //{ imports
   import flash.display.Sprite;
   import org.wvxvws.signals.ISemaphore;
   import org.wvxvws.signals.SignalError;
   import org.wvxvws.signals.Signals;
   //}
   
   /**
    * TestAsteriscNamespace class.
    * @author wvxvw
    * @langVersion 3.0
    * @playerVersion 10.0.32
    */
   public class SignalsExample extends Sprite implements ISemaphore
   {
      //--------------------------------------------------------------------------
      //
      //  Public properties
      //
      //--------------------------------------------------------------------------
      
      public static const FOO:Vector.<Class> = new <Class>[int];
      public static const BAR:Vector.<Class> = new <Class>[String, int];
      
      /* INTERFACE org.wvxvws.signals.ISemaphore */
      
      public function get signals():Signals { return this._signals; }
      
      //--------------------------------------------------------------------------
      //
      //  Protected properties
      //
      //--------------------------------------------------------------------------
      
      protected var _signals:Signals;
      
      //--------------------------------------------------------------------------
      //
      //  Constructor
      //
      //--------------------------------------------------------------------------
      
      public function SignalsExample()
      {
         super();
         this._signals = new Signals(this);
         this._signals.add(FOO, this.slotTest3);
         this._signals.add(BAR, this.slotTest);
         this._signals.add(BAR, this.slotTest2);
         this._signals.call(BAR, "Foo", 100);
         this._signals.call(FOO, 200);
         try
         {
            this._signals.call(FOO, "Foo", 100);
         }
         catch (error:SignalError)
         {
            // Attempting to call slot with wrong signature.
            trace(error.message);
         }
      }
      
      //--------------------------------------------------------------------------
      //
      //  Public methods
      //
      //--------------------------------------------------------------------------
      
      /* INTERFACE org.wvxvws.signals.ISemaphore */
      
      public function signalTypes():Vector.<Vector.<Class>>
      {
         return new <Vector.<Class>>[FOO, BAR];
      }
      
      //--------------------------------------------------------------------------
      //
      //  Private methods
      //
      //--------------------------------------------------------------------------
      
      private function slotTest(par0:String, par1:int):void
      {
         trace("slotTest called", par0, par1);
      }
      
      private function slotTest2(par0:String, par1:int):void
      {
         trace("slotTest2 called", par0, par1);
      }
      
      private function slotTest3(par1:int):void
      {
         trace("slotTest3 called", par1);
      }
   }
}


The library is here:
http://code.google.com/p/e4xu/source/br ... ws/signals
It isn't yet available as SWC, but I'll make one once I'm 100% sure on the interfaces (so, there are potential changes on the way).
Looking forward to hear your feedback! Enjoy :)

EDIT: Slot signature is verified when called.
EDIT: Removed SignalType class, made it a little bit smaller.
EDIT: Some performance optimization added (but can optimize more!).

_________________
http://www.couchsurfing.com/people/wvxvw


Last edited by wvxvw on Sat Feb 13, 2010 8:41 pm, edited 5 times in total.



Sun Feb 07, 2010 10:24 am
Profile
Member

Joined: Wed Apr 19, 2006 12:39 pm
Posts: 245
Post Re: Signal-slot library for AS3.
is there a reason for not using http://github.com/robertpenner/as3-signals


Sun Feb 07, 2010 1:04 pm
Profile
Member

Joined: Sun May 11, 2008 3:01 pm
Posts: 860
Post Re: Signal-slot library for AS3.
If the question is meant to ask why not use this particular implementation, then me say "no objections" :) Whatever suites you better!
If you ask about particular differences between the two, then there are:
well, they are really different :) My variant lets you pass arguments to slots it also lets you manage priority and define them as weak / strong references. Well, and it's just smaller. But the one who would use my variant would need to write more code, ummm... unless they will reuse it etc... But, really these two are just different approaches to the same problem, neither cancels the other, but I still like my better :)
Ah, yet one more difference - my is targeting Flash Player 10.

_________________
http://www.couchsurfing.com/people/wvxvw


Sun Feb 07, 2010 1:32 pm
Profile
Member

Joined: Thu May 15, 2008 7:36 pm
Posts: 200
Location: Baltimore, MD
Post Re: Signal-slot library for AS3.
wvxvw: Awesome, thanks for sharing this. I'll have to look deeper at it as I've just did a one-off to raise "signals" for a timer class (ripped out native event listening) for the same reason: better performance.

xMCNUGGETx: Thanks for the link too; I wasn't aware of that implementation.

_________________
UI Lead / Game Designer - Firaxis
President - Geek House Games
Baltimore, MD


Tue Feb 09, 2010 11:58 am
Profile E-mail WWW
Member

Joined: Thu May 15, 2008 7:36 pm
Posts: 200
Location: Baltimore, MD
Post Re: Signal-slot library for AS3.
Follow up, dang wvxvw you have a tremendous amount of code on Google Code for the community! I knew you were putting stuff up, but when the heck do you find time to sleep? 8)

_________________
UI Lead / Game Designer - Firaxis
President - Geek House Games
Baltimore, MD


Tue Feb 09, 2010 12:01 pm
Profile E-mail WWW
Member

Joined: Sun May 11, 2008 3:01 pm
Posts: 860
Post Re: Signal-slot library for AS3.
Haha, well, I think I often use googlecode instead of the pastebin :) Even though it might seem like a lot, there are few really useful things there... I'd have to have way much more hours in a day to get it all into some presentable shape. Which makes me wonder how other people find the time to document their code for example :)

BTW. I've realized I've done something wrong... I've updated the code.The wrong part was that I somehow assumed I can check the method signature, but in fact I was checking something else :(
Fixed this by placing the verification in the call() method.

_________________
http://www.couchsurfing.com/people/wvxvw


Wed Feb 10, 2010 12:20 am
Profile
Member

Joined: Sun May 11, 2008 3:01 pm
Posts: 860
Post Re: Signal-slot library for AS3.
BTW: http://n2.nabble.com/HSL-Connection-Con ... 14720.html
Here's an interesting discussion IMO related to the subject.

_________________
http://www.couchsurfing.com/people/wvxvw


Fri Feb 12, 2010 3:47 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 7 posts ] 

Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.