views:

26

answers:

5

As simply as possible, I am trying to find out if events are guaranteed to be handled in the order they are dispatched. Let's say I have the following, simple code:

private function handler1(e:Event):void { .. processing ..}
private function handler2(e:Event):void { .. processing ..}
private function handler3(e:Event):void { .. processing ..}

<SomeComponent myEvent1="handler1(event)" myEvent2="handler2(event)" myEvent3="handler3(event)" ... />

If somewhere in my application (either from within the component itself or another place with the component instantiated), if I dispatch those events in the order of 1, 2, 3, like:

dispatchEvent(new Event('myEvent1'));
dispatchEvent(new Event('myEvent2'));
dispatchEvent(new Event('myEvent3'));

Are those handlers guaranteed to fire in the same order (i.e., handler1, handler2, handler3)...? Anything I search the internet (read: Google) for concerning "adobe event processing order" or anything similar just refers to the event lifecycle. I cannot find anything on this specific nuiance though. Any help is greatly appreciated.

--Ian

A: 

They usually do, but there's no guarantee, so you shouldn't assume they will.

Robert Bak
I get this conflicting answer at the Adobe forum: http://forums.adobe.com/thread/739912?tstart=0
istrasci
A: 

Don't forget about event priorities as well.

Assaf Lavie
A: 

If two events are dispatched one after another as shown in your code, the second event will be dispatched only after all handlers of the first event is executed and the dispatchEvent method is returned.

private function handler_1(e:Event):void {}
private function handler_2(e:Event):void {}

a.addEventListener(Event.TYPE_1, handler_1);
a.addEventListener(Event.TYPE_2, handler_2);

//Different events:
//handler_1 will be called before handler_2
a.dispatchEvent(new Event(Event.TYPE_1));
a.dispatchEvent(new Event(Event.TYPE_2));

But if you've registered two listeners registered with same priority for an event, they are processed in the order in which they were added.

private function handler_1(e:Event):void {}
private function handler_2(e:Event):void {}

a.addEventListener(Event.TYPE_1, handler_1);
a.addEventListener(Event.TYPE_1, handler_2);

/*
  Same event, same priority:
  Called based on the order in which they were added;
  Hence handler_1 is called first.
*/
a.dispatchEvent(new Event(Event.TYPE_1));

You can use the priority parameter of the addEventListener method to enforce the order:

a.addEventListener(Event.TYPE_1, handler_1, false, 0);
a.addEventListener(Event.TYPE_1, handler_2, false, 1);//higher priority

/*
  handler_2 will be invoked before handler_1 
*/
a.dispatchEvent(new Event(Event.TYPE_1));
Amarghosh
A: 

If God had meant us to know the order in which events will occur, he wouldn't have given us callLater()

;)

micapam
A: 

Ditto Robert Bak's response. If you absolutely -need- to make this work for whatever reason, though, you could tag the events with a number that increments by one each time, have your listeners merely store them in a central queue of some kind, then sort them according to the index you've assigned. Probably more work than it's worth though.

Montagist