Skip to content

Relaxed Signals #57

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

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions src/org/osflash/signals/natives/NativeMappedSignal.as
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ package org.osflash.signals.natives
public function NativeMappedSignal(target:IEventDispatcher, eventType:String, eventClass:Class=null, ... mappedTypes)
{
super(target, eventType, eventClass);
// Cannot use super.apply(null, mappedTypes), so allow the subclass to call super(mappedTypes).
mappedTypes = (mappedTypes.length == 1 && mappedTypes[0] is Array) ? mappedTypes[0]:mappedTypes;
valueClasses = mappedTypes;
}

Expand Down
96 changes: 96 additions & 0 deletions src/org/osflash/signals/relaxed/RelaxedDeluxeSignal.as
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package org.osflash.signals.relaxed
{
import org.osflash.signals.DeluxeSignal;
import org.osflash.signals.ISlot;

/**
* Allows the valueClasses to be set in MXML, e.g.
* <signals:Signal id="nameChanged">{[String, uint]}</signals:Signal>
*/
[DefaultProperty("valueClasses")]
/**
* Signal dispatches events to multiple listeners.
* It is inspired by C# events and delegates, and by
* <a target="_top" href="http://en.wikipedia.org/wiki/Signals_and_slots">signals and slots</a>
* in Qt.
* A Signal adds event dispatching functionality through composition and interfaces,
* rather than inheriting from a dispatcher.
* <br/><br/>
* Project home: <a target="_top" href="http://github.com/robertpenner/as3-signals/">http://github.com/robertpenner/as3-signals/</a>
*/
public class RelaxedDeluxeSignal extends DeluxeSignal
{
/**
* Creates a RelaxedDeluxeSignal instance to dispatch events on behalf of a target object.
* @param target The object the signal is dispatching events on behalf of.
* @param valueClasses Any number of class references that enable type checks in dispatch().
* For example, new DeluxeSignal(this, String, uint)
* would allow: signal.dispatch("the Answer", 42)
* but not: signal.dispatch(true, 42.5)
* nor: signal.dispatch()
*
* NOTE: Subclasses cannot call super.apply(null, valueClasses),
* but this constructor has logic to support super(valueClasses).
*/
public function RelaxedDeluxeSignal(target:Object=null, ...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(target, valueClasses);
_stateController = new RelaxedStateController();
}

protected var _stateController : RelaxedStateController;

/**
* @inheritDoc
* @throws ArgumentError <code>ArgumentError</code>: Incorrect number of arguments.
* @throws ArgumentError <code>ArgumentError</code>: Value object is not an instance of the appropriate valueClasses Class.
*/
override public function dispatch(...valueObjects):void
{
_stateController.dispatchedValueObjects = valueObjects;
_stateController.hasBeenDispatched = true;
super.dispatch.apply( this, valueObjects);
}

/**
* @inheritDoc
* @throws flash.errors.IllegalOperationError <code>IllegalOperationError</code>: You cannot addOnce() then add() the same listener without removing the relationship first.
* @throws ArgumentError <code>ArgumentError</code>: Given listener is <code>null</code>.
*/
override public function addOnceWithPriority(listener:Function, priority:int=0):ISlot
{
return _stateController.handleSlot( super.addOnceWithPriority( listener, priority ) );
}

/**
* @inheritDoc
* @throws flash.errors.IllegalOperationError <code>IllegalOperationError</code>: You cannot addOnce() then add() the same listener without removing the relationship first.
* @throws ArgumentError <code>ArgumentError</code>: Given listener is <code>null</code>.
*/
override public function addWithPriority(listener:Function, priority:int=0):ISlot
{
return _stateController.handleSlot( super.addWithPriority( listener, priority ) );
}
/**
* @inheritDoc
* @throws flash.errors.IllegalOperationError <code>IllegalOperationError</code>: You cannot addOnce() then add() the same listener without removing the relationship first.
* @throws ArgumentError <code>ArgumentError</code>: Given listener is <code>null</code>.
*/
override public function addOnce(listener:Function):ISlot
{
return _stateController.handleSlot( super.addOnce( listener ) );
}

/**
* @inheritDoc
* @throws flash.errors.IllegalOperationError <code>IllegalOperationError</code>: You cannot addOnce() then add() the same listener without removing the relationship first.
* @throws ArgumentError <code>ArgumentError</code>: Given listener is <code>null</code>.
*/
override public function add(listener:Function):ISlot
{
return _stateController.handleSlot( super.add( listener ) );
}
}
}
69 changes: 69 additions & 0 deletions src/org/osflash/signals/relaxed/RelaxedMonoSignal.as
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package org.osflash.signals.relaxed
{
import org.osflash.signals.ISlot;
import org.osflash.signals.MonoSignal;

/**
* Allows the valueClasses to be set in MXML, e.g.
* <signals:Signal id="nameChanged">{[String, uint]}</signals:Signal>
*/
[DefaultProperty("valueClasses")]

/**
* A MonoSignal can have only one listener.
*/
public class RelaxedMonoSignal extends MonoSignal
{
/**
* Creates a RelaxedMonoSignal 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: Subclasses cannot call super.apply(null, valueClasses),
* but this constructor has logic to support super(valueClasses).
*/
public function RelaxedMonoSignal(...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);
_stateController = new RelaxedStateController();
}
protected var _stateController : RelaxedStateController;

/**
* @inheritDoc
* @throws flash.errors.IllegalOperationError <code>IllegalOperationError</code>: You cannot add or addOnce with a listener already added, remove the current listener first.
* @throws ArgumentError <code>ArgumentError</code>: Given listener is <code>null</code>.
*/
override public function add(listener:Function):ISlot
{
return _stateController.handleSlot( super.add( listener ) );
}

/**
* @inheritDoc
* @throws flash.errors.IllegalOperationError <code>IllegalOperationError</code>: You cannot add or addOnce with a listener already added, remove the current listener first.
* @throws ArgumentError <code>ArgumentError</code>: Given listener is <code>null</code>.
*/
override public function addOnce(listener:Function):ISlot
{
return _stateController.handleSlot( super.addOnce( listener ) );
}

/**
* @inheritDoc
* @throws ArgumentError <code>ArgumentError</code>: Incorrect number of arguments.
* @throws ArgumentError <code>ArgumentError</code>: Value object is not an instance of the appropriate valueClasses Class.
*/
override public function dispatch(...valueObjects):void
{
_stateController.dispatchedValueObjects = valueObjects;
_stateController.hasBeenDispatched = true;
super.dispatch.apply( this, valueObjects);
}
}
}
72 changes: 72 additions & 0 deletions src/org/osflash/signals/relaxed/RelaxedOnceSignal.as
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package org.osflash.signals.relaxed
{
import org.osflash.signals.ISlot;
import org.osflash.signals.OnceSignal;

/**
* Allows the valueClasses to be set in MXML, e.g.
* <signals:Signal id="nameChanged">{[String, uint]}</signals:Signal>
*/
[DefaultProperty("valueClasses")]

/**
* @author Camille Reynders - [email protected]
*
* Signal dispatches events to multiple listeners.
* It is inspired by C# events and delegates, and by
* <a target="_top" href="http://en.wikipedia.org/wiki/Signals_and_slots">signals and slots</a>
* in Qt.
* A Signal adds event dispatching functionality through composition and interfaces,
* rather than inheriting from a dispatcher.
* <br/><br/>
* Project home: <a target="_top" href="http://github.com/robertpenner/as3-signals/">http://github.com/robertpenner/as3-signals/</a>
*/

public class RelaxedOnceSignal extends OnceSignal
{
/**
* Creates a Signal 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 RelaxedOnceSignal(...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);
_stateController = new RelaxedStateController();
}

protected var _stateController : RelaxedStateController;

/**
* @inheritDoc
* @throws flash.errors.IllegalOperationError <code>IllegalOperationError</code>: You cannot addOnce() then add() the same listener without removing the relationship first.
* @throws ArgumentError <code>ArgumentError</code>: Given listener is <code>null</code>.
*/
override public function addOnce(listener:Function):ISlot
{
return _stateController.handleSlot( super.addOnce( listener ) );
}


/**
* @inheritDoc
* @throws ArgumentError <code>ArgumentError</code>: Incorrect number of arguments.
* @throws ArgumentError <code>ArgumentError</code>: Value object is not an instance of the appropriate valueClasses Class.
*/
override public function dispatch(...valueObjects):void
{
_stateController.dispatchedValueObjects = valueObjects;
_stateController.hasBeenDispatched = true;
super.dispatch.apply( this, valueObjects);
}

}
}
69 changes: 69 additions & 0 deletions src/org/osflash/signals/relaxed/RelaxedPrioritySignal.as
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package org.osflash.signals.relaxed
{
import org.osflash.signals.ISlot;
import org.osflash.signals.PrioritySignal;

public class RelaxedPrioritySignal extends PrioritySignal
{
public function RelaxedPrioritySignal(...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);
_stateController = new RelaxedStateController();
}

protected var _stateController : RelaxedStateController;

/**
* @inheritDoc
* @throws ArgumentError <code>ArgumentError</code>: Incorrect number of arguments.
* @throws ArgumentError <code>ArgumentError</code>: Value object is not an instance of the appropriate valueClasses Class.
*/
override public function dispatch(...valueObjects):void
{
_stateController.dispatchedValueObjects = valueObjects;
_stateController.hasBeenDispatched = true;
super.dispatch.apply( this, valueObjects);
}

/**
* @inheritDoc
* @throws flash.errors.IllegalOperationError <code>IllegalOperationError</code>: You cannot addOnce() then add() the same listener without removing the relationship first.
* @throws ArgumentError <code>ArgumentError</code>: Given listener is <code>null</code>.
*/
override public function addOnceWithPriority(listener:Function, priority:int=0):ISlot
{
return _stateController.handleSlot( super.addOnceWithPriority( listener, priority ) );
}

/**
* @inheritDoc
* @throws flash.errors.IllegalOperationError <code>IllegalOperationError</code>: You cannot addOnce() then add() the same listener without removing the relationship first.
* @throws ArgumentError <code>ArgumentError</code>: Given listener is <code>null</code>.
*/
override public function addWithPriority(listener:Function, priority:int=0):ISlot
{
return _stateController.handleSlot( super.addWithPriority( listener, priority ) );
}
/**
* @inheritDoc
* @throws flash.errors.IllegalOperationError <code>IllegalOperationError</code>: You cannot addOnce() then add() the same listener without removing the relationship first.
* @throws ArgumentError <code>ArgumentError</code>: Given listener is <code>null</code>.
*/
override public function addOnce(listener:Function):ISlot
{
return _stateController.handleSlot( super.addOnce( listener ) );
}

/**
* @inheritDoc
* @throws flash.errors.IllegalOperationError <code>IllegalOperationError</code>: You cannot addOnce() then add() the same listener without removing the relationship first.
* @throws ArgumentError <code>ArgumentError</code>: Given listener is <code>null</code>.
*/
override public function add(listener:Function):ISlot
{
return _stateController.handleSlot( super.add( listener ) );
}
}
}
52 changes: 52 additions & 0 deletions src/org/osflash/signals/relaxed/RelaxedSignal.as
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package org.osflash.signals.relaxed
{
import org.osflash.signals.ISignal;
import org.osflash.signals.ISlot;
import org.osflash.signals.Signal;

public class RelaxedSignal extends Signal implements ISignal
{
public function RelaxedSignal(...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);
_stateController = new RelaxedStateController();
}

protected var _stateController : RelaxedStateController;

/**
* @inheritDoc
* @throws flash.errors.IllegalOperationError <code>IllegalOperationError</code>: You cannot addOnce() then add() the same listener without removing the relationship first.
* @throws ArgumentError <code>ArgumentError</code>: Given listener is <code>null</code>.
*/
override public function add(listener:Function):ISlot
{
return _stateController.handleSlot( super.add( listener ) );
}

/**
* @inheritDoc
* @throws flash.errors.IllegalOperationError <code>IllegalOperationError</code>: You cannot addOnce() then add() the same listener without removing the relationship first.
* @throws ArgumentError <code>ArgumentError</code>: Given listener is <code>null</code>.
*/
override public function addOnce(listener:Function):ISlot
{
return _stateController.handleSlot( super.addOnce( listener ) );
}

/**
* @inheritDoc
* @throws ArgumentError <code>ArgumentError</code>: Incorrect number of arguments.
* @throws ArgumentError <code>ArgumentError</code>: Value object is not an instance of the appropriate valueClasses Class.
*/
override public function dispatch(...valueObjects):void
{
_stateController.dispatchedValueObjects = valueObjects;
_stateController.hasBeenDispatched = true;
super.dispatch.apply( this, valueObjects);
}

}
}
Loading