PageRenderTime 19ms CodeModel.GetById 11ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/src/org/osflash/signals/natives/sets/NativeSignalSet.as

http://github.com/robertpenner/as3-signals
ActionScript | 111 lines | 48 code | 9 blank | 54 comment | 6 complexity | 48497acb2fdd728827e016abd473b3d0 MD5 | raw file
  1package org.osflash.signals.natives.sets 
  2{
  3	import org.osflash.signals.natives.INativeDispatcher;
  4	import org.osflash.signals.natives.NativeSignal;
  5
  6	import flash.events.Event;
  7	import flash.events.IEventDispatcher;
  8	import flash.utils.Dictionary;
  9
 10	/**
 11	 * A convenient way to access a logical set of signals.
 12	 * 
 13	 * @author Jon Adams
 14	 * 
 15	 * @example SignalSets allow you to get predefined signals for many built in events
 16	 * <listing version="3.0" >
 17		package {
 18			import org.osflash.signals.natives.sets.InteractiveObjectSignalSet;
 19		
 20			import flash.display.Sprite;
 21			import flash.events.Event;
 22		
 23			public class Example extends Sprite {
 24		
 25				private var button:Sprite;
 26				private var buttonSignals:InteractiveObjectSignalSet;
 27		
 28				public function Main() {
 29					button = new Sprite();
 30					button.graphics.beginFill(0xff0000);
 31					button.graphics.drawRect(0, 0, 100, 100);
 32					button.graphics.endFill();
 33					
 34					buttonSignals = new InteractiveObjectSignalSet(button);
 35					buttonSignals.click.add(handler);
 36					buttonSignals.addedToStage.add(handler);
 37					buttonSignals.enterFrame.addOnce(handler);
 38					
 39					addChild(button);
 40				}
 41		
 42				private function handler(event:Event):void {
 43					trace(event.target, "fired", event.type);
 44				}
 45			}
 46		}
 47	 * </listing>
 48	 */
 49	public class NativeSignalSet 
 50	{
 51		protected var target:IEventDispatcher;
 52		
 53		protected const _signals:Dictionary = new Dictionary();
 54
 55		public function NativeSignalSet(target:IEventDispatcher) 
 56		{
 57			this.target = target;
 58		}
 59
 60		/**
 61		 * Lazily instantiates a NativeSignal
 62		 * @throws ArgumentError <code>ArgumentError</code>: eventType must not be null.
 63		 */
 64		public function getNativeSignal(eventType:String, eventClass:Class = null):NativeSignal 
 65		{
 66			if(null == eventType) throw new ArgumentError('eventType must not be null.');
 67			
 68			return _signals[eventType] ||= new NativeSignal(target, eventType, eventClass || Event);
 69		}
 70
 71		/**
 72		 * The current number of listeners for the signal.
 73		 */
 74		public function get numListeners():int 
 75		{
 76			// TODO : This is horrid, it's very expensive to call this if there is a lot of signals.
 77			var count:int = 0;
 78			for each (var signal:INativeDispatcher in _signals) 
 79			{
 80				count += signal.numListeners;
 81			}
 82			return count;
 83		}
 84		
 85		/**
 86		 * The signals in the SignalSet as an Array.
 87		 */
 88		public function get signals():Array 
 89		{
 90			// TODO : This is horrid, it's very expensive to call this if there is a lot of signals.
 91			var result:Array = [];
 92			for each (var signal:INativeDispatcher in _signals) 
 93			{
 94				result[result.length] = signal;
 95			}
 96			return result;
 97		}
 98		
 99		/**
100		 * Unsubscribes all listeners from all signals in the set.
101		 */
102		public function removeAll():void 
103		{
104			for each (var signal:INativeDispatcher in _signals) 
105			{
106				signal.removeAll();
107				delete _signals[signal.eventType];
108			}
109		}
110	}
111}