package com.toddrothe.service { import flash.events.Event; import flash.events.EventDispatcher; import flash.utils.Timer; import flash.utils.getDefinitionByName; import flash.utils.getQualifiedClassName; import com.toddrothe.proxies.FlickrProxy; // TR: holds service specific calls like loadImage() import com.toddrothe.proxies.YouTubeProxy; import com.toddrothe.proxies.GoogleAnalyticsProxy; public class ServiceProxy extends EventDispatcher { protected var callDurationTimer:Timer = new Timer() protected var analyticsProxy:GoogleAnalyticsProxy = new GoogleAnalyticsProxy(); protected var _service:*; // TR: in this sample the servics are imported above (YouTube and Flickr) protected var _serviceCall:String; protected var _event:String; protected var _rest:Array; public function ServiceProxy() { } public function makeCall( service:String, serviceCall:String, event:String = null, ...rest ):void { _service = service; _serviceCall = serviceCall; _event = event; _rest = rest; callDurationTimer.start(); // TR: start timer to track how long it takes to receive a response executeCall(); // TR: create and start another timer to track server timeouts } protected function executeCall():void { if( _event ) { // TR: use eventProxy helper func to clone and fire the event returned from the swc service.addEventListener( _event, eventProxy); } if( _rest ) { // TR: if there are params service[_serviceCall].apply( null, _rest ); // TR: *note the '.apply' } else { // TR: if no params then just make the call service[_serviceCall](); } } // TR: clone and dispatch the event that is returned protected function eventProxy( event:Event ):void { callDurationTimer.stop(); _request.removeEventListener( _event, eventProxy); analyticsProxy.submit( _service, _serviceCall, event, callDurationTimer.toString(), _rest ) this.dispatchEvent( event.clone() ); // TR: LoginService.login() would assign a listener which this would trigger } } }