Posted by: edsyrett | 18 December 2008

How to load the same module repeatedly…


I’m currently working on a dashboard application that loads up the same module over and over.  Each module contains a chart component, and the only difference in the way the module is used is the data that is passed in from the server.  It makes sense to use the same module because the charts are all presented in the same way.

So I started off setting ModuleLoader.url over and over, and I ran into a bunch of exceptions.  After having a good look at ModuleManager, it wasn’t hard to see why…

Have a look at this code…


public function getModule(url:String):IModuleInfo
{
    var info:ModuleInfo = moduleList[url] as ModuleInfo;

    if (!info)
    {
        info = new ModuleInfo(url);
        moduleList[url] = info;
    }

    return new ModuleInfoProxy(info);
}

If you request the same URL over again, you’ll get the same instance of ModuleInfo. But in ModuleInfo there are a bunch of private vars – loader and factoryInfo.

If you end up calling getModule() with a url that you’ve already requested, and that module hasn’t finished loading yet, loader and factoryInfo get overwritten, and from that point on, you’re doomed.

I’ve created a little test app here that demonstrates this problem. It shows code that sets ModuleLoader.url to the same value in a tight loop. And if you do that, it will fail. You could wait until the module has loaded the first time before requesting it again, but to my mind that’s a poor solution because it would take ages to load a number of modules, and the Flex Framework is perfectly capable of loading them in parallel.

But look at the code from ModuleManager again – it decides whether or not to return the same ModuleInfo based on the URL. So if you can fake the URL but make it load the same module, you’re in business. And that’s just what the test app does, by appending an index (it could be anything) as though it’s a parameter on the end of the URL.

So instead of this…

http://.../TestModule.swf

… we’re using this…

http://.../TestModule.swf?1

ModuleManager is obviously like this by design – after all, who’s going to be daft enough to load the same module over and over… But quite why it is designed like this is beyond me. loader and factoryInfo are written to behave almost like Global vars, and you know how dangerous they are.

But for now, the solution I suggest above seems to work, and we’re going to use it in our Dashboard app…

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: