ChannelBroadcaster

Kind of class:public class
Package:com.bourre.events
Inherits from:none
Known subclasses:
Author:Francis Bourre
Classpath:com.bourre.events.ChannelBroadcaster
File last modified:Monday, 24 November 2008, 11:36:53
The ChannelBroadcaster is a macro broadcaster
which offer to dispatch events over communication channels.

The main idea is the following, developpers can create specific
channels for communication, onto which event will be broadcasted.
To take a metaphor, is like a CB radio, you can select a frequency
and then you will listen and talk to people which are connected to
that frequency, and only that one. To achieve this process, the
channel broadcaster aggregates Broadcaster implementation
and map it with EventChannel object. In that principle
the channel broadcaster is no more than a CB base station. With
a big difference anyway, the broadcaster channel offers an access
to a frequency, it doesn't broadcast messages itself.

To initiate the dispatching on a specific channel, developers
only need to call the redefined functions of the class as they
can do with a Broadcaster implementation with a proper
event channel, the channel broadcaster will check for the presence
of a broadcaster instance for this channel, and if there's no
corresponding broadcaster it will create it.

The channel broadcaster redefines many functions of the

Broadcaster interface in order to add the channel
parameter. See below the list of redefined methods :

  • isRegistered
  • broadcastEvent
  • addListener
  • addEventListener
  • removeListener
  • removeEventListener
  • hasChannelListener, which correspond
    to the hasListenerCollection method of the

    EventBroadcaster class
  • .

Summary


Constructor
Instance methods

Constructor

ChannelBroadcaster

public function ChannelBroadcaster (
broadcasterClass:Class = null, channel:EventChannel = null)

Creates a new ChannelBroadcaster with the passed-in

Broadcaster class to build broadcaster instances and
an event channel as default channel. The default channel is used
when a call to function is done without specifying any channel. If
the channel argument is omitted, the default channel is set to the
internal DefaultChannel.CHANNEL constant.
Parameters:
broadcasterClass:
broadcaster class wrapped by this channel
broadcaster
channel :
default channel for this broadcaster
Throws:
  • {VISDOC_LINK_0}IllegalArgumentException — If the passed-in
    class doesn't implement Broadcaster interface

Instance methods

addEventListener

public function addEventListener (
type:String, listener:Object, channel:EventChannel = null) : Boolean

Adds an event listener for the specified event type of the
specified channel. There is two behaviors for the

addEventListener function :
  1. The passed-in listener is an object :
    The object is added as listener only for the specified event, the object
    must have a function with the same name than type or at least
    a handleEvent function.
  2. The passed-in listener is a function :
    A Delegate object is created and then
    added as listener for the event type . There is no restriction on the name
    of the function.
Parameters:
type :
name of the event for which register the listener
listener:
object or function which will receive this event
channel :
event channel for which the listener listen
Returns:
  • true if the function have been succesfully added as
    listener fot the passed-in event
Throws:
  • {VISDOC_LINK_0}UnsupportedOperationException — If the listener is an object
    which have neither a function with the same name than the event type nor
    a function called handleEvent

addListener

public function addListener (
listener:Object, channel:EventChannel = null) : Boolean

Adds the passed-in listener as listener for all events
dispatched by this event channel broadcaster. The function
returns true if the listener have been added
at the end of the call. If the listener is already registered
in this event channel broadcaster the function returns

false.

Note : The addListener function doesn't accept
functions as listener, functions could only register for
a single event.

Parameters:
listener:
the listener object to add as channel listener
channel :
the channel for which the object listen
Returns:
  • true if the listener have been added during this call
Throws:
  • {VISDOC_LINK_0}IllegalArgumentException — If the passed-in listener
    listener doesn't match the listener type supported by this
    broadcaster instance
  • {VISDOC_LINK_1}IllegalArgumentException — If the passed-in listener
    is a function

broadcastEvent

public function broadcastEvent (
e:Event, channel:EventChannel = null) : void

Broadcast the passed-in event object to listeners
according to the event's type and channelargument. The event is broadcasted to both listeners
registered specifically for this event type and global
listeners in the broadcaster.

If the target property of the passed-in event
is null, it will be set using the value of the
source property of this event broadcaster.

Parameters:
e :
event object to broadcast
channel:
event channel onto which broadcast event
Throws:
  • {VISDOC_LINK_0}UnsupportedOperationException — If one listener is an object
    which have neither a function with the same name than the event type nor
    a function called handleEvent

empty

public function empty (
) : void

Clean the current channel broacaster by removing all

Broadcaster instances previously created
and then rebuild the default EventBroadcaster.

getDefaultChannel

public function getDefaultChannel (

Returns a reference to the default channel of this
channel broadcaster. This function never returns

null.
Returns:
  • a reference to the default channel of this
    channel broadcaster

getDefaultDispatcher

public function getDefaultDispatcher (

Returns the Broadcaster implementation associated
with the default channel of this channel broadcaster.
Returns:
  • the Broadcaster implementatation associated
    with the default channel of this channel broadcaster

hasChannelDispatcher

public function hasChannelDispatcher (
channel:EventChannel) : Boolean

Returns true if there is a Broadcasterinstance registered for the passed-in channel.
Parameters:
channel:
channel onto which look at
Returns:
  • true if there is a Broadcaster instance registered for the passed-in channel

hasChannelListener

public function hasChannelListener (
type:String, channel:EventChannel = null) : Boolean

Returns true if there is a Broadcaster instance
registered for the passed-in channel, and if this broadcaster has registered
listeners.
Parameters:
type :
event type to look at
channel:
channel onto which look at
Returns:
  • true if there is a Broadcaster instance registered for the passed-in channel, and if this
    broadcaster has registered listeners

isRegistered

public function isRegistered (
listener:Object, type:String, channel:EventChannel) : Boolean

Returns true if the passed-in listeneris registered as listener for the passed-in event typein the passed-in channel.
Parameters:
listener:
object to look for registration
type :
event type to look at
channel :
channel onto which look at
Returns:
  • true if the passed-in listener is registered as listener for the passed-in event
    type in the passed-in channel

releaseChannelDispatcher

public function releaseChannelDispatcher (
channel:EventChannel) : Boolean

Returns the Broadcaster instance associated with the
passed-in channel<code>. The <code>owner is an optionnal
parameter which is used to initialize the newly created Broadcasterwhen there is no broadcaster for this channel.
Parameters:
channel:
the channel for which get the associated broadcaster
owner :
an optional object which will used as source if there
is no broadcaster associated to the channel
channel:
channel for which remove the associated broadcaster
Returns:
  • the Broadcaster instance associated with
    the passed-in
    channel<code>
    %ASDOCHR%/
        public function getChannelDispatcher( channel : EventChannel = null, owner : Object = null ) : Broadcaster
        {
            if ( hasChannelDispatcher( channel ) )
                return channel == null ? _mChannel.get( _oDefaultChannel ) : _mChannel.get( channel );
            else
            {
                var eb : Broadcaster = new (_broadcasterClass as Class)( owner );
                _mChannel.put( channel, eb );
                return eb;
            }
        }
    
        /**
    %ASDOCHR%Removes the <code>Broadcaster
    instance associated with
    the passed-in channel<code>, and return <code>trueif there is a broadcaster and if it have been successfully removed.
  • true if there is a broadcaster and if it have
    been successfully removed.

removeEventListener

public function removeEventListener (
type:String, listener:Object, channel:EventChannel = null) : Boolean

Removes the passed-in listener for listening the specified event of
the specified channel. The listener could be either an object or a function.
Parameters:
type :
name of the event for which unregister the listener
listener:
object or function to be unregistered
channel :
event channel on which unregister the listener
Returns:
  • true if the listener have been successfully removed
    as listener for the passed-in event

removeListener

public function removeListener (
listener:Object, channel:EventChannel = null) : Boolean

Removes the passed-in listener object from this event
channel broadcaster. The object is removed as listener
for all events the broadcaster may dispatch on this
channel.
Parameters:
listener:
the listener object to remove from
this event broadcaster object
channel :
the channel for which the object will be removed
Returns:
  • true if the object have been successfully
    removed from this broadcaster instance
Throws:
  • {VISDOC_LINK_0}IllegalArgumentException — If the passed-in listener
    is a function

setDefaultChannel

public function setDefaultChannel (
channel:EventChannel = null) : void

Defines which channel is used as default channel for this
channel broadcaster. If the passed-in channel is nullthe internal DefaultChannel.CHANNEL constant is used
as default channel.
Parameters:
channel:
the new default channel for this channel broadcaster

toString

public function toString (
) : String

Returns the string representation of this instance.
Returns:
  • the string representation of this instance