Maybe you didn't read what I worte carefully or maybe I didn't explain it
clearly enough. But note I made a distinction between the dispatcher - the
object that performs the callback, directly or through dispatchEvent - and
the code (let's refer to it as the "first caller") that calls the method
that contains the dispatcher.
Now, what I mean, and I repeat it now, is that if the dispatch is
synchronous or asynchronous changes the stack call: in the first case the
"first caller" will be on the stack; in the second, it won't.
Perhaps this code makes this easier to see (modified from the previously
posted code):
package {
import flash.display.Sprite;
import flash.events.Event;
public class Main extends Sprite {
public function Main():void {
runTest();
}
private function runTest():void {
var callbackTest:Tester = new Tester();
callbackTest.run(callback);
var eventTester:Tester = new Tester();
eventTester.addEventListener(Event.COMPLETE,handleComplete);
eventTester.run(null);
}
private function handleComplete(evt:Event):void {
trace("entering handler");
var stacktrace:String = new Error().getStackTrace();
trace(stacktrace);
trace("returning from handler");
}
private function callback(evt:Event):void {
trace("entering callback");
var stacktrace:String = new Error().getStackTrace();
trace(stacktrace);
trace("returning from callback");
}
}
}
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.utils.setTimeout;
class Tester extends EventDispatcher {
public function Tester() {
}
public function run(callback:Function):void {
setTimeout(function():void {
trace("entering run");
if(callback != null) {
callback(new Event(Event.COMPLETE));
} else {
dispatchEvent(new Event(Event.COMPLETE));
}
trace("returning from run");
trace("------------");
},1);
}
}
Just added a setTimeout to ensure it's asynchronous, but the same will
happen if you load external content, for instance.
As you can see, runTest() (the "first caller") is not in the call stack in
this case (while it was where the dispatch was synchronous):
entering run
entering callback
Error
at Main/callback()[Main.as:28]
at <anonymous>()[Main.as:51]
at Function/http://adobe.com/AS3/2006/builtin::apply()
at SetIntervalTimer/onTimer()
at flash.utils::Timer/_timerDispatch()
at flash.utils::Timer/tick()
returning from callback
returning from run
Cheers
Juan Pablo Califano
2010/7/28 Henrik Andersson <[email protected]>
> Juan Pablo Califano wrote:
>
>> Yes, the dispatcher and the listener are both in the same call stack
>> always,
>> but I think what we were discussing is whether the caller (the original
>> caller, the one that calls the method that will eventually dispatch the
>> event) is in the same callstack as the listener / event handler. That's
>> not
>> always the case, as it depends on whether the event is dispatched
>> synchronously or not.
>>
>
> If you call a method, you will be guaranteed that you will not resume
> execution while the method is executing. If that method dispatches any
> events, you will be in the callstack. There is no other scenarios where the
> method does dispatch the event.
>
> _______________________________________________
> Flashcoders mailing list
> [email protected]
> http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
>
_______________________________________________
Flashcoders mailing list
[email protected]
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders