Writing the PreloadQueue
March 14th, 2007 by DC
The e-Learning applications we develop at work are often used by employees out in the field. Not all of these employees have broadband connections, so figuring out ways to lessen the wait time for course distribution has been an on going battle. As our courses become more advanced, the file sizes often increase as well. This led me to develop the PreloadQueue (PQ) class which continues to pull content to the users computer while viewing other sections of the course.
The requirements of the PQ class where as follows:
- It had to sort the Preloads based on priority
- It had to be able to be cleared on request
- Preloads must be able to be interrupted/stopped
- Preloads must be able to ‘force’ load immediately - bypassing the queue
- Preloads must be able to specify a target MovieClip in which to load
As a rule, I like to have only one item loading at a time. I also wanted the PQ to be accessed by any part of the application and make sure that everything was adding to the same queue. Making the PQ a Singleton object was the natural solution to all of these issues.
The PQ is tightly coupled with the PreloadItem, which stores all the information about the preload. It takes four parameters. The first parameter is the path to the SWF you want to load. The second is the target MovieClip in which to display upon completing the load. Next is the priority - the lower the number, the sooner the SWF will load. The final parameter is a special object I called the linkage (I realize now that this is probably a bad name for the variable as ‘linkage’ is associated with other parts of Flash development). The linkage is used to pass extra information from the events dispatched by the PQ. For example, we organize our courses into Volumes, Chapters and Pages. We sometimes want the first page of a volume to appear on the screen differently than subsequent pages. So to do this, our new PreloadItem would look something like this:
new PreloadItem(“http://www.path.com/aSwf.swf”, holder_mc, 0, {volume:“firstSwf”})
Now I would set my eventlisteners to check for the eventObject.preloadItem.linkage.volume variable and act accordingly.
Most of the time your preloaded items won’t need to display on the screen. Simply downloading in the background to the users computer is sufficient (caching). When this is the case, you need only pass the PreloadItem a path in the constructor. There is a chance that the users computer will delete the cached SWF before it is actually needed, but in any case the SWF will simply load again. In any case, you haven’t cost the user any time since had you not used the PQ to begin with, they would have had to sit through it anyways.
Since most content displayed on screen is placed into the same holder MovieClip, I didn’t want that MovieClip to disappear while loading the next item. So, rather than force the developer to create another holder MovieClip, you pass the Stage (or a suitable MovieClip) in the getInstance function. The PQ will then use this MovieClip to create ‘dummy clips’ to load background information into.
After instantiating the PQ, developers can add preloads to it by using a method like below:
__preloadQueue.addPreload(new PreloadItem(“http://www.path.com/aSwf.swf”, holder_mc);
This will add the PreloadItem and give it a default priority of 0. If the PQ is running (which by default it is), it will automatically start to load the item at the top of the queue (which was just added). Combining this technique with the use of a for loop or an XML file, you can easily have it preload an entire site.
But what if the user clicks a link in my site and I need to load a SWF immediately? You use the loadNow function. When the loadNow function is called, the PQ will check to see if something is already in the process of loading, stop it, and re-add it to the queue. (note that depending on the priority number of the preload, it may not be the next item to loaded if there are other items with the same priority number).
But what if the item currently be loading is the same SWF asking to loadNow? If that is the case the PQ will switch it to be treated as if it was originally called from a loadNow function. That way you don’t need to start the content downloading over again.
SWFs technically get loaded twice, once via the PQ and once by the PreloadItem. When the request is made to load it in the PreloadItem script, it will have already been cached on the users computer and will load instantly into the target MovieClip. Using a hookback, the PreloadItem then tells the PQ to dispatch events for download complete and init. This allows the developer to only listen to events from the PQ (which dispatches separate events for background preload items and preloadNows).
You can download all the files needed for the PreloadQueue from the downloads section.