Reply to comment

What is AsyncDispatcher?

While poking around in the Flex SDK source code I came across an undocumented class called AsyncDispacther under \Flex Builder 3 Plug-in\sdks\3.2.0\frameworks\projects\rpc\src\mx\rpc and it reminded me of some of the tricks we used to use before Flash Player 9 to overcome problems we encountered.

AsyncDispatcher is a way to delay the calling of a method for a specific time interval. Its constructor is below.

public function AsyncDispatcher(method:Function, args:Array, delay:Number)
{
super();
_method = method;
_args = args;
_timer = new Timer(delay);
_timer.addEventListener(TimerEvent.TIMER, timerEventHandler);
_timer.start();
}

It takes as parameters a function, the functions arguments and a delay in milliseconds. It then creates a Timer and waits the specified delay to call the function in the timerEventHandler method.

Back before Flash Player 9 when the Flash Player wasn’t as fast you could see performance degradation if you tried to do too much in a single frame. It was common in those days to use tricks like this to delay processing if for instance your UI was rendering or an animation was running. This is basically what the callLater method in UIComponent does. It delays a method call for one frame. But what if you are not extending UIComponent? This class would let you do the same thing from say a model class or a responder class. This class could be used in much the same way to delay invoking some method until it was safe to do so.

One improvement I would make to this class would be the ability to call the method a specific number of times or indefinitely. Since Timer takes a ‘repeatCount’ parameter this would be as simple as passing an additional argument in the constructor. This ‘repeatCount’ parameter could default to 1. I worked on a project long before before WebORB was free and for a client that didn’t want to pay $10,000 per CPU for it and before the Flex SDK was open source so the source for classes like this were not included. We needed a way to update all open clients whenever a change was made to the database. I created a class very similar to this to call a script on the server every 4 minutes to check for updates. It worked surprisingly well.

As a note - because this class is undocumented doesn’t mean you shouldn’t use it. It is used by the RPC classes in the framework.

Reply

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options