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.ConsumableEventBroadcaster; 20 import org.as2lib.env.event.broadcaster.EventInfo; 21 import org.as2lib.env.event.broadcaster.ConsumableEventInfo; 22 23 /** 24 * {@code SimpleConsumableEventBroadcaster} broadcasts an event to listeners until 25 * the event has been consumed. 26 * 27 * <p>The dispatching of the event will be stopped as soon as the event is consumed. 28 * The event can be consumed by calling the appropriate method on the 29 * {@code ConsumableEventInfo} instance passed to the {@code dispatch} method and 30 * from there to the appropriate event method on the listeners. 31 * 32 * @author Simon Wacker 33 * @author Martin Heidegger 34 */ 35 class org.as2lib.env.event.broadcaster.SimpleConsumableEventBroadcaster extends SimpleEventListenerSource implements ConsumableEventBroadcaster { 36 37 /** 38 * Constructs a new {@code SimpleConsumableEventBroadcaster} instance. 39 * 40 * @param listeners (optional) an array of listeners to populate this broadcaster 41 * with 42 */ 43 public function SimpleConsumableEventBroadcaster(listeners:Array) { 44 if (listeners) { 45 addAllListeners(listeners); 46 } 47 } 48 49 /** 50 * Dispatches the passed-in {@code eventInfo} to all listeners until the event has 51 * been consumed. 52 * 53 * <p>The name returned by the {@link EventInfo#getName} method of the passed-in 54 * {@code eventInfo} is used as event method name to invoke on the listeners. 55 * 56 * <p>The passed-in {@code eventInfo} is also passed as parameter to the listeners' 57 * event methods. 58 * 59 * <p>The event dispatching will be stopped as soon as the event is consumed. An 60 * event can be consumed by calling the appropriate method on the passed-in 61 * {@code eventInfo} that must be, if the consumption of events shall be enabled, 62 * an instance of type {@link ConsumableEventInfo}. 63 * 64 * @param eventInfo the event to dispatch to all listeners 65 * @throws EventExecutionException if a listener threw an exception during 66 * dispatching 67 */ 68 public function dispatch(eventInfo:EventInfo):Void { 69 if (eventInfo) { 70 var s:Number = this.l.length; 71 if (s > 0) { 72 var n:String = eventInfo.getName(); 73 if (n) { 74 var i:Number; 75 try { 76 if (eventInfo instanceof ConsumableEventInfo) { 77 var c:ConsumableEventInfo = ConsumableEventInfo(eventInfo); 78 for (i = 0; i < s && !c.isConsumed(); i++) { 79 this.l[i][n](eventInfo); 80 } 81 } else { 82 for (i = 0; i < s; i++) { 83 this.l[i][n](eventInfo); 84 } 85 } 86 } catch(e) { 87 // "new EventExecutionException" without braces is not MTASC compatible because of the following method call to "initCause" 88 throw (new EventExecutionException("Unexpected exception was thrown during dispatch of event [" + n + "] on listener [" + this.l[i] + "] with event info [" + eventInfo + "].", this, arguments)).initCause(e); 89 } 90 } 91 } 92 } 93 } 94 95 }