0 Replies Latest reply on Aug 16, 2007 7:27 AM by gkidd

    Dealing with asynchronous events

    gkidd
      Hi,

      I am currently writing a flex application that speaks to a SOAP webservice. I was able to write actionscript to speak to the webservice operations and everything works fine. Then I got to an operation that actually required multiple webservice operations to be executed serially.

      Initially I took the typically naive (or so I imagine) approach of executing these as if they fired synchronously as I was ignorant, i.e.:

      this._webService.login.addEventListener(ResultEvent.RESULT, handleLoginResult);
      this._webService.getUsers.addEventListener(ResultEvent.RESULT, handleGetUsersResult);


      this._webService.login.send();

      // this operation requires login to have fired so it fails at least some of the time
      this._webService.getUsers.send();


      As you can see this does not work. So then I got to thinking and looking at the core classes and the way they fit together and I came up with the idea of chaining the operations from the handlers since this is where completion occurs, this left me with:

      this._webService.login.addEventListener(ResultEvent.RESULT, handleLoginResult);
      this._webService.getUsers.addEventListener(ResultEvent.RESULT, handleGetUsersResult);

      this._webService.login.send();


      public function handleLoginResult(event:ResultEvent):void
      {
      var someState:Object = event.result;
      this._webService.getUsers.send();
      }

      This appraoch seems to work, but it requires me to author specific handlers for each sequence of operations I need which requires me to add and remove event listeners on a per operation basis based on what sequence I need. This is not reusable and seems very error prone.

      I hit the internet again and I found a blog called kuwamoto.org, which had 3 fascinating articles that sort of dealt with some of the problems I was having, specifically the articles are:

      Dealing with asynchronous events, part 1 - http://kuwamoto.org/2006/05/16/dealing-with-asynchronous-events-part-1/
      Dealing with asynchronous events, part 2 - http://kuwamoto.org/2006/05/16/dealing-with-asynchronous-events-part-2/
      Dealing with asynchronous events, part 3 - http://kuwamoto.org/2006/05/16/dealing-with-asynchronous-events-part-3/

      But even after this, the article does not really address any design patterns for dealing with n-step sequences of operations needed to achieve model state for a given view. So after reading all of this I do have some ideas whereby I could use the AsyncToken returned from the send() methods of each operation to store an object that keeps track of the chain to execute via some generic result handler (chain of responsibility pattern with inversion of control pattern) but then I am starting to think that this approach might be overkill.

      So are there any established design patterns for dealing with this problem? Can anyone help me or point me to some article or anything at all. I would greatly appreciate it.