Permalink
Browse files

dispatch() can now send any number of arguments to listeners.

  • Loading branch information...
1 parent 6699b4a commit 2f98c391bbf62d6b630182cb896b52babf68f002 @robertpenner committed Sep 14, 2009
@@ -20,6 +20,6 @@ package org.osflash.signals
* @param eventObject Any object, but an IEvent will take advantage of targeting and bubbling.
* @throws ArgumentError <code>ArgumentError</code>: eventObject is not compatible with eventClass.
*/
- function dispatch(eventObject:Object = null):void;
+ function dispatch(eventObject:Object = null, ...args):void;
}
}
@@ -75,7 +75,7 @@ package org.osflash.signals
}
/** @inheritDoc */
- public function dispatch(eventObject:Object = null):void
+ public function dispatch(eventObject:Object = null, ...args):void
{
var event:Event = Event(eventObject); // will throw TypeError if the cast fails
if (!(event is _eventClass))
@@ -61,8 +61,8 @@ package org.osflash.signals
// Don't add the same listener twice.
if (indexOfListener(listener) >= 0) return;
- // Assume the listeners are already sorted by priority.
- // For listeners with the same priority,
+ // Assume the listeners are already sorted by priority
+ // and insert in the right spot. For listeners with the same priority,
// we must preserve the order in which they were added.
var len:int = listeners.length;
for (var i:int = 0; i < len; i++)
@@ -109,7 +109,7 @@ package org.osflash.signals
}
/** @inheritDoc */
- public function dispatch(eventObject:Object = null):void
+ public function dispatch(eventObject:Object = null, ...args):void
{
if (_eventClass && !(eventObject is _eventClass))
throw new ArgumentError('Event object '+eventObject+' is not an instance of '+_eventClass+'.');
@@ -122,25 +122,35 @@ package org.osflash.signals
event.currentTarget = this.target;
event.signal = this;
}
-
+
//// Send eventObject to each listener.
if (listeners.length)
{
+ if (args.length && eventObject)
+ {
+ args.unshift(eventObject);
+ }
+
+ //TODO: investigate performance of various approaches
// Clone listeners array because add/remove may occur during the dispatch.
- //TODO: test performance of for each vs. for
for each (var listenerBox:Object in listeners.concat())
{
var listener:Function = listenerBox.listener;
//TODO: Maybe put this conditional outside the loop.
- eventObject ? listener(eventObject) : listener();
+ if (!eventObject)
+ listener();
+ else if (args.length)
+ listener.apply(null, args);
+ else
+ listener(eventObject);
}
}
for (var onceListener:Object in onceListeners)
{
remove(onceListener as Function);
}
-
+
if (!event || !event.bubbles) return;
//// Bubble the event as far as possible.
@@ -4,7 +4,7 @@ package org.osflash.signals {
import org.osflash.signals.PriorityListenersTest;
import org.osflash.signals.NativeRelaySignalTest;
import org.osflash.signals.NativeSignalTest;
- import org.osflash.signals.SignalDispatchNoArgsTest;
+ import org.osflash.signals.SignalDispatchExtraArgsTest;
import org.osflash.signals.SignalSplitInterfacesTest;
import org.osflash.signals.SignalWithBubblingEventTest;
import org.osflash.signals.SignalWithCustomEventTest;
@@ -17,6 +17,7 @@ package org.osflash.signals {
addTest(new org.osflash.signals.PriorityListenersTest());
addTest(new org.osflash.signals.NativeRelaySignalTest());
addTest(new org.osflash.signals.NativeSignalTest());
+ addTest(new org.osflash.signals.SignalDispatchExtraArgsTest());
addTest(new org.osflash.signals.SignalDispatchNoArgsTest());
addTest(new org.osflash.signals.SignalSplitInterfacesTest());
addTest(new org.osflash.signals.SignalWithBubblingEventTest());
@@ -0,0 +1,42 @@
+package org.osflash.signals
+{
+ import asunit.framework.TestCase;
+ import org.osflash.signals.ISignal;
+
+ public class SignalDispatchExtraArgsTest extends TestCase
+ {
+ public var completed:ISignal;
+
+ public function SignalDispatchExtraArgsTest(testMethod:String = null)
+ {
+ super(testMethod);
+ }
+
+ protected override function setUp():void
+ {
+ completed = new Signal(this);
+ }
+
+ protected override function tearDown():void
+ {
+ completed.removeAll();
+ completed = null;
+ }
+ //////
+ public function test_dispatch_extra_args_should_call_listener_with_extra_args():void
+ {
+ completed.add( addAsync(onCompleted, 10) );
+ completed.dispatch(22, 'done', new Date());
+ }
+
+ private function onCompleted(...args:Array):void
+ {
+ assertEquals(3, args.length);
+ assertEquals(22, args[0]);
+ assertEquals('done', args[1]);
+ assertTrue(args[2] is Date);
+ }
+ //////
+
+ }
+}

0 comments on commit 2f98c39

Please sign in to comment.