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.EventExecutionException;
    18  import org.as2lib.env.event.distributor.AbstractEventDistributorControl;
    19  import org.as2lib.env.event.distributor.EventDistributorControl;
    20  
    21  /**
    22   * {@code SimpleEventDistributorControl} acts as a listener source and event
    23   * distributor control. It enables you to distribute and handle events in the
    24   * safest way possible by providing a compiler-safe distributor.
    25   * 
    26   * <p>Example:
    27   * <code>
    28   *   // creates a distributor control with the expected listener type
    29   *   var distributorControl:SimpleEventDistributorControl = new SimpleEventDistributorControl(ErrorListener);
    30   *   // adds new listeners that must be of the expected type
    31   *   distributorControl.addListener(new MyErrorListener());
    32   *   distributorControl.addListener(new SimpleErrorListener());
    33   *   // gets a distributor to distribute the event to all listeners
    34   *   var distributor:ErrorListener = ErrorListener(distributorControl.getDistributor());
    35   *   // distributes the event with custom arguments
    36   *   distributor.onError(myErrorCode, myException);
    37   * </code>
    38   * 
    39   * @author Simon Wacker
    40   * @author Martin Heidegger
    41   */
    42  class org.as2lib.env.event.distributor.SimpleEventDistributorControl extends AbstractEventDistributorControl implements EventDistributorControl {
    43  	
    44  	/** The wrapped {@code AsBroadcaster} needed for actual distribution. */
    45  	private var b;
    46  	
    47  	/**
    48  	 * Constructs a new {@code SimpleEventDistributorControl} instance.
    49  	 *
    50  	 * <p>{@code checkListenerType} is by default set to {@code true}.
    51  	 * 
    52  	 * @param listenerType the expected type of listeners
    53  	 * @param checkListenerType determines whether to check that passed-in listeners
    54  	 * are of the expected type
    55  	 * @param listeners (optional) the listeners to add
    56  	 * @throws IllegalArgumentException if the passed-in {@code listenerType} is
    57  	 * {@code null} or {@code undefined}
    58  	 */
    59  	public function SimpleEventDistributorControl(listenerType:Function, checkListenerType:Boolean, listeners:Array) {
    60  		super (listenerType, checkListenerType);
    61  		this.b = new Object();
    62  		AsBroadcaster.initialize(this.b);
    63  		this.b._listeners = this.l;
    64  		if (listeners) {
    65  			addAllListeners(listeners);
    66  		}
    67  	}
    68  	
    69  	/**
    70  	 * Removes all added listeners.
    71  	 */
    72  	public function removeAllListeners(Void):Void {
    73  		super.removeAllListeners();
    74  		this.b._listeners = this.l;
    75  	}
    76  	
    77  	/**
    78  	 * Executes the event with the given {@code eventName} on all added listeners, using
    79  	 * the arguments after {@code eventName} as parameters.
    80  	 *
    81  	 * <p>If {@code eventName} is {@code null} or {@code undefined} the distribution
    82  	 * will be omited.
    83  	 *
    84  	 * <p>If {@code args} is {@code null} or {@code undefined} nor parameters will be
    85  	 * passed to the listeners' event methods.
    86  	 * 
    87  	 * @param eventName the name of the event method to execute on the added listeners
    88  	 * @param args any number of arguments that are used as parameters on execution of
    89  	 * the event on the listeners
    90  	 * @throws EventExecutionException if an event method on a listener threw an
    91  	 * exception
    92  	 */
    93  	private function distribute(eventName:String, args:Array):Void {
    94  		if (eventName != null) {
    95  			if (this.l.length > 0) {
    96  				try {
    97  					this.b.broadcastMessage.apply(this.b, [eventName].concat(args));
    98  				} catch (e) {
    99  					// "new EventExecutionException" without braces is not MTASC compatible because of the following method call to "initCause"
   100  					throw (new EventExecutionException("Unexpected exception was thrown during distribution of event [" + eventName + "].", this, arguments)).initCause(e);
   101  				}
   102  			}
   103  		}
   104  	}
   105  	
   106  }