Gaia Arguments, Real World Bridge Pattern, and gaia_internal

The Gaia Beta was released today by a friend of mine, Steven Sacks. Gaia is a Flash framework created for Flash Designers & Developers who create Flash sites. The reason this is an important is that it now supports ActionScript 3 and Flash CS3.

Discussions

I must of racked up at least $500 in cell phone / mobile bills talking to Steven about Gaia over the phone over the past few months (he’s in Los Angeles, I’m in Atlanta). This doesn’t include numerous emails and IM’s. We’ll argue about implementation details, coding styles, and design pattern implementations. Sometimes their just discussions about details because we agree and are on the same page. The arguments about terminology and Flash authoring techniques are usually one sided; Steven stands his ground, has chosen pretty appropriate industry lingo, and knows his audience way better than I do.

My job isn’t to congratulate him on the immense amount of work he’s done on the AS3 version, on porting the new ideas gained in AS3 development BACK into AS2, or for just the good execution of his passion. My job is to be his friend. That means to question everything to ensure he’s thought thoroughly about something, to be devils advocate, and generally be a dick to see if he cracks. If he does crack, it’s a weakness exposed, and we then have to discuss about who’s opinion on fixing it is better.

This doesn’t happen with everything, only small parts of Gaia that he asks for feedback on. The rest I have confidence he already got right… although, I did manage to write 24 TODO’s/FIXME’s for 3 classes he wanted my feedback on. F$@#ker only agreed with like 2… or at least, he only openly admitted 2 were decent. I’m sure if I did the whole framework, I’d have more, although, I might have less once I then understand most of the design decisions :: shrugs ::. Doesn’t mean Steven would agree; it’s his framework and he’s a good Flash Developer. With his understanding of other Flash Designers & Dev’s and how they work, he ultimately knows best.

Solving the “no more _global” Problem

One part Steven DID let me actually help a lot on was the global Gaia API. In Flash Player 8 and below, this Singleton existed on a namespace called “_global”. This was a dynamic object you could put anything you wanted on and all code everywhere, including dynamically loaded SWF’s, could access. Aka, the perfect place for the Gaia API Singleton. Naturally, we both were like… crap, what the heck do we do since there is no _global in AS3. Damn Java developers can do DIAF. Someone get that Python creator guy’s number and tell him that Macromedia would like to re-consider their offer back in Flash 5 instead of going with ECMA… oh wait… Macromedia is no more… dammit!

It just so happens, Steven remembered reading my blog entry with the proposed solution for Flash CS3 not having an exclude.xml option. The server architect and long time Java dev at my work, John Howard, suggested the Bridge pattern idea initially, explaining that interfaces are smaller than actual class implementations in file size. Steven and I discussed the Bridge pattern way I suggested, using internal classes in an SWC sneakily injected into people’s Libraries, and another solution proposed by one of my readers, Sanders, in the comments. The Bridge pattern seemed best, but we were concerned about file size because it was an un-tested theory. As you can see, this turned out to be a good theory; 1.3k == f’ing dope!

When I went back and re-read my blog post I realized I didn’t really explain how the Bridge pattern works in Flash Developer lingo. As my blog reader audience has accumulated Java & C++ devs just getting into Flex, I’ve tried to use lingo they’d jive with. So, let me re-hash what the Bridge pattern attempts to solve in 1 2 3 4 sentences 1 paragraph.

You cannot exclude classes in Flash CS3 using exclude.xml like you could in Flash MX 2004 using AS2. Therefore, if you re-use classes, say “Gaia.api.goto” in other FLA’s that will later be loaded in, you’re duplicating classes in many SWF’s, greatly increasing the file size of your entire site. Instead, we just created Gaia to be a shell that makes calls an object “we’ll set in the parent SWF”. This Gaia shell class compiles to 1.3k vs. the 6 to 12k the implementation would of normally taken. That’s 10k (probably more) savings per SWF.

These savings make HUGE differences on enterprise size Flash sites like Ford Vehicles and Disney; basically any huge Flash portal that gets one million+ visitors a day. Akamai or other CDN‘s aren’t exactly cheap. The 10k you save per SWF could be $10,000 in bandwidth costs per month. But screw the bandwidth costs, it’s all about the user experience, baby! Fast for the win.

The gaia_internal namespace

The down side was I KNEW we’d have to expose at least 1 public variable on the Gaia Singleton. We don’t want people setting things on the Gaia api class they aren’t supposed to; whether on purpose or by accident (accidental h@xn04?). So, I copied what the Flex SDK does. They use this thing called “mx_internal”. It’s a namespace the Flex team created for the same situation: You want to expose a public property, but you don’t want other people messing with it.

You can’t use private because it’s not accessible by other classes. You can’t use protected because you have to extend the class. You can’t use public because that implies its ok to touch… like certain outfits certain genders wear… and in the same vein, that doesn’t really mean you CAN touch! In that scenario, it’s a wedding band. In the ActionScript scenario, it’s using a specifically named namespace you create your self. I suggested gaia_internal. That way, only Steven can use that namespace and thus set those properties. If other people do it, they’re either really smart, or crackheads. For the latter, it makes it easier to call someone out on doing something un-supported if they are actively using the gaia_internal namespace in their code.

It ALSO makes it easier to change implementation details in the future if Steven so chooses. Like all things in Flash, even AS3, things will be custom created for certain projects. This could include changes or extensions to the Gaia framework itself. You should encourage this instead of be against it. Therefore, keeping weird internal things in a specific namespace helps, at least a little, ensure existing projects won’t have to worry too much about changes & improvements in future versions of Gaia.

Future Solution: Using Flex’ compc

Yes, Sanders, your solution is technically superior. As others have told you, however, it is too complicated. Flash Developers thrive on getting cool stuff done quickly. While I’m sure some Linux afficiando, command line pro, Emacs weilding zealot will argue that he can run your solution faster than I can hit Control + Enter, most Flash Devs don’t care.

We all agree the Gaia api should be 1 class; not 3. The whole point of the Bridge pattern is to support new implementations. I highly doubt Steven will ever create a new implementation of Gaia; we just followed the pattern to save filesize.

Therefore, what you need to do to both win the hearts of millions of Flash designer & developers everywhere as well as fix a flaw in Flash CS3 is to write a JSFL script that does your solution; and then have a way to map your JSFL script as a keyboard shortcut (this part is easy; its built into Flash). The golden rule in Flash is you should be able to “Test Movie” and see it work. It’s the same thing as checking in code that compiles into a Subversion repository. If you nail that, you’re golden and the Bridge pattern way will then become a nightmare of the past we can all forget.

If you need help writing JSFL, let me know; my skills are rusty but I can re-learn pretty quick. The goals are:

1. Get a JSFL script to compile as normal so you can see a movie work via Control + Enter “Test Movie”
2. Get a JSFL script to run your magic so the classes you don’t want compiled in (aka Gaia, PageAsset, etc.); you can then map this to like Control + Alt + Enter (or whatever)

Conclusions

If you’re a Flash Developer who builds Flash sites, go check out Gaia. If you’re using a ton of loaded SWF’s in your site, go check out my original entry as I now have proof the theory works. If you’re Sanders, GET TO WORK! AS3 Flash Site is about to die… needs Sanders’ bandwidth reduction, badly!!!

21 Replies to “Gaia Arguments, Real World Bridge Pattern, and gaia_internal”

  1. 24 TODOs? Ha! I think there were maybe 10 and most of them were unnecessary null checks. I know, I know. You think every argument passed in a function is out to get you! Might want to take some chlorpromazine for that. ;)

    Go ahead and look at the whole thing and slather my code with your TODOs. FlashDevelop has a nifty little “Tasks” panel that shows me your paranoid scribblings in an itemized list. Heeeeeeeeeeeeeeeeeeeeere’s Jesse!

  2. You’re right! I do need to get to work :-) And now I know for sure my explanation sucks! But I still will not be writing any JSFL though, you can actually see you’re movie working using the magic ctrl + enter combination, just download my rpc examples and see for yourself…

    I believe that if you know what intrinsic is and you’re into ‘Bridge’ patterns and the sorts then you’re already a bit of a Linux afficiando, command line pro, Emacs weilding zealot anyway. So is the manual compilation of a separate ‘library’ really such a big deal?

    Now I’ll stop evangelizing my solution, and so some real work! :-)

  3. Hah, no way dude, only play games hosted on Linux servers, I don’t actually use Linux. No, your explanation doesn’t suck. There are just certain audiences that totally agree with what your doing, and others who are like, “That’s too much trouble”. We need to satiate those peeps. Cool, I’ll go download your RPC and take a closer look. Yes, compiling a separate library is hard; it’s an extra step. If you make this shiz easy for Flash peeps, it rocks. Stay tuned…

  4. Hey, instead of duplicating all of the GaiaImp methods in the Gaia class, why don’t you just use Gaia as an holder for the GaiaImp instance ?

    so in your swfs, instead of calling:
    Gaia.api.goto(“…”);

    you could call :
    Gaia.api.impl.goto(“…”);

    Gaia.api.impl would be a public var typed as IGaia, so you get strict typing and you don’t have to maintain 3 classes with the same methods…
    I’d prefer to use Gaia.instance instead of Gaia.api though, it’s more standard singleton wording imo.

    Or better yet, no need for a singleton, just make a public static var impl:IGaia in Gaia so you can call it like this :
    Gaia.impl.goto(“…”);

  5. The second you do an import AND use GaiaImpl, a child SWF will then compile that in. We don’t want that. That same 6k/10k (whatever GaiaImpl.as compiles to) is now duplicated unnecessarily in each child SWF. The only classes we are duplicating in child.swf’s on purpose are Gaia and IGaia; those together compile to 1.3k.

    That way, the main.fla, the dude who runs GaiaMain, can then set this instance variable AND be the ONLY SWF who actually compiles GaiaImpl.as. Make sense?

    Maintaining 2 classes isn’t hard when they both extend the same interface; if we mess up, Flash yells at us and refuses to compile.

    …however, you do have a point about the instance naming convention. I believe Steven chose Gaia.api because it’s easier to type than Gaia.instance. Flash Developer pragmatism over Programmer Purism Methodology. :: shrugs ::

  6. The reason I chose api over instance is because the actual implementation of it as a singleton is irrelevant to the developer who is accessing the API. The developer doesn’t know it’s a singleton (it used to be a static class, anyway), shouldn’t know it’s a singleton and doesn’t need to treat it like a singleton.

    So now it comes down to a naming convention and I think api is very specific and successfully conveys what it is you’re accessing, while instance is too general and exposes the implementation when that exposure is, IMO, counter-productive.

    Gaia.api means you’re accessing my framework’s api. It would be great if I could just say Gaia.whatever() like it was before when it was a static class, but unfortunately, you cannot put static methods in an interface.

  7. package com.gaiaframework.api
    {
    import com.gaiaframework.core.gaia_internal;
    public class Gaia
    {
    gaia_internal static var impl:IGaia;
    public static function get api():IGaia
    {
    return impl;
    }
    }
    }
    

    It’s ends up being 872 bytes less in the child swf, which is about half the size it was before (1.63k). This makes logical sense in that I was duplicating every method. Awesome optimization, Patrick. Thanks!

  8. Pingback: Wichers

Comments are closed.