本文編譯後的補丁下載連結 http://download.csdn.net/source/1908278
多模組應用,每載入一個模組都會重複載入模組所使用的RSL,現在增加一個
private static var loadedRSLs:Dictionary = new Dictionary(); 來儲存是否已載入
mx.core.RSLListLoader
//////////////////////////////////////////////////////////////////////////////// // // ADOBE SYSTEMS INCORPORATED // Copyright 2007 Adobe Systems Incorporated // All Rights Reserved. // // NOTICE: Adobe permits you to use, modify, and distribute this file // in accordance with the terms of the license agreement accompanying it. // 修改SDK代碼免費RSL重複載入,提升模組載入效能 // //////////////////////////////////////////////////////////////////////////////// package mx.core { import flash.events.IEventDispatcher; import flash.events.Event; import flash.utils.Dictionary; [ExcludeClass] /** * @private * Utility class for loading a list of RSLs. * * <p>A list of cross-domain RSLs and a list of regular RSLs * can be loaded using this utility.</p> */ public class RSLListLoader { //-------------------------------------------------------------------------- // // Constructor // //-------------------------------------------------------------------------- /** * Constructor. * * @param rslList Array of RSLs to load. * Each entry in the array is of type RSLItem or CdRSLItem. * The RSLs will be loaded from index 0 to the end of the array. */ public function RSLListLoader(rslList:Array) { super(); this.rslList = rslList; } //-------------------------------------------------------------------------- // // Variables // //-------------------------------------------------------------------------- /** * @private * The index of the RSL being loaded. */ private var currentIndex:int = 0; /** * @private * The list of RSLs to load. * Each entry is of type RSLNode or CdRSLNode. */ private var rslList:Array = []; /** * @private * Supplied by caller. */ private var chainedProgressHandler:Function; /** * @private * Supplied by caller. */ private var chainedCompleteHandler:Function; /** * @private * Supplied by caller. */ private var chainedIOErrorHandler:Function; /** * @private * Supplied by caller. */ private var chainedSecurityErrorHandler:Function; /** * @private * Supplied by caller. */ private var chainedRSLErrorHandler:Function; /** * 儲存已載入的RSL標誌 */ private static var loadedRSLs:Dictionary = new Dictionary(); //-------------------------------------------------------------------------- // // Methods // //-------------------------------------------------------------------------- /** * Loads the RSLs in this list object. * * <p>This function accepts listeners to observe events * that result from loading an RSL. * The function must be of the form required by a listener function * passed to <code>IEventDispatcher.addEventListener();</code> * * When an event is received, the RSL that generated * this event can be found by calling * <code>rslListLoader.getItem(getIndex());</code></p> * * @param progressHandler Receives ProgressEvent.PROGRESS events. * May be null. * * @param completeHandler Receives Event.COMPLETE events. * May be null. * * @param ioErrorHandler Receives IOErrorEvent.IO_ERROR events. * May be null. * * @param securityErrorHandler * Receives SecurityErrorEvent.SECURITY_ERROR events. * May be null. * * @param rslErrorHandler Receives RSLEvent.RSL_ERROR events. * May be null. */ public function load(progressHandler:Function, completeHandler:Function, ioErrorHandler:Function, securityErrorHandler:Function, rslErrorHandler:Function):void { chainedProgressHandler = progressHandler; chainedCompleteHandler = completeHandler; chainedIOErrorHandler = ioErrorHandler; chainedSecurityErrorHandler = securityErrorHandler; chainedRSLErrorHandler = rslErrorHandler; currentIndex = -1; // so first loaded item will be index 0 loadNext(); } /** * Increments the current index and loads the next RSL. */ private function loadNext():void { if (!isDone()) { currentIndex++; // Load the current RSL. if (currentIndex < rslList.length) { // Load rsl and have the RSL loader chain the // events our internal events handler or the chained // events handler if we don't care about them. trace("Load RSL URL:"+(rslList[currentIndex] as RSLItem).url); if (loadedRSLs[(rslList[currentIndex] as RSLItem).url] == null){ rslList[currentIndex].load(chainedProgressHandler, listCompleteHandler, listIOErrorHandler, listSecurityErrorHandler, chainedRSLErrorHandler); loadedRSLs[(rslList[currentIndex] as RSLItem).url] = true; } else { loadNext(); // Skip already loaded RSLs } } } } /** * Gets an RSL from the list of RSLs. * * @param index A zero-based index into the list of RSLs. * * @return The current item at <code>index</code> in the list, * or <code>null</code> if there is no item at that index. */ public function getItem(index:int):RSLItem { if (index < 0 || index >= rslList.length) return null; return rslList[index]; } /** * Gets the index of the currently loading RSL. * * @return The index of the currently loading RSL. */ public function getIndex():int { return currentIndex; } /** * Gets the total number of RSLs in this object. * When the load() method is called the RSLs will be loaded. * * @return The total number of RSLs in this object */ public function getItemCount():int { return rslList.length; } /** * Tests if all the RSLs are done loading. * * @return <code>true</code> if all the RSLs have been loaded, * <code>false</code> otherwise. */ public function isDone():Boolean { return currentIndex >= rslList.length; } //-------------------------------------------------------------------------- // // Event handlers // //-------------------------------------------------------------------------- /** * @private */ private function listCompleteHandler(event:Event):void { // Pass event to external listener. if (chainedCompleteHandler != null) chainedCompleteHandler(event); // Load the next RSL. loadNext(); } /** * @private */ private function listIOErrorHandler(event:Event):void { // Pass event to external listener. if (chainedIOErrorHandler != null) chainedIOErrorHandler(event); } /** * @private */ private function listSecurityErrorHandler(event:Event):void { // Pass event to external listener. if (chainedSecurityErrorHandler != null) chainedSecurityErrorHandler(event); } } }
mx.core.RSLItem
//////////////////////////////////////////////////////////////////////////////// // // ADOBE SYSTEMS INCORPORATED // Copyright 2007 Adobe Systems Incorporated // All Rights Reserved. // // NOTICE: Adobe permits you to use, modify, and distribute this file // in accordance with the terms of the license agreement accompanying it. // //////////////////////////////////////////////////////////////////////////////// package mx.core { import flash.display.Loader; import flash.events.Event; import flash.events.ErrorEvent; import flash.events.ProgressEvent; import flash.events.IOErrorEvent; import flash.events.SecurityErrorEvent; import flash.net.URLRequest; import flash.system.LoaderContext; import flash.system.ApplicationDomain; import flash.system.LoaderContext; import flash.system.Security; import flash.system.SecurityDomain; import flash.utils.ByteArray; import mx.events.RSLEvent; import mx.utils.LoaderUtil; [ExcludeClass] /** * @private * RSL Item Class * * Contains properties to describe the RSL and methods to help load the RSL. */ public class RSLItem { //-------------------------------------------------------------------------- // // Properties // //-------------------------------------------------------------------------- //---------------------------------- // urlRequest //---------------------------------- /** * @private * Only valid after loading has started */ public var urlRequest:URLRequest; //---------------------------------- // total //---------------------------------- /** * @private */ public var total:uint = 0; //---------------------------------- // loaded //---------------------------------- /** * @private */ public var loaded:uint = 0; //---------------------------------- // rootURL //---------------------------------- /** * @private * * Provides the url used to locate relative RSL urls. */ public var rootURL:String; //-------------------------------------------------------------------------- // // Variables // //-------------------------------------------------------------------------- /** * @private */ public var url:String; /** * @private */ private var errorText:String; /** * @private */ private var completed:Boolean = false; /** * @private * External handlers so the load can be * observed by the class calling load(). */ protected var chainedProgressHandler:Function; protected var chainedCompleteHandler:Function; protected var chainedIOErrorHandler:Function; protected var chainedSecurityErrorHandler:Function; protected var chainedRSLErrorHandler:Function; //-------------------------------------------------------------------------- // // Constructor // //-------------------------------------------------------------------------- /** * Create a RSLItem with a given URL. * * @param url location of RSL to load * * @param rootURL provides the url used to locate relative RSL urls. */ public function RSLItem(url:String, rootURL:String = null) { super(); this.url = url; this.rootURL = rootURL; } //-------------------------------------------------------------------------- // // Methods // //-------------------------------------------------------------------------- /** * * Load an RSL. * * @param progressHandler Receives ProgressEvent.PROGRESS events. * May be null. * * @param completeHandler Receives Event.COMPLETE events. * May be null. * * @param ioErrorHandler Receives IOErrorEvent.IO_ERROR events. * May be null. * * @param securityErrorHandler * Receives SecurityErrorEvent.SECURITY_ERROR events. * May be null. * * @param rslErrorHandler Receives RSLEvent.RSL_ERROR events. * May be null. */ public function load(progressHandler:Function, completeHandler:Function, ioErrorHandler:Function, securityErrorHandler:Function, rslErrorHandler:Function):void { chainedProgressHandler = progressHandler; chainedCompleteHandler = completeHandler; chainedIOErrorHandler = ioErrorHandler; chainedSecurityErrorHandler = securityErrorHandler; chainedRSLErrorHandler = rslErrorHandler; var loader:Loader = new Loader(); var loaderContext:LoaderContext = new LoaderContext(); urlRequest = new URLRequest(LoaderUtil.createAbsoluteURL(rootURL, url)); // The RSLItem needs to listen to certain events. loader.contentLoaderInfo.addEventListener( ProgressEvent.PROGRESS, itemProgressHandler); loader.contentLoaderInfo.addEventListener( Event.COMPLETE, itemCompleteHandler); loader.contentLoaderInfo.addEventListener( IOErrorEvent.IO_ERROR, itemErrorHandler); loader.contentLoaderInfo.addEventListener( SecurityErrorEvent.SECURITY_ERROR, itemErrorHandler); loaderContext.applicationDomain = ApplicationDomain.currentDomain; loader.load(urlRequest, loaderContext); } //-------------------------------------------------------------------------- // // Event handlers // //-------------------------------------------------------------------------- /** * @private */ public function itemProgressHandler(event:ProgressEvent):void { // Update the loaded and total properties. loaded = event.bytesLoaded; total = event.bytesTotal; // Notify an external listener if (chainedProgressHandler != null) chainedProgressHandler(event); } /** * @private */ public function itemCompleteHandler(event:Event):void { completed = true; // Notify an external listener if (chainedCompleteHandler != null) chainedCompleteHandler(event); } /** * @private */ public function itemErrorHandler(event:ErrorEvent):void { errorText = decodeURI(event.text); completed = true; loaded = 0; total = 0; trace(errorText); // Notify an external listener if (event.type == IOErrorEvent.IO_ERROR && chainedIOErrorHandler != null) { chainedIOErrorHandler(event); } else if (event.type == SecurityErrorEvent.SECURITY_ERROR && chainedSecurityErrorHandler != null) { chainedSecurityErrorHandler(event); } else if (event.type == RSLEvent.RSL_ERROR && chainedRSLErrorHandler != null) { chainedRSLErrorHandler(event); } } } }
參考文章 http://www.adobe.com/devnet/flex/articles/flex_performance/flex_performance.pdf