Search code examples
actionscript-3listeners

addEventListeners and best practices


I'm using this simple code to retrieve a value from a PHP script:

package 
{
    import flash.display.MovieClip;
    import flash.net.URLLoader;
    import flash.net.URLVariables;
    import flash.net.URLRequest;
    import flash.net.URLRequestMethod;
    import flash.events.Event;
    import flash.events.IOErrorEvent;
    //
    public class URLTest extends MovieClip
    {
        //
        var scriptLoader:URLLoader;
        //
        public function URLTest()
        {
            scriptLoader = new URLLoader();
            scriptLoader.addEventListener(Event.COMPLETE, handleLoadSuccessful);
            scriptLoader.addEventListener(IOErrorEvent.IO_ERROR, handleLoadError);
        }
        //;
        public function handleLoadSuccessful($evt:Event):void
        {
            trace("Message sent.");
            trace($evt.target.data);
        }
        //
        public function handleLoadError($evt:IOErrorEvent):void
        {
            trace("Message failed.");
        }
        //
        public function sendLoad(variable):void
        {
            var scriptRequest:URLRequest = new URLRequest("http://mydomain/myapp/my.php");
            var scriptVars:URLVariables = new URLVariables();

            scriptVars.var1 = variable;

            scriptRequest.method = URLRequestMethod.POST;
            scriptRequest.data = scriptVars;

            scriptLoader.load(scriptRequest);
            trace(scriptLoader);
        }

    }

}

If you look at the URLTest constructor you'll find I initialize the listeners of an instance of URLLoader.

Is it a better practice to declare a method initListeners and call it everytime I call sendAndLoad? As a consequence, would it be a better practice that of removing the listeners inside the handleLoadError and handleLoadSuccesful methods? Are listeners cpu-intensive?

Btw, I don't understand why this question "appears to be subjective", since we're talking about performances, not programming style.


Solution

  • As your class is designed to be able to make multiple calls to the same webservice, I would definitely go for option 2. Consider this:

    var loader:URLTest = new URLTest();
    loader.sendLoad("a");
    loader.sendLoad("b");
    

    Now, if you re-use the same URLLoader for both calls, there is no way you can know which COMPLETE event is going to correspond to which call.

    To solve this issue you would create a URLLoader (+ event listeners) for each call and put it in an Array to be able to identify it. When an answer comes from the server - be it a COMPLETE or an IOERROR - you can remove the event listeners and remove the identified URLLoader (the one that matches event.currentTarget) from the Array.

    The performance issue is trivial here. There will be no performance hit for creating a bunch of event listeners as long as you remember to remove them when you're done with them. Otherwise the corresponding URLLoaders will not be garbage collected and will keep piling up in memory.

    On a side note: there's absolutely no reason to extend from MovieClip. If you want your class to dispatch events, just extend EventDispatcher.