Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Promise.isDispatched now exposed as read-only property #72

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 37 additions & 7 deletions src/org/osflash/signals/Promise.as
Original file line number Diff line number Diff line change
@@ -1,20 +1,42 @@
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 _isDispatched:Boolean;
private var valueObjects:Array;


/** Whether to ignore any subsequent calls to <code>dispatch()</code>. By default, subsequent calls will throw an error. */
public var ignoreSubsequentDipatches:Boolean = false;

/**
* Creates a Promise instance to dispatch value objects.
* @param valueClasses Any number of class references that enable type checks in dispatch().
* For example, new Signal(String, uint)
* would allow: signal.dispatch("the Answer", 42)
* but not: signal.dispatch(true, 42.5)
* nor: signal.dispatch()
*
* NOTE: In AS3, subclasses cannot call super.apply(null, valueClasses),
* but this constructor has logic to support super(valueClasses).
*/
public function Promise(...valueClasses)
{
// Cannot use super.apply(null, valueClasses), so allow the subclass to call super(valueClasses).
valueClasses = (valueClasses.length == 1 && valueClasses[0] is Array) ? valueClasses[0]:valueClasses;

super(valueClasses);
}

/** @inheritDoc */
override public function addOnce(listener:Function):ISlot
{
var slot:ISlot = super.addOnce(listener);
if (isDispatched)
if (_isDispatched)
{
slot.execute(valueObjects);
slot.remove();
Expand All @@ -29,16 +51,24 @@ package org.osflash.signals
*/
override public function dispatch(...valueObjects):void
{
if (isDispatched)
if (_isDispatched)
{
throw new IllegalOperationError("You cannot dispatch() a Promise more than once");
if (!ignoreSubsequentDipatches)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just use a OnceSignal?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wasn't aware I was submitting this as a pull request! I'm using promises for their retroactive dispatch functionality, and was sometimes getting runtime errors as promises sometimes got dispatched more than once. This just got around that issue.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

{
throw new IllegalOperationError("You cannot dispatch() a Promise more than once");
}
}
else
{
isDispatched = true;
_isDispatched = true;
this.valueObjects = valueObjects;
super.dispatch.apply(this, valueObjects);
}
}

public function get isDispatched():Boolean
{
return _isDispatched;
}
}
}