18 Replies Latest reply on May 21, 2011 3:02 AM by pauland

    Events and functions order question

    FM_Flame Level 1

      Hi,

       

      sorry for the stupid question but I am working on something big and I would hate a small rock to thow me off in the end.

       

      So here it is:

       

      I know how functions work -> If you have:

       

      function function1(){

           function3();

      }

      function function2(){

      }

      function function3(){

      }

       

      function1();

      function2();

       

      First will run function1 then function3 and then function2. So it's simple first runs the function1 + all of it's nested functions again starting from the top to the bottom and then function2 etc.

       

      Ok so now I have this situation:

       

      If you have a component INNER which is in component MAIN. If you addEventListener(FlexEvent.CREATION_COMPLETE,test) inside the INNER (class in the construct function in my case) and also add the same event listener again to INNER but from the MAIN component like:

       

      public function main_creationCompleteHandler(event:FlexEvent):void{

           inner.addEventListener(FlexEvent.CREATION_COMPLETE,test);

           addElement(inner);

      }

       

      Then you will have 2 event listeners for the same event. First will run the one that is registered to the INNER component and then it will run in the MAIN component.

      Now if I define many many functions in INNER creationCompleteHandler, will they be all 100% complete before the creationCompleteHandler in MAIN runs?

       

      As much as I've tested It looks like every processing must be 100% done in the INNER creationCompleteHandler before it moves on to the MAIN creationCompleteHandler.

       

      Can someone confirm that flash/flex works like that? Thanks!

        • 1. Re: Events and functions order question
          Peter Blazejewicz Level 4

          Hello,

           

          that depends on what framework you're using. For example it is mostly true for Flex SDK based components (mainly: containers). I cannot find reference in Flex 4.5 SDK but here is older one (for mx components):

          http://livedocs.adobe.com/flex/3/html/help.html?content=containers_intro_3.html

          (see section about creationComplete):

          creationComplete Dispatched when the component, and all of its child components, and all of their children, and so on have been created, laid out, and are visible

          But that not always have to be true as there are deferred creation/lazy creation feature in Flex (containers have creation policy).

          So if you plan to depends on events order you need to consider what events you'll be relying on - and for what purpose I think,

           

          regards,

          Peter

          • 2. Re: Events and functions order question
            FM_Flame Level 1

            Hi,

             

            thanks for the reply.

             

            I have specified my question to be about the same eventListener - CREATION_COMPLETE registered twice: once inside the component that is created and second time in its container. Both times it is for the same component. First fires the event in the component itself and then in its container.

             

            The question is will all functions/operations defined in the first event handler be 100% complete by the time the second event fires?

            • 3. Re: Events and functions order question
              pauland Level 4

              I think if you're messing around with inner classes and needing seperate event handlers, then you have a needlessly complicated architecture.

              • 4. Re: Events and functions order question
                FM_Flame Level 1

                Guys I really don't understand why you have difficulties imagine this simple example. So here it is with a test case, the question is commented out:

                 

                <?xml version="1.0" encoding="utf-8"?>
                <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
                               xmlns:s="library://ns.adobe.com/flex/spark"
                               xmlns:mx="library://ns.adobe.com/flex/mx"
                               xmlns:local="*">

                 

                    <fx:Script>
                        <![CDATA[
                            import mx.events.FlexEvent;

                 

                            protected function customGroup_creationCompleteHandler(event:FlexEvent):void
                            {
                                // functions here - will these run only after all the functions/processing are 100% complete
                                // in the first creationCompleteHanlder which is in the customGroup component?
                            }

                 

                        ]]>
                    </fx:Script>
                   
                    <local:CustomGroup id="customGroup" creationComplete="customGroup_creationCompleteHandler(event)" />
                   
                </s:Application>

                 

                <?xml version="1.0" encoding="utf-8"?>
                <s:Group xmlns:fx="http://ns.adobe.com/mxml/2009"
                         xmlns:s="library://ns.adobe.com/flex/spark"
                         xmlns:mx="library://ns.adobe.com/flex/mx" width="400" height="300" creationComplete="group1_creationCompleteHandler(event)">
                    <fx:Script>
                        <![CDATA[
                            import mx.events.FlexEvent;

                 

                            protected function group1_creationCompleteHandler(event:FlexEvent):void
                            {
                                // functions here - will these be 100% complete before the second
                                // creationCompleteHandler runs which is in the container of this component?
                            }

                 

                        ]]>
                    </fx:Script>
                  
                </s:Group>

                • 5. Re: Events and functions order question
                  SteveDepp

                  Using your example, there is no way to tell which will execute first. You have registered two event listener functions for the same event. Both will run but the order cannot be guaranteed.

                   

                  If you would like a guarantee, create a custom event and send it at the end of the components function.

                  • 6. Re: Events and functions order question
                    pauland Level 4

                    Generally, event handlers are called according to the order in which they are added, though it's poor practice to build applications that rely on that ordering. I have yet to see anyone else add a creation complete handler to an individual child. Why would you?

                    • 7. Re: Events and functions order question
                      FM_Flame Level 1

                      That's why I am asking, to know if it is certain that all functions/operations will be 100% done in the first listener handler before continuing with the second one or I should definitely create a custom event and dispatch it in the end. If it IS certain I won't bother making custom events and dispatch them always at the end etc, if it is not I will have to... BUT that's why I am asking, someone to confirm for sure if it certain or not. So are you ? (I am using Flex 4)

                       

                      About the example. Yes I do have an explanation. My architecture is much more complicated than that cause it uses modules with components in them and data is passed from application -> module -> component. So basicly I have 3 ways of making sure that all functions will run in a precise order:

                      1) with the custom event dispatched when the component is created and all the functions I want are executed.

                      2) using interface for the module and defining 2 functions. One to run when the module is created for the first time to build the components logic. And second function to populate different data to that compontents which will run every time.


                      Note: for these 2 options I am certain they will work. I am not certian for this one and that's why I am asking someone who KNOWS FOR SURE to let me know:

                       

                      3) Currently it's like this. When the module is created it builds the component logic on CREATION_COMPLETE - that is inside the module. On the same event but this time on the container of the module when it's created it calls a function on it to populate the data. This function will also be called by buttons etc.

                      The point here is WILL this function that is on the second CREATION COMPLETE event run on the module after all the logic has been setup by the first CREATION_COMPLETE event. Or I should use 1) and 2) options.

                       

                      Please don't guess, I am guessing too and I even tested it but could get it to fail so I am asking if someone is certain if 3) option would work the way I want it to. Thanks!

                      • 8. Re: Events and functions order question
                        ssardar Level 1

                        You know what's a funny word?

                         

                        Asynchronous.

                        • 9. Re: Events and functions order question
                          Flex harUI Adobe Employee

                          Yes, that is the way it works.

                          • 10. Re: Events and functions order question
                            FM_Flame Level 1

                            Haha Yeah I know... that's the reason I asked this question here. Cause I was not sure if it applies to this case. I thought it does but then I tested some random functions and math calculations on the first event and no matter what the second event handler fired always after everything in the first one has been executed.

                             

                            Can you tell for sure if Asynchronous applies for this case and I should use options 1) or 2) ?

                            • 11. Re: Events and functions order question
                              FM_Flame Level 1

                              Alex, could you please  clarify how it works ? I couldn't get what you are referring to. Thanks!

                              • 12. Re: Events and functions order question
                                FM_Flame Level 1

                                Can I use option 3) safely or I should go for 1) or 2) ?

                                • 13. Re: Events and functions order question
                                  pauland Level 4

                                  Don't rely on the order that handlers are called for a particular event. PERIOD.

                                   

                                  If the order is important, then it means you most likely need TWO differrent types of event to sequence the stages.

                                   

                                  In the container you should really be listening to a custom event (MODULE_READY or CHILD_READY or something) generated from the child after it has processed the handling of create complete.

                                   

                                  Generally speaking we CAN UNDERSTAND what you are writing WITHOUT USING CAPITALS.

                                  • 14. Re: Events and functions order question
                                    ssardar Level 1

                                    Flame sorry about my "smart" answer before

                                     

                                    I've found 2 ways to get things to happen in a predictable and repeatable order.

                                     

                                    chain the events, have one dispatch the event of the next

                                     

                                    or use the flash.utils.timer

                                     

                                     

                                    There's an example here

                                    http://livedocs.adobe.com/flex/3/html/help.html?content=08_Dates_and_times_4.html

                                     

                                     

                                    I generally got accustomed to using the timer in a situation where I write to a db and a successive read happens before the write is complete.  Works pretty well so far.

                                    • 15. Re: Events and functions order question
                                      Flex harUI Adobe Employee

                                      Today, the events are dispatched to the listeners in the order they are

                                      attached.  I went digging through the spec, however, and found that the W3C

                                      doesn't guarantee order, and neither does our langref, so I suppose it could

                                      change someday, but would require a swf version change so you should be

                                      protected.

                                       

                                      We extended the W3C spec to add priority values to the listeners.  You can't

                                      set them from MXML attributes, but that is the way we guarantee order in SDK

                                      code.

                                       

                                      -Alex

                                      • 16. Re: Events and functions order question
                                        FM_Flame Level 1

                                        Hi Alex, guys,

                                         

                                        so I would like to summarize the question and if you could just say yes or no I even guided what is yes so there is no confusion

                                         

                                        Here are 2 scenarios:

                                        1) 2 Listeners are added for the same even for example CREATION_COMPLETE - one to the component and one to the container.

                                        This will work like this:

                                        a) Component -> creationCompleteHandler starts executing it's functions/operations

                                        b) Container -> creationCompleteHandler starts executing it's functions/operations

                                         

                                        The question here is: Will a) be 100% complete before before flash moves to b) or b) could happen sometime while a) is being executed?

                                        Note: Yes here means a) will be 100% complete bofore b) occurs.

                                         

                                        2) For the second scenario lets take 2 different events like INITIALIZE and CREATION_COMPLETE from the FlexEvent and add listener for both in the component only this time so we don't complicate things.

                                        This will work like this:

                                        a) Component -> initializeHandler starts executing it's functions/operations

                                        b) Component -> creationCompleteHandler starts executing it's functions/operations

                                         

                                        The question here is the same: Will a) be 100% complete before before flash moves to b) or b) could happen sometime while a) is being executed?

                                        Note: Yes here means a) will be 100% complete bofore b) occurs.

                                         

                                        Thanks a lot

                                        • 17. Re: Events and functions order question
                                          pauland Level 4

                                          FM_Flame wrote:

                                           

                                          Hi Alex, guys,

                                           

                                          so I would like to summarize the question and if you could just say yes or no I even guided what is yes so there is no confusion

                                           

                                          Here are 2 scenarios:

                                          1) 2 Listeners are added for the same even for example CREATION_COMPLETE - one to the component and one to the container.

                                          This will work like this:

                                          a) Component -> creationCompleteHandler starts executing it's functions/operations

                                          b) Container -> creationCompleteHandler starts executing it's functions/operations

                                           

                                          The question here is: Will a) be 100% complete before before flash moves to b) or b) could happen sometime while a) is being executed?

                                          Note: Yes here means a) will be 100% complete bofore b) occurs.

                                           

                                          2) For the second scenario lets take 2 different events like INITIALIZE and CREATION_COMPLETE from the FlexEvent and add listener for both in the component only this time so we don't complicate things.

                                          This will work like this:

                                          a) Component -> initializeHandler starts executing it's functions/operations

                                          b) Component -> creationCompleteHandler starts executing it's functions/operations

                                           

                                          The question here is the same: Will a) be 100% complete before before flash moves to b) or b) could happen sometime while a) is being executed?

                                          Note: Yes here means a) will be 100% complete bofore b) occurs.

                                           

                                          Thanks a lot

                                           

                                          1) Flash is currently single-threaded, so one handler will complete before another handler for the same event completes. The order of handling is not guaranteed and since your event handlers are listening for the same event attached to the same object, it is foolhardy to write any code that depends on the order in which these handlers are called. Adobe are working on multi-threading the flash plugin, so in the future any order that exists now may be differrent in the future and any processing that depends on one handler completing before another is called may also fail in the future because a multi-threaded player could activate both handlers concurrently.

                                           

                                          It's nice to have yes/no answers but only if you want to write poor code that may work, by accident, until something changes that you have no control over.

                                           

                                          Do not write code that depends on the order that event handlers are called for the same event.

                                           

                                          2) initialize happens before creation complete. The answer is yes.

                                          • 18. Re: Events and functions order question
                                            FM_Flame Level 1

                                            Thanks a lot