views:

510

answers:

2

When, in ActionScript, an event is dispatched:

foo.addEventListener("some event", someHandler);
foo.dispatchEvent(new Event("some event"));

At what point are the event handlers executed?

I ask because I caught this at the end of an Adobe developer guide:

Notice that some properties are assigned to the [AsyncToken] after the call to the remote service is made. In a multi-threaded language, there would be a race condition where the result comes back before the token is assigned. This situation is not a problem in ActionScript because the remote call cannot be initiated until the currently executing code finishes.

But I could not find any information on what they meant by "currently executing code".


See also: http://stackoverflow.com/questions/1033661/actionscript-event-handler-execution-order

+4  A: 

Actionscript is a single threaded event driven language. Notice how there are no "main" methods in Actionscript. All code belongs in events, eg. initialization code tends to be placed in response to "creationComplete" events. Once the code in that event handler is run, the next event is executed. So if you did:

private function someOtherHandler():void 
{
    foo.addEventListener("some event", someHandler);
    while(true) { ... spin wheels ... }
}

No other handler would be able to run because the currently executing code (the infinite loop) would never complete.

Note that Flash probably uses multiple threads internally, but that is abstracted from the developer.

CookieOfFortune
Ah, that's exactly what I needed. Thanks for the clarification.
David Wolever
So does that mean that, if I dispatch two events, one after the other, (foo.dispatch(a), foo.dispatch(b)), I can be guaranteed that the handler for event 'a' would be executed before the handler for 'b'? Or is there some amount of nondeterminism when it comes to executing handlers?
David Wolever
No, there is a difference between making the call and getting the result. The handler would be called asynchronously, b could return before a. It just means that if a returns first, all of the code in the a handler would be run before the b handler could run.
Ryan Guill
Thanks for the comment Ryan... Although I'm not sure I understand. When you say "the handler would be called asynchronously", what do you mean? What else would be running at the same time? Or is there something I'm missing?
David Wolever
Or is there some "good" resource for learning the nuts-and-bolds of the implementation? Googling just turns up pages pertaining to /using/ events :(
David Wolever
this is the best place to start to learn about events: http://livedocs.adobe.com/flex/3/html/help.html?content=advdatagrid_04.htmlTo expand on my above answer though, lets say you have two results coming back. Remember that the results are asynchronous, so even though you called a before b, b can and sometimes will return before a. What the above text means is that whichever result comes back first, all of the code in that handler function will be executed before the second handler function is executed. Is that any more clear? Its kinda hard to put into words for sure.
Ryan Guill
What do you mean by "two results coming back"? And, if it's possible for event "b"'s handler to return before event "a"'s handler, what would trigger the execution of either? From some experiments that I've done, it /looks/ like events are handled as soon as they are dispatched, except for external events (like clicks, timer tickets, network traffic, etc), which are put in queue to be executed after the currently executing handler is finished.
David Wolever
Although it "looks" like the events are executed in order, there is no "guarantee" that this would occur.
CookieOfFortune
mmm alright. Still trying to find official documentation on this, though... Maybe I'll open a new question about it. Thanks for the help.
David Wolever
Alright, formalized and cleaned up the question here: http://stackoverflow.com/questions/1033661/actionscript-event-handler-execution-order
David Wolever
+4  A: 

If you call dispatchEvent() in ActionScript, the handlers will execute immediately. The order is determined first by the priority specified when you call addEventListener() and then by the order in which they were added if the priorities are the same. First come first served.

If an event is dispatched internally from Flash Player, such as Event.COMPLETE from a URLLoader instance or anything else that requires network communication, it will not be dispatched while ActionScript is running. It gets queued up for later. I imagine this is precisely to avoid the race condition described in the documentation. I believe it has been observed that "later" is the next frame, but it could happen after all the other ActionScript for the current frame has run.

joshtynjala