Tuesday, May 25, 2010

Loading Queue in Actionscript

There comes a time when in an actionscript project you have to load a bunch of assets and have them all be ready before calling the setup function that operates on those loaded assets.
The solution for this recurring problem should be standardized, and separated into its own class rather than polluting classes concerned with GUI events or with server-side interaction.
We are aiming for a highly reusable and hence simple bulk loader. Regular usage is listed in the following:
  • Pass an array of the requests to the bulk loader class
  • Listen for when the list has completed
  • Listen for when an item has been completed
  • Get the number of remaining items
Here's a simple implementation to this problem. Please report any misbehavior using comments.
package {
    import flash.display.Loader;
    import flash.events.Event;
    import flash.events.EventDispatcher;
    import flash.net.URLRequest;
    
    public class BulkLoader extends EventDispatcher{
        private var requests:Vector.<URLRequest>;
        private var current:Loader;
        public function BulkLoader(requests:Vector.<URLRequest>) {
            this.requests = requests;
        }
        
        public function start():void {
            if (requests.length == 0return;
            var req:URLRequest = requests.shift();
            current = new Loader();
            current.contentLoaderInfo.addEventListener(Event.COMPLETE, _currentComplete);
            current.load(req);
        }
        
        public function stop():void {
            if (current == nullreturn;
            current.close();
            requests.unshift(current);
            current = null;
        }
        
        private function _currentComplete(e:Event):void {
            current.removeEventListener(Event.COMPLETE, _currentComplete);
            dispatchEvent(new Event(Event.CHANGE));
            if (requests.length == 0)
                dispatchEvent(new Event(Event.COMPLETE));
            else
                start();
        }
    }
}

2 comments:

  1. Ok this is looking good. I really liked the idea. But I have to ask, am I supposed to load the requests into the Vector passed in the instructor, call the start() function and let it do the work all by itself? Or do I need to manage it from where I'm calling it (the Document Class for example)? I'm leaning towards the second assumption because you're not calling stop() from within the class itself...

    var reqs:Vector = new Vector;
    //code to load the requests into reqs

    var bulkLoader:BulkLoader = new BulkLoader(reqs);
    bulkLoader.start();
    bulkLoader.addEventListener(Event.COMPLETE, cleanup);

    function cleanup(e:Event)
    {
    e.target.stop();
    }

    Is that how it's supposed to be used? I feel there's something wrong about my code, but that's what I could think of... Correct me if I'm wrong please

    ReplyDelete
  2. You're close enough, however, instead of cleanup, you should be calling a continuation of your logic. Perhaps a setup function. There is no need to stop the bulk loader once it's complete. (The stop function is only there if you intend to interrupt the loading operation, or postpone it).. Most often than not, you will not need to.

    ReplyDelete