1  /*
     2   * Copyright the original author or authors.
     3   * 
     4   * Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   * 
     8   *      http://www.mozilla.org/MPL/MPL-1.1.html
     9   * 
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  import org.as2lib.env.event.SimpleEventListenerSource;
    18  import org.as2lib.env.event.EventExecutionException;
    19  import org.as2lib.env.event.broadcaster.EventBroadcaster;
    20  import org.as2lib.env.event.broadcaster.EventInfo;
    21  
    22  /**
    23   * {@code SpeedEventBroadcaster} broadcasts events to listeners in the fastest way
    24   * possible. It does therefore not support any kind of special functionalities like
    25   * consuming events.
    26   * 
    27   * @author Martin Heidegger
    28   * @author Simon Wacker
    29   */
    30  class org.as2lib.env.event.broadcaster.SpeedEventBroadcaster extends SimpleEventListenerSource implements EventBroadcaster {	
    31  
    32  	/** The wrapped {@code AsBroadcaster} needed for actual distribution. */
    33  	private var b:Object;
    34  	
    35  	/**
    36  	 * Constructs a new {@code SpeedEventBroadcaster} instance.
    37  	 * 
    38  	 * @param listeners (optional) an array of listeners to populate this broadcaster
    39  	 * with
    40  	 */
    41  	public function SpeedEventBroadcaster(listeners:Array) {
    42  		this.b = new Object();
    43  		AsBroadcaster.initialize(this.b);
    44  		this.b._listeners = this.l;
    45  		if (listeners) {
    46  			addAllListeners(listeners);
    47  		}
    48  	}
    49  	
    50  	/**
    51  	 * Removes all added listeners.
    52  	 */
    53  	public function removeAllListeners(Void):Void {
    54  		super.removeAllListeners();
    55  		this.b._listeners = this.l;
    56  	}
    57  	
    58  	/**
    59  	 * Dispatches the passed-in {@code eventInfo} to all added listeners.
    60  	 * 
    61  	 * <p>The name returned by the {@link EventInfo#getName} method of the passed-in
    62  	 * {@code eventInfo} is used as event method name to invoke on the listeners.
    63  	 *
    64  	 * <p>The passed-in {@code eventInfo} is also passed as parameter to the listeners'
    65  	 * event methods.
    66  	 * 
    67  	 * @param eventInfo the event to dispatch to all listeners
    68  	 * @throws EventExecutionException if a listener threw an exception during
    69  	 * dispatching
    70  	 */
    71  	public function dispatch(eventInfo:EventInfo):Void {
    72  		if (eventInfo) {
    73  			if (this.l.length > 0) {
    74  				var n:String = eventInfo.getName();
    75  				try {
    76  					if (n) this.b.broadcastMessage(n, eventInfo);
    77  				} catch (e) {
    78  					// braces are around "new EventExecutionException..." because otherwise it wouldn't be MTASC compatible
    79  					throw (new EventExecutionException("Unexpected exception was thrown during dispatch of event [" + n + "] with event info [" + eventInfo + "].", this, arguments)).initCause(e);
    80  				}
    81  			}
    82  		}
    83  	}
    84  	
    85  }