Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adobe Flex/Actionscript event handling order

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

like image 478
istrasci Avatar asked Oct 18 '10 17:10

istrasci


2 Answers

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));
like image 137
Amarghosh Avatar answered Oct 24 '22 02:10

Amarghosh


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.

like image 44
Montagist Avatar answered Oct 24 '22 02:10

Montagist