Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Adds Promise and PromiseTest to the as3-signals library.

  • Loading branch information...
commit 221e50f08164dc431e019766e79b3e12ae713bc7 1 parent fd03bf8
@alecmce alecmce authored
View
44 src/org/osflash/signals/Promise.as
@@ -0,0 +1,44 @@
+package org.osflash.signals
+{
+ import flash.errors.IllegalOperationError;
+
+ import org.osflash.signals.ISlot;
+ import org.osflash.signals.OnceSignal;
+
+ public class Promise extends OnceSignal
+ {
+ private var isDispatched:Boolean;
+ private var valueObjects:Array;
+
+ /** @inheritDoc */
+ override public function addOnce(listener:Function):ISlot
+ {
+ var slot:ISlot = super.addOnce(listener);
+ if (isDispatched)
+ {
+ slot.execute(valueObjects);
+ slot.remove();
+ }
+
+ return slot;
+ }
+
+ /**
+ * @inheritDoc
+ * @throws flash.errors.IllegalOperationError <code>IllegalOperationError</code>: You cannot dispatch() a Promise more than once
+ */
+ override public function dispatch(...valueObjects):void
+ {
+ if (isDispatched)
+ {
+ throw new IllegalOperationError("You cannot dispatch() a Promise more than once");
+ }
+ else
+ {
+ isDispatched = true;
+ this.valueObjects = valueObjects;
+ super.dispatch.apply(this, valueObjects);
+ }
+ }
+ }
+}
View
86 tests/org/osflash/signals/PromiseTest.as
@@ -0,0 +1,86 @@
+package alecmce.util
+{
+ import org.hamcrest.assertThat;
+ import org.hamcrest.object.isTrue;
+ import org.hamcrest.object.sameInstance;
+
+ public class PromiseTest
+ {
+ private var promise:Promise;
+
+ [Before]
+ public function before():void
+ {
+ promise = new Promise();
+ }
+
+ [Test]
+ public function addOncedListenerWillExecuteWhenPromiseIsDispatched():void
+ {
+ var isCalled:Boolean = false;
+ function listener():void
+ {
+ isCalled = true;
+ }
+
+ promise.addOnce(listener);
+ promise.dispatch();
+
+ assertThat(isCalled, isTrue());
+ }
+
+ [Test]
+ public function addOncedListenerWillReceiveDataWhenPromiseIsDispatched():void
+ {
+ var received:Object;
+ function listener(data:Object):void
+ {
+ received = data;
+ }
+
+ var object:Object = {hello:"world"};
+ promise.addOnce(listener);
+ promise.dispatch(object);
+
+ assertThat(received, sameInstance(object));
+ }
+
+ [Test]
+ public function listenerWillExecuteWhenBoundAfterPromiseIsDispatched():void
+ {
+ var isCalled:Boolean = false;
+ function listener():void
+ {
+ isCalled = true;
+ }
+
+ promise.dispatch();
+ promise.addOnce(listener);
+
+ assertThat(isCalled, isTrue());
+ }
+
+ [Test]
+ public function listenerWillReceiveDataWhenBoundAfterPromiseIsDispatched():void
+ {
+ var received:Object;
+ function listener(data:Object):void
+ {
+ received = data;
+ }
+
+ var object:Object = {hello:"world"};
+ promise.dispatch(object);
+ promise.addOnce(listener);
+
+ assertThat(received, sameInstance(object));
+ }
+
+ [Test(expects="flash.errors.IllegalOperationError")]
+ public function promiseWillThrowErrorIfDispatchedTwice():void
+ {
+ promise.dispatch();
+ promise.dispatch();
+ }
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.