Over 10 years of community discussion and knowledge are maintained here as a read-only archive.
public function addResource( proxyName:String, blockChain:Boolean = false ):void{this.resourceList.push( new ResourceVO( proxyName, blockChain ) );}
registerCommand( PrepStartupManagerCommand.NAME, PrepStartupManagerCommand)
sendNotification(PrepStartupManagerCommand.NAME)
package org.puremvc.utilities.startupmanager{ import com....ApplicationFacade; import flash.events.TimerEvent; import flash.utils.Timer; import org.puremvc.interfaces.IProxy; import org.puremvc.patterns.proxy.Proxy; public class StartupResourceProxy extends Proxy implements IProxy { private static const EMPTY :String = 'empty'; private static const LOADING :String = 'loading'; private static const LOADED :String = 'loaded'; private static const TIMED_OUT:String = 'timed_out'; private var status :String; private var _timeout:int; private var timer:Timer; // StartupResources, pre-requisites for this resource, if any. // These pre-requisites must be loaded before this resource can be loaded. private var _requires :Array; public function StartupResourceProxy(proxyName:String=null, data:Object=null) { super(proxyName, data); this.status = EMPTY; this.requires = new Array(); } public function setStatusToLoading() :void { status = LOADING; if(this._timeout != 0) startTimer(); } public function setStatusToLoaded() :void { status = LOADED; if(this._timeout != 0) timer.stop(); } public function setStatusToTimedOut() :void { status = TIMED_OUT; } public function isLoaded() :Boolean { return status == LOADED; } public function isTimedOut() :Boolean { return status == TIMED_OUT; } public function set requires( resources :Array ) :void { _requires = resources; } public function get requires() :Array { return _requires; } public function isOkToLoad() :Boolean { if ( status != EMPTY ) return false; for( var i:int =0; i < requires.length; i++) { var r:StartupResourceProxy = requires[i] as StartupResourceProxy; if ( !r.isLoaded()) return false; } return true; } public function set timeout(value:int):void { this._timeout = value; } private function startTimer():void { timer = new Timer(this._timeout*1000, 1); timer.addEventListener(TimerEvent.TIMER, timedOut); timer.start(); } private function timedOut(e:TimerEvent):void { this.sendNotification( ApplicationFacade.STARTUP_RESOURCE_TIMEDOUT, this.getProxyName()); } }}
package org.puremvc.utilities.startupmanager{ import flash.events.Event; public interface IStartupResourceProxy { /** * Get the Proxy name * * @return the Proxy instance name */ function getProxyName():String; function onComplete(e:Event):void; function load():void; }}
package com....controller{ import com....ApplicationFacade; import com....model.StartupMonitorProxy; import org.puremvc.interfaces.ICommand; import org.puremvc.interfaces.INotification; import org.puremvc.patterns.command.SimpleCommand; public class StartupResourceTimedOutCommand extends SimpleCommand implements ICommand { override public function execute(notification:INotification):void { ( facade.retrieveProxy( StartupMonitorProxy.NAME ) as StartupMonitorProxy). resourceTimedOut( notification.getBody() as String ); } }}
package com....model{ import com....ApplicationFacade; import org.puremvc.interfaces.*; import org.puremvc.patterns.proxy.Proxy; import org.puremvc.utilities.startupmanager.StartupResourceProxy; public class StartupMonitorProxy extends Proxy implements IProxy { public static const NAME:String = "StartupMonitorProxy"; // array listing all the resources to be loaded private var resourceList:Array; // array listing all the resources that timed out. private var timedOutResourcesList:Array; // number of loaded resources private var loadedResources:int = 0; public function StartupMonitorProxy ( data:Object = null ) { super ( NAME, data ); this.resourceList = new Array(); this.timedOutResourcesList = new Array(); } /** *Add a resource to be loaded * * @param r * */ public function addResource( r :StartupResourceProxy, timeout:int=0 ):void { this.resourceList.push( r ); //If timeout included, add a timer; if(timeout!=0) r.timeout = timeout; } /** * Start/Continue to load all resources */ public function loadResources():void { for( var i:int = 0; i < this.resourceList.length; i++) { var r:StartupResourceProxy = this.resourceList[i] as StartupResourceProxy; if ( r.isOkToLoad() ) { var proxy:* = facade.retrieveProxy( r.getProxyName() ) as Proxy; r.setStatusToLoading(); proxy.load(); } } } /** *The resource timed out, adds proxy to a list of any timed out proxies. * * @param proxyName * */ public function resourceTimedOut(proxyName :String ):void { for( var i:int = 0; i < this.resourceList.length; i++) { var r:StartupResourceProxy = this.resourceList[i] as StartupResourceProxy; if ( r.getProxyName() == proxyName ) { r.setStatusToTimedOut(); timedOutResourcesList.push(r.getProxyName()); this.loadedResources++; // if not all loaded, continue load. if ( !allResourcesLoaded() ) { this.loadResources(); } break; } } } /** * The resource is loaded, update the state and check if the loading process is completed * * @param name proxy name */ public function resourceLoaded( proxyName :String ):void { for( var i:int = 0; i < this.resourceList.length; i++) { var r:StartupResourceProxy = this.resourceList[i] as StartupResourceProxy; if ( r.getProxyName() == proxyName ) { r.setStatusToLoaded(); this.loadedResources++; // send the notification for update the progress bar //this.sendNotification( ApplicationFacade.LOADING_STEP, this.resourceList.length / this.loadedReources * 100 ); // if not all loaded, continue load. if ( !allResourcesLoaded() ) { this.loadResources(); } break; } } } /** * Check if the loading process is completed * * @return boolean process is completed */ private function allResourcesLoaded():Boolean { for( var i:int = 0; i < this.resourceList.length; i++) { var r:StartupResourceProxy = this.resourceList[i] as StartupResourceProxy; if ( !r.isLoaded() && !r.isTimedOut() ) { return false; } } this.sendNotification( ApplicationFacade.LOADING_COMPLETE ); return true; } public function timeOutList():Array { return timedOutResourcesList; } }}
package com....controller{ public class ModelPrepCommand extends SimpleCommand implements ICommand { override public function execute( note:INotification ):void { /* ... register monitor proxies ... */ facade.registerProxy( new StartupMonitorProxy() ); var monitor:StartupMonitorProxy = facade.retrieveProxy( StartupMonitorProxy.NAME ) as StartupMonitorProxy; /* ... register other proxies ... */ facade.registerProxy( new ProxyA() ); facade.registerProxy( new ProxyB() ); var rA:StartupResourceProxy = facade.retrieveProxy( ProxyA.NAME ) as StartupResourceProxy; var rB:StartupResourceProxy = facade.retrieveProxy( ProxyB.NAME ) as StartupResourceProxy; monitor.addResource( rA, 10); monitor.addResource( rB, 10 ); rA.requires = [ rB ]; monitor.loadResources(); } }}