Asynchronous calls from Flash
August 21, 2006
In ActionScript it’s possible to ask a fileserver or webserver to go and fetch something for you while ActionScript continues doing work. You can use the LoadVars class for that purpose.
This LoadVars mechanism works as follows
- You define a Event-Handler that implements a function that handles the “onLoad” event
- You create a LoadVars object that’s going to receive the reply to your request.
- You add the Event-Handler to it’s listeners array.
- You create a LoadVars object that’s gonna carry the request.
- You optionally load the LoadVars request object with parameters
- You send the request
- Your ActionScript continues while the LoadVars object is going to the server
- When the receiving LoadVars object is filled with the reply to your request, the onLoad function fires and handles the servers answer.
I encountered a reocurring problem which has a real simple solution that I want to share with you.
The Problem.
There are times when the reply’s to a request are of no interest anymore when they reach your client code. For example consider your browsing through text/picture/sound -items, when you decide to stop waiting for an item (for example because the item takes too long to load), you don’t want it to pop-up later and interrupt the new stuff your doing.
Let’s state, in slightly more formal form, the characteristics of the problem;
You receive a reply to a request that belongs to a possible series of requests to which the rule applies that only the answer to the last request is interesting. How to filter out the noise?
The Solution.
I encountered this problem in many forms and this is my general solution pattern to it. The requests usually all come from the same class. In this class I keep a variable that at all times hold an encoding of the last initiated request. An encoded request variable is inserted in the request LoadVars and returned by the server. Anytime a reply to a request reaches my code I can check whether it’s a reply to the last request (by comparing the received request-encoding with the stored last-request-encoding) to determine whether it’s of interest to me. Encoding the request sounds harder then it is. You could for example, store the id of an item you requested.


July 18, 2008 at 2:50 pm
ch
July 18, 2008 at 2:55 pm
argt? are you allright? long time no see!