Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Generality
The "signal engine" include in the package system.signals is a very easy ActionScript 3
messaging tools.
With the Signaler interface we can define objects who communicates by signals and can be use
to create a light-weight implementation of the Observer pattern. A signal emit simple values with
its own array of receivers (slots). This values can be strongly-typed with an internal checking
process.
Receivers can be defines with a simple function reference or a custom object who implements
the interface system.signals.Receiver .
Receivers subscribe to real objects, not to string-based channels. Event string constants are no
longer needed like W3C DOM 2/3 event model.
Interfaces
The system.signals package contains two interfaces : Signaler and Receiver.
The Signaler interface is simple but contains all important methods to deploy your signals.
package system.signals
/**
*/
/**
*/
/**
*/
function connected():Boolean ;
/**
*/
/**
*/
/**
*/
function hasReceiver( receiver:* ):Boolean ;
The Receiver interface is optional and can be use to defines a basic slot connected with Signaler
objects with the connect() method (only the receive method is connected with the signal not the
Receiver reference).
package system.signals
{
/**
* The Receiver defines a simple method for receiving values from Signaler.
*/
public interface Receiver
{
/**
* This method is called when the receiver is connected with a Signal
object.
* @param ...values All the values emitting by the signals connected with
this object.
*/
function receive( ...values:Array ):void ;
}
}
Hello World
We begin with a first basic example. In this example we use the system.signals.Signal, this
class is a basic but full implementation of the Signaler interface. This script can be compiled as
an application with mxmlc or as a document class in Flash Professional or all ActionScript IDE
(FDT, etc.)
package examples
{
import system.signals.Signal;
import flash.display.Sprite;
signal.connect( write ) ;
Features
Signals have many features.
Restrict the types and the number of arguments passed-in the emit
method.
A signal can be initialized with specific value classes that will validate value objects on dispatch
(optional).
The 'types' property is an optional read-write Array attribute who can register Class references to
defines a validation when the signal emit. If this property is null the signal not check the emit
parameters.
If the 'types' property is null the signal not check the types and the number of the passed-in
arguments when the emit method is invoked.
Basic example to restrict the emit method with 3 arguments with the types String , Number and
uint.
import system.signals.Signal ;
var signal:Signal ;
signal.connect( slot ) ;
try
{
signal.emit( "hello" , 2.5 , 4.5 ) ;
}
catch( e:Error )
{
trace( e.message ) ;
// The parameter with the index 2 in the emit method isn't valid,
// must be an instance of the [class uint] class but is an instance of the
Number class.
}
try
{
signal.emit( "hello" ) ;
}
catch( e:Error )
{
trace( e.message ) ;
// The number of arguments in the emit method is not valid,
// must be invoked with 3 arguments and you call it with 1 argument.
}
try
{
signal.emit() ;
}
catch( e:Error )
{
trace( e.message ) ;
// The number of arguments in the emit method is not valid,
// must be invoked with 3 arguments and you call it with 0 argument.
}
You can disconnect the receivers registered in a Signaler object with the method disconnect().
This method can target a specific receiver (function reference or Receiver instance) but you can
disconnect all registered receivers if you don't passed-in value.
Example :
import system.signals.Signal ;
signal.connect( slot1 ) ;
signal.connect( slot2 ) ;
signal.connect( slot3 ) ;
trace( signal.length ) ; // 3
signal.disconnect( slot2 ) ;
trace( signal.length ) ; // 2
trace( signal.length ) ; // 0
Note :
• You can retrieve the number of receivers with the read-only attribute : signal.length
• You can retrieve if the signal contains one or more receivers with the method :
signal.connected():Boolean
• You can retrieve if a specific receiver is connected with the method :
signal.hasReceiver( receiver:* ):Boolean
Receivers can be added for a one-time call and removed automatically on dispatch
import system.signals.Signal ;
Only the first message is dispatched, the reveiver is automatically disconnected and can't receive
the next messages.
Receiver priority
When you connect a receiver with the connect method you can apply a priority over your
reference.
The priority is designated by an uint value( an integer > 0 ). The higher the number, the higher
the priority. All receivers with priority n are processed before receivers of priority n-1. If two or
more receivers share the same priority, they are processed in the order in which they were added.
The default priority is 0.
import system.signals.Signal ;
signal.connect( slot1 , 10 ) ;
signal.connect( slot2 , 999 ) ;
signal.connect( slot3 , 400 ) ;
package examples.signals
{
import system.signals.Signal;
import system.signals.Signaler;
import flash.display.Sprite;
import flash.events.MouseEvent;
package examples
{
import examples.signals.SignalButton;
import flash.display.Sprite;
button.x = 25 ;
button.y = 24 ;
button.connect( notify ) ;
addChild( button ) ;
}
First we defines a little class Messenger who format a specific receiving message with a name and
a string pattern.
package examples.signals
{
import core.strings.fastformat;
import system.console;
import system.signals.Receiver;
package examples
{
import examples.signals.Messenger;
import system.console;
import system.diagnostics.TextFieldConsole;
import system.signals.Signal;
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.text.TextField;
import flash.text.TextFormat;
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.addEventListener( Event.RESIZE , resize ) ;
// console
addChild( textfield ) ;
resize() ;
// receiver
// signaler
signal.connect( messenger ) ;