PageRenderTime 23ms CodeModel.GetById 13ms app.highlight 7ms RepoModel.GetById 2ms app.codeStats 0ms

/flight/domain/AsyncCommand.as

https://code.google.com/
ActionScript | 107 lines | 64 code | 14 blank | 29 comment | 10 complexity | 21177647dcbb70d7f0d259513f2f3013 MD5 | raw file
  1////////////////////////////////////////////////////////////////////////////////
  2//
  3// Copyright (c) 2009 Tyler Wright, Robert Taylor, Jacob Wright
  4// 
  5// Permission is hereby granted, free of charge, to any person obtaining a copy
  6// of this software and associated documentation files (the "Software"), to deal
  7// in the Software without restriction, including without limitation the rights
  8// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9// copies of the Software, and to permit persons to whom the Software is
 10// furnished to do so, subject to the following conditions:
 11// 
 12// The above copyright notice and this permission notice shall be included in
 13// all copies or substantial portions of the Software.
 14// 
 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 20// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 21// THE SOFTWARE.
 22//
 23////////////////////////////////////////////////////////////////////////////////
 24
 25package flight.domain
 26{
 27	import flash.events.Event;
 28	import flash.utils.clearTimeout;
 29	import flash.utils.setTimeout;
 30	
 31	import flight.commands.IAsyncCommand;
 32	import flight.net.IResponse;
 33	import flight.net.Response;
 34	
 35	[Event(name="complete", type="flash.events.Event")]
 36	[Event(name="cancel", type="flash.events.Event")]
 37	
 38	/**
 39	 * An abstract command class that supports Asynchronous commands through dispatching
 40	 * the ExecutionComplete event upon completion of the asynchronous action.
 41	 */
 42	public class AsyncCommand extends Command implements IAsyncCommand
 43	{
 44		private var interval:uint;
 45		private var _response:IResponse;
 46		
 47		[Bindable(event="responseChange")]
 48		public function get response():IResponse
 49		{
 50			if (_response == null) {
 51				// set response through the setter for appropriate handlers
 52				response = new Response();
 53			}
 54			return _response;
 55		}
 56		public function set response(value:IResponse):void
 57		{
 58			if (_response == value) {
 59				return;
 60			}
 61			
 62			if (_response != null) {
 63				_response.removeResultHandler(onResult);
 64				_response.removeFaultHandler(onFault);
 65				
 66				// link old response to the new response
 67				if (value != null) {
 68					_response.progress = value.progress;
 69					_response.status = value.status;
 70					value.addResultHandler(_response.complete);
 71					value.addFaultHandler(_response.cancel);
 72				}
 73			}
 74			
 75			var oldValue:IResponse = _response;
 76			_response = value;
 77			
 78			if (_response != null) {
 79				_response.addResultHandler(onResult);
 80				_response.addFaultHandler(onFault);
 81			}
 82			
 83			propertyChange("response", oldValue, _response);
 84		}
 85		
 86		private function complete():void
 87		{
 88			dispatchEvent(new Event(Event.COMPLETE));
 89		}
 90		
 91		private function cancel():void
 92		{
 93			dispatchEvent(new Event(Event.CANCEL));
 94		}
 95		
 96		private function onResult(data:Object):void
 97		{
 98			interval = setTimeout(complete, 1);
 99		}
100		
101		private function onFault(error:Error):void
102		{
103			clearTimeout(interval);
104			setTimeout(cancel, 1);
105		}
106	}
107}