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.except.IllegalArgumentException; 18 import org.as2lib.env.event.SimpleEventListenerSource; 19 import org.as2lib.env.event.EventExecutionException; 20 import org.as2lib.env.event.multicaster.EventMulticaster; 21 22 /** 23 * {@code SimpleEventMulticaster} multicasts an event to all added listeners with 24 * custom arguments. This class does not support consuming events. 25 * 26 * <p>Example: 27 * <code> 28 * // creates a multicaster for the 'onError' event 29 * var multicaster:SimpleEventMulticaster = new SimpleEventMulticaster("onError"); 30 * // adds listeners 31 * multicaster.addListener(new SimpleErrorListener()); 32 * multicaster.addListener(new MyErrorListener()); 33 * // executes the specified event on all listeners passing the given arguments 34 * multicaster.dispatch(myErrorCode, myException); 35 * </code> 36 * 37 * @author Simon Wacker 38 */ 39 class org.as2lib.env.event.multicaster.SimpleEventMulticaster extends SimpleEventListenerSource implements EventMulticaster { 40 41 /** The name of the event. */ 42 private var eventName:String; 43 44 /** The wrapped {@code AsBroadcaster} needed for actual distribution. */ 45 private var b:Object; 46 47 /** 48 * Constructs a new {@code SimpleEventMulticaster} instance. 49 * 50 * @param eventName the name of the event to execute on all added listeners 51 * @param listeners (optional) an array of listeners to populate this broadcaster 52 * with 53 * @throws IllegalArgumentException if passed-in {@code eventName} is {@code null}, 54 * {@code undefined} or an empty string 55 */ 56 public function SimpleEventMulticaster(eventName:String, listeners:Array) { 57 if (!eventName) throw new IllegalArgumentException("Argument 'eventName' [" + eventName + "] must not be 'null' nor 'undefined'.", this, arguments); 58 this.eventName = eventName; 59 this.b = new Object(); 60 AsBroadcaster.initialize(this.b); 61 this.b._listeners = this.l; 62 if (listeners) { 63 addAllListeners(listeners); 64 } 65 } 66 67 /** 68 * Returns the event name set on construction. 69 * 70 * @return the name of the event 71 */ 72 public function getEventName(Void):String { 73 return this.eventName; 74 } 75 76 /** 77 * Removes all added listeners. 78 */ 79 public function removeAllListeners(Void):Void { 80 super.removeAllListeners(); 81 this.b._listeners = this.l; 82 } 83 84 /** 85 * Dispatches the event to all added listeners passing the given arguments as 86 * parameters to the listeners' event methods. 87 * 88 * @param .. any number of arguments to pass to the listeners' event methods 89 * @throws EventExecutionException if a listener's event method threw an exception 90 */ 91 public function dispatch():Void { 92 var i:Number = this.l.length; 93 if (i > 0) { 94 if (this.l.length > 0) { 95 try { 96 this.b.broadcastMessage.apply(this.b, [eventName].concat(arguments)); 97 } catch (e) { 98 // braces are around "new EventExecutionException..." because otherwise it wouldn't be MTASC compatible 99 throw (new EventExecutionException("Unexpected exception was thrown during dispatch of event [" + eventName + "] with arguments [" + arguments + "].", this, arguments)).initCause(e); 100 } 101 } 102 } 103 } 104 105 }