title: how to synchronize methods in actionscript?
body: <p>The question is how could I stop a method being called twice, where the first call has not "completed" because its handler is waiting for a url to load for example?</p>
  
  <p>Here is the situation:</p>
  
  <ol>
  <li>I have written a flash client which interfaces with a java server using a binary encrypted protocol (I would love to not have had to re-invent the whole client/server object communcation stack, but I had to encrypt the data in such a way that simple tools like tamper data and charles proxy could not pick them up if using SSL).</li>
  <li>The API presents itself to flas as an actionscript swf file, and the API itself is a singleton. </li>
  <li>the api exposes some simple methods, including:
  <ol>
  <li>login()</li>
  <li>getBalance()</li>
  <li>startGame()</li>
  <li>endGame()</li>
  </ol></li>
  <li>Each method will call my HttpCommunicator class.</li>
  </ol>
  
  <p>HttpCommunicator.as (with error handling and stuff removed):</p>
  
  <pre><code>public class HttpCommunicator {
      private var _externalHalder:function;
      public function communicate(data:String, externalHandler:APIHandler):void {      
          // do encryption
          // add message numbers etc to data.
          this._externalHalder = externalHandler;
          request.data = encrypt(addMessageNumers(data)));
          loader.addEventListener(Event.COMPLETE, handleComplete);
          loader.load(request);
      }
  
      private function handleComplete(event:Event):void {
        var loader:URLLoader = URLLoader(event.target);
            String data = decrypt(loader.data);
            // check message numbers match etc.
     _externalHandler(data);
      }
  </code></pre>
  
  <p>The problem with this is I cant protect the same HttpCommunicator object from being called twice before the first has handled the complete event, unless:</p>
  
  <ol>
  <li>I create a new HttpCommunicator object every single time I want to send a message.  I also want to avoid creating a URLLoader each time, but this is not my code so will be more problematic to know how it behaves).</li>
  <li>I can do something like syncronize on communicate.  This would effectivly block, but this is better than currupting the data transmission.  In theory, the Flash client should not call the same api function twice in a row, but I but it will happen.</li>
  <li>I implement a queue of messages.  However, this also needs syncronization around the push and pop methods, which I cant find how to do.</li>
  </ol>
  
  <p>Will option 1. even work?  If I have a singleton with a method say getBalance, and the getBalance method has:</p>
  
  <pre><code>// class is instantiated through a factory as a singleton
  public class API{   
  var balanceCommunicator:HttpCommunicator = new HttpCommunicator();  // create one for all future calls.
  public funciton getBalance(playerId:uint, hander:Fuction):Number {
      balanceCommunicator.communicate(...);  // this doesnt block
      // do other stuff
  }
  </code></pre>
  
  <p>Will the second call trounce the first calls communicator variable?  i.e. will it behave as if its static, as there is onlyone copy of the API object?</p>
  
  <p>If say there was a button on the GUI which had "update balance", and the user kept clicking on it, at the same time as say a URLLoader complete event hander being called which also cals the apis getBalance() function (i.e. flash being multithreaded).</p>