package aw.net.amf{ import flash.events.Event; import flash.events.EventDispatcher; import flash.utils.Dictionary; public dynamic class ServiceCallerSequence extends EventDispatcher{ static private const sequences:Dictionary = new Dictionary(); protected var _callers:Array = []; protected var _successHandlers:Array = []; protected var _errorHandlers:Array = []; protected var _arguments:Array = []; protected var _inProgress:Boolean; public function ServiceCallerSequence():void{ super(); } public function call(method:String, successHandler:Function, errorHandler:Function=null, ...args):void{ this.callWithArgumentList(method, successHandler, errorHandler, args); } public function callWithArgumentList(method:String, successHandler:Function, errorHandler:Function=null, args:Array=null):void{ this._callers.push(new ServiceCaller(method, this.callerSuccessHandler, this.callerSuccessHandler)); this._successHandlers.push(successHandler); this._errorHandlers.push(errorHandler); this._arguments.push(args ? args : []); this.start(); } protected function start():void{ if(this._inProgress) return; if(this._callers.length){ sequences[this] = true; var caller:ServiceCaller = this._callers.shift(); caller.callWithArgumentList(this._arguments.shift()); this._inProgress = true; }else{ delete sequences[this]; this.dispatchEvent(new Event(Event.COMPLETE)); } } protected function callerSuccessHandler(value:*):void{ this._inProgress = false; var handler:Function = this._successHandlers.shift(); handler(value); this.start(); } protected function callerErrorHandler(value:*):void{ this._inProgress = false; var handler:Function = this._errorHandlers.shift(); handler(value); this.start(); } } }