Callbacks Class To Store Your Wishes In Time

Things are much easier when they works synchronously, you just ask for what you want and thats it, but with flash you always have to count on with asynchrony. For example you are waiting on some requests to complete or 3rd party to log you on etc. The issue is, you know you want to execute something, with custom properties, but you have to do that just after some asynchronous event dispatched. E.g. you want to update status with some message, but you need to connect to facebook first. Here comes the handy Callbacks class. What it does is, it stores your wishes and executes when things are prepared.

public function Test():void
{
    updateStatus("new message");
    facebook.addEventListener(FacebookOAuthGraphEvent.AUTHORIZED, onAuthorized);
    // now click on connect button to get connected
}

private function onAuthorized(event:FacebookOAuthGraphEvent):void
{
    // now you are connected, execute waiting functions
    Callbacks.execute("facebookFlag");
}

private function updateStatus(message:String):void
{
    if(!facebook.authorized)
    {
        // if you are not connected, store callback with the exact same arguments
        Callbacks.add(this, arguments.callee, arguments, "facebookFlag");
        // Callbacks.add(this, arguments.callee, arguments) ...
        // ... is universal call, and it does the same as:
        // Callbacks.add(this, updateStatus, [message])
    }
    else
    {
        // if you are connected, execute whatever you have to
        trace("... changing status");
    }
}

flag parameter is optional. When calling:

Callbacks.execute()

… all stored is executed, even those with flags. When calling

Callbacks.execute("customFlag")

… only the callbacks with customFlag are executed

1 comment so far

  1. […] automatically executes again after connected thanks to Callbacks […]

Leave a comment

Please be polite and on topic. Your e-mail will never be published.