Package | flash.system |
Class | public final class MessageChannel |
Inheritance | MessageChannel EventDispatcher Object |
Language Version: | ActionScript 3.0 |
Runtime Versions: | Flash Player 11.4, AIR 3.4 |
Each MessageChannel object contains a queue of message objects sent
from the sending worker to the receiving worker. Each call to
send()
adds an object to the queue. Each call to
receive()
retrieves the oldest message object from the queue.
You do not create MessageChannel instances directly by calling the
MessageChannel()
constructor. To create a MessageChannel
instance, call the createMessageChannel()
method of the
Worker object that will send messages on the channel, passing the
receiving Worker object as an argument.
The typical workflow for sending messages with a MessageChannel object is as follows:
-
Call the sending worker's
createMessageChannel()
method to create the message channel// In the sending worker swf var sendChannel:MessageChannel; sendChannel = Worker.current.createMessageChannel(receivingWorker);
-
Pass the message channel to the other worker, either by calling
Worker.setSharedProperty()
or by sending it through an existing message channelreceivingWorker.setSharedProperty("incomingChannel", sendChannel);
-
Code in the receiving worker registers a listener with the MessageChannel object for the
channelMessage
event// In the receiving worker swf var incomingChannel:MessageChannel; incomingChannel = Worker.current.getSharedProperty("incomingChannel"); incomingChannel.addEventListener(Event.CHANNEL_MESSAGE, handleIncomingMessage);
-
Code in the sending worker sends a message by calling the
send()
method// In the sending worker swf sendChannel.send("This is a message");
-
The runtime calls the event handler in the receiving worker code, indicating that a message has been sent
// In the receiving worker swf // This method is called when the message channel gets a message private function handleIncomingMessage(event:Event):void { // Do something with the message, as shown in the next code listing }
-
Code in the receiving worker calls the
receive()
method to get the message. The object returned by thereceive()
method has the same data type as the object passed to thesend()
method.var message:String = incomingChannel.receive() as String;
In addition to the asynchronous workflow outlined above, you can use
an alternative workflow with the receive()
method to pause
the code in the receiving worker and wait until a message is sent. See
the receive()
method description for more information.
The MessageChannel class is one of the special object types that are shared
between workers rather than copied between them. When you pass a message channel
from one worker to another worker either by calling the Worker object's
setSharedProperty()
method or by using a MessageChannel object,
both workers have a reference to the same MessageChannel object in the runtime's memory.
Related API Elements
Property | Defined By | ||
---|---|---|---|
constructor : Object
A reference to the class object or constructor function for a given object instance. | Object | ||
messageAvailable : Boolean [read-only]
Indicates whether the MessageChannel has one or more messages from
the sending worker in its internal message queue. | MessageChannel | ||
state : String [read-only]
Indicates the current state of the MessageChannel object (open,
closing, or closed). | MessageChannel |
Method | Defined By | ||
---|---|---|---|
addEventListener(type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false):void [override]
Registers an event listener object with an EventDispatcher object so that the listener
receives notification of an event. | MessageChannel | ||
Instructs the current MessageChannel to close once all messages have
been received. | MessageChannel | ||
Dispatches an event into the event flow. | EventDispatcher | ||
Checks whether the EventDispatcher object has any listeners registered for a specific type
of event. | EventDispatcher | ||
Indicates whether an object has a specified property defined. | Object | ||
Indicates whether an instance of the Object class is in the prototype chain of the object specified
as the parameter. | Object | ||
Indicates whether the specified property exists and is enumerable. | Object | ||
Retrieves a single message object from the queue of messages sent
through this message channel. | MessageChannel | ||
[override]
Removes a listener from the EventDispatcher object. | MessageChannel | ||
Sends an object from the sending worker, adding it to the message
queue for the receiving worker. | MessageChannel | ||
Sets the availability of a dynamic property for loop operations. | Object | ||
Returns the string representation of this object, formatted according to locale-specific conventions. | Object | ||
[override]
Returns the string representation of the specified object. | MessageChannel | ||
Returns the primitive value of the specified object. | Object | ||
Checks whether an event listener is registered with this EventDispatcher object or any of
its ancestors for the specified event type. | EventDispatcher |
Event | Summary | Defined By | ||
---|---|---|---|---|
[broadcast event] Dispatched when the Flash Player or AIR application gains operating system focus and becomes active. | EventDispatcher | |||
Dispatched each time the sending worker calls this MessageChannel object's send() method, indicating that a new message object is available in the MessageChannel instance's queue. | MessageChannel | |||
Dispatched when the value of the message channel's state property changes. | MessageChannel | |||
[broadcast event] Dispatched when the Flash Player or AIR application operating loses system focus and is becoming inactive. | EventDispatcher |
messageAvailable | property |
state | property |
state:String
[read-only] Language Version: | ActionScript 3.0 |
Runtime Versions: | Flash Player 11.4, AIR 3.4 |
Indicates the current state of the MessageChannel object (open, closing, or closed). The possible values for this property are defined as constants in the MessageChannelState class.
Implementation
public function get state():String
Related API Elements
addEventListener | () | method |
override public function addEventListener(type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false):void
Language Version: | ActionScript 3.0 |
Runtime Versions: | Flash Player 11.4, AIR 3.4 |
Registers an event listener object with an EventDispatcher object so that the listener receives notification of an event. You can register event listeners on all nodes in the display list for a specific type of event, phase, and priority.
After you successfully register an event listener, you cannot change its priority
through additional calls to addEventListener()
. To change a listener's
priority, you must first call removeListener()
. Then you can register the
listener again with the new priority level.
Keep in mind that after the listener is registered, subsequent calls to
addEventListener()
with a different type
or
useCapture
value result in the creation of a separate listener registration.
For example, if you first register a listener with useCapture
set to
true
, it listens only during the capture phase. If you call
addEventListener()
again using the same listener object, but with
useCapture
set to false
, you have two separate listeners: one
that listens during the capture phase and another that listens during the target and
bubbling phases.
You cannot register an event listener for only the target phase or the bubbling phase. Those phases are coupled during registration because bubbling applies only to the ancestors of the target node.
If you no longer need an event listener, remove it by calling
removeEventListener()
, or memory problems could result. Event listeners are not automatically
removed from memory because the garbage
collector does not remove the listener as long as the dispatching object exists (unless the useWeakReference
parameter is set to true
).
Copying an EventDispatcher instance does not copy the event listeners attached to it. (If your newly created node needs an event listener, you must attach the listener after creating the node.) However, if you move an EventDispatcher instance, the event listeners attached to it move along with it.
If the event listener is being registered on a node while an event is being processed on this node, the event listener is not triggered during the current phase but can be triggered during a later phase in the event flow, such as the bubbling phase.
If an event listener is removed from a node while an event is being processed on the node, it is still triggered by the current actions. After it is removed, the event listener is never invoked again (unless registered again for future processing).
Parameters
type:String — The type of event.
| |
listener:Function — The listener function that processes the event. This function must accept
an Event object as its only parameter and must return nothing, as this example shows:
function(evt:Event):void The function can have any name. | |
useCapture:Boolean (default = false ) —
Determines whether the listener works in the capture phase or the
target and bubbling phases. If useCapture is set to true ,
the listener processes the event only during the capture phase and not in the
target or bubbling phase. If useCapture is false , the
listener processes the event only during the target or bubbling phase. To listen for
the event in all three phases, call addEventListener twice, once with
useCapture set to true , then again with
useCapture set to false .
| |
priority:int (default = 0 ) — The priority level of the event listener. The priority is designated by
a signed 32-bit integer. The higher the number, the higher the priority. All listeners
with priority n are processed before listeners of priority n-1. If two
or more listeners share the same priority, they are processed in the order in which they
were added. The default priority is 0.
| |
useWeakReference:Boolean (default = false ) — Determines whether the reference to the listener is strong or
weak. A strong reference (the default) prevents your listener from being garbage-collected.
A weak reference does not. Class-level member functions are not subject to garbage
collection, so you can set |
close | () | method |
public function close():void
Language Version: | ActionScript 3.0 |
Runtime Versions: | Flash Player 11.4, AIR 3.4 |
Instructs the current MessageChannel to close once all messages have been received.
Once you call this method, you can no longer call the
send()
method to add messages to the queue. The
send()
call will fail and return false
.
You can also only call the receive()
method to
receive messages that are already waiting in the queue. If the queue
is empty, the receive()
call will return null
.
Events
channelState: — dispatched when the
close() method is called (which sets the
state property is to MessageChannelState.CLOSING ).
Dispatched again when all the messages have been received and
the state property is set to
MessageChannelState.CLOSED .
|
receive | () | method |
public function receive(blockUntilReceived:Boolean = false):*
Language Version: | ActionScript 3.0 |
Runtime Versions: | Flash Player 11.4, AIR 3.4 |
Retrieves a single message object from the queue of messages sent through this message channel.
Each time the sending worker's code calls the MessageChannel
object's send()
method, a single object is added to the
message channel's internal message queue. These objects stack up in
the queue until they are removed one at a time by the receiving
worker calling the receive()
method. The message objects
are received in the order they are sent.
To check if the queue contains a message object to receive, use
the messageAvailable
property.
In the standard case, the object passed into send()
is serialized in AMF3 format. When it is removed from the queue by
the receive()
call, it is deserialized into an
ActionScript object (a copy of the original object) in the receiving
worker and the worker receives a reference to that copy. Certain
types of objects are shared between workers rather than copied. In
that case the object that the receiving worker gets is a reference
to the shared object itself rather than a new copy of the object.
For more information about this case see the send()
method description.
The method's behavior changes if the message queue is empty and
you pass true
for the blockUntilReceived
parameter. In that case the worker pauses its execution thread at
the receive()
call and does not execute more code. Once
the sending worker calls send()
, the
receive()
call completes by receiving the message. The
worker then resumes code execution at the next line of code following
the receive call.
Parameters
blockUntilReceived:Boolean (default = false ) — indicates whether the worker's execution
thread should receive a message object and then continue
execution (false ), or if it should pause at the
receive() call and wait for a message to be sent
if the queue is empty (true )
|
* — a copy of the object passed into the send()
method by the sending worker. If the object is one of the
special types that are shared between workers, the return
value is a reference to the shared object rather than to a
copy of it. If no message is available on the queue, the
method returns null .
|
Throws
IOError — if the channel is closed at the time the
method is called or if the blockUntilReceived argument
results in the execution being paused and the channel is then
closed by another worker.
| |
ArgumentError — if the calling code is not in the receiving worker
| |
ScriptTimeoutError — if the method is called from
code in the primordial worker in Flash Player and the
blockUntilReceived argument causes the worker to pause
longer than the script timeout limit (15 seconds by default)
|
Related API Elements
removeEventListener | () | method |
override public function removeEventListener(type:String, listener:Function, useCapture:Boolean = false):void
Language Version: | ActionScript 3.0 |
Runtime Versions: | Flash Player 11.4, AIR 3.4 |
Removes a listener from the EventDispatcher object. If there is no matching listener registered with the EventDispatcher object, a call to this method has no effect.
Parameters
type:String — The type of event.
| |
listener:Function — The listener object to remove.
| |
useCapture:Boolean (default = false ) —
Specifies whether the listener was registered for the capture phase or the
target and bubbling phases. If the listener was registered for both the capture phase and the
target and bubbling phases, two calls to removeEventListener() are required
to remove both, one call with useCapture() set to true , and another
call with useCapture() set to false .
|
send | () | method |
public function send(arg:*, queueLimit:int = -1):void
Language Version: | ActionScript 3.0 |
Runtime Versions: | Flash Player 11.4, AIR 3.4 |
Sends an object from the sending worker, adding it to the message queue for the receiving worker.
The object passed to the arg
parameter can be almost any
object. Other than the exceptions noted below, any object that is passed
to the arg
parameter is not passed by reference. Any changes made to the
object in one worker after send()
is called are
not carried over to the other worker. The object is copied by
serializing it to AMF3 format and deserializing it into a new object
in the receiving worker when receive()
is called.
For this reason, any object that can't be serialized in AMF3 format,
including display objects, can't be passed to the arg
parameter.
In order for a custom class to be passed properly, the class definition
must be registered using the flash.net.registerClassAlias()
function or [RemoteClass]
metadata. With either technique
the same alias must be used for both worker's versions of the class.
There are five types of objects that are an exception to the rule that objects aren't shared between workers:
- Worker
- MessageChannel
- shareable ByteArray (a ByteArray object with its
shareable
property set totrue
- Mutex
- Condition
If you pass an instance of these objects to the arg
parameter, each worker has a reference to the same underlying object.
Changes made to an instance in one worker are immediately available
in other workers. In addition, if you pass the same instance of these
objects more than once using send()
, the runtime doesn't
create a new copy of the object in the receiving worker. Instead, the
same reference is re-used, reducing system memory use.
By default, this method adds the object to the queue and immediately
returns, continuing execution with the next line of code. If you want
to prevent the queue from growing beyond a certain size, you can use
the queueLimit
parameter to specify the maximum number
of items to allow in the queue. If at the time you call
send()
the number of items in the queue is greater than
the limit you specify, the worker pauses the execution thread at the
send()
call. Once the receiving worker calls
receive()
enough times that the queue size is less than
the specified queue limit, the send() call completes. The worker then
continues execution at the next line of code.
Parameters
arg:* — the object to add to the message queue
| |
queueLimit:int (default = -1 ) — the maximum number of message objects that the
message queue can contain. If the queue contains more objects
than the limit, the sending worker pauses execution until
messages are received and the queue size drops below the limit.
|
Events
channelMessage: — dispatched to notify the
receiving worker that a message object is available in the queue
|
Throws
IOError — if the channel is closed at the time the
method is called or if the queueLimit argument
results in the execution being paused and the channel is then
closed by another worker.
| |
ArgumentError — if the calling code is not in the sending worker
| |
ScriptTimeoutError — if the method is called from
code in the primordial worker in Flash Player and the
queueLimit argument causes the worker to pause
longer than the script timeout limit (15 seconds by default)
|
Related API Elements
toString | () | method |
override public function toString():String
Language Version: | ActionScript 3.0 |
Runtime Versions: | Flash Player 11.4, AIR 3.4 |
Returns the string representation of the specified object.
Note: Methods of the Object class are dynamically created on Object's prototype. To redefine this method in a subclass of Object, do not use the override
keyword. For example, a subclass of Object implements function toString():String
instead of using an override of the base class.
String — A string representation of the object.
|
channelMessage | Event |
flash.events.Event
property Event.type =
flash.events.Event.CHANNEL_MESSAGE
Language Version: | ActionScript 3.0 |
Runtime Versions: | Flash Player 11.4, AIR 3.4 |
Dispatched each time the sending worker calls this MessageChannel
object's send()
method, indicating that a new message object
is available in the MessageChannel instance's queue.
Event.CHANNEL_MESSAGE
constant defines the value of the type
property of a channelMessage
event object.
This event has the following properties:
Property | Value |
---|---|
bubbles | false |
cancelable | false ; there is no default behavior to cancel. |
currentTarget | The object that is actively processing the Event object with an event listener. |
target | The object that dispatched this event. |
channelState | Event |
flash.events.Event
property Event.type =
flash.events.Event.CHANNEL_STATE
Language Version: | ActionScript 3.0 |
Runtime Versions: | Flash Player 11.4, AIR 3.4 |
Dispatched when the value of the message channel's state
property changes.
Event.CHANNEL_STATE
constant defines the value of the type
property of a channelState
event object.
This event has the following properties:
Property | Value |
---|---|
bubbles | false |
cancelable | false ; there is no default behavior to cancel. |
currentTarget | The object that is actively processing the Event object with an event listener. |
target | The object that dispatched this event. |
Thu Dec 6 2018, 01:12 PM -08:00