ActionScript事件处理程序执行顺序 [英] ActionScript event handler execution order

查看:189
本文介绍了ActionScript事件处理程序执行顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在试图了解ActionScript的事件的实现方式,但是我被困住了。



我知道AS是单线程的,这意味着只有一个事件处理程序将一次执行,也意味着处理程序将以确定的顺序执行*。



例如,考虑以下代码:

  1:var x = {execution:false}; 
2:foo.addEventListener(execute,function(){x.executed = true;});
3:foo.dispatchEvent(new Event(execute));
4:assert(x.executed);

如果ActionScript是多线程的,那么第4行的断言有可能有时会失败,但是由于AS不是多线程的,因此断言将始终始终失败或永远是 或者换句话说,事件将以确定的方式处理。


所以,这个假设? Adobe提供任何关于此事的明确文件?


注意:我只是通过 dispatchEvent - 我意识到外部调度事件(网络流量,用户输入,计时器滴答等)的行为不同。






*:除了用户输入或网络流量等非确定性事件触发的事件外,还有一个例外。

²:总是会失败例如,如果事件处理算法是:将新事件推送到堆栈上,然后不断地从堆栈中弹出顶部事件,执行它直到它终止,然后继续下一个事件。

³:如果调度由 dispatchEvent 分派的事件一经处理,它们将始终成功。

解决方案

除非我有误解 - 在这种情况下我道歉! - 我不同意其他人:您只需要测试您提交的代码,以便每次都会看到x.executed追踪的值为true。



例如如果代替你的foo对象,你要替换一个IEventDispatcher(在这种情况下,我隐含地这样做,我的应用程序对象和它的creationComplete处理程序),你会看到:

 <?xml version =1.0encoding =utf-8?> 
< mx:WindowedApplication xmlns:mx =http://www.adobe.com/2006/mxmllayout =absolutecreationComplete =onCreationComplete()>

< mx:脚本>
<![CDATA [

private function onCreationComplete():void
{
var x = {execution:false};
addEventListener(execute,function(){x.executed = true;});
trace(x.executed); // false
dispatchEvent(new Event(execute));
trace(x.executed); // true
}

]]>
< / mx:脚本>

< / mx:WindowedApplication>

当然,有些方法可以控制事件处理订单(使用addEventListener的优先级参数)以及显示列表上的对象的事件传播的各个阶段(例如,捕获,定位,冒泡 - 有关详细信息,请参阅Flex文档,这里这里),但在这种情况下,事件确实基本上是内联和优先顺序处理的。根据文档:


Flex在
顺序中注册事件监听器,其中addEventListener()
方法是调用。当事件
按照
注册的顺序发生时,Flex会调用
监听器函数。但是,如果您使用addEventListener()方法在线注册
某些事件监听器,另外一些

调用单个事件的监听器
的顺序可以是
不可预测。


希望有帮助!


I have been trying to understand the way ActionScript's events are implemented, but I'm stuck.

I know that AS is single threaded, which means that only one event handler will be executing at a time, and also means that handlers will be executed in a deterministic order*.

For example, consider the following code:

1: var x = {executed: false};
2: foo.addEventListener("execute", function(){ x.executed = true; });
3: foo.dispatchEvent(new Event("execute"));
4: assert(x.executed);

If ActionScript was multi-threaded, it would be possible that the assertion on line 4 could fail sometimes and succeed others.

But since AS is not multi-threaded, it stands to reason that the assertion will either always fail² or always succeed³. Or, in other words, events will be handled in a deterministic way.

So, is this assumption (that events hare handled deterministically) correct? Does Adobe provide any definitive documentation on the matter?

Note: I am only concerned here with events dispatched by dispatchEvent – I realize that "externally dispatched" events (network traffic, user input, timers ticking, etc) behave differently.


*: with the exception, of course, for events triggered by nondeterministic things like user input or network traffic.
²: it would always fail if, for example, if the event handling algorithm was: "push new events onto a stack, then continuously pop the top event off the stack, execute it until it terminates, then go on to the next event".
³: it would always succeed if events dispatched by dispatchEvent were handled as soon as they were dispatched.

解决方案

Unless I'm misunderstanding -- in which case I do apologize! -- I have to disagree with the others: you need only test the code you've submitted to see the value of x.executed traces true every time.

For example, if, in place of your foo object, you were to substitute an IEventDispatcher (in this case I do so implicitly, with my app object and its creationComplete handler), you'd see:

<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="onCreationComplete()">

    <mx:Script>
        <![CDATA[

            private function onCreationComplete():void
            {
                var x = {executed: false};
                addEventListener("execute", function() { x.executed = true; });
                trace(x.executed); // false
                dispatchEvent(new Event("execute"));
                trace(x.executed); // true
            }

        ]]>
    </mx:Script>

</mx:WindowedApplication>

Of course, there are ways of controlling event-handling order (using the priority argument of addEventListener), and various phases of event propagation for objects on the display list (e.g., capturing, targeting, bubbling -- see the Flex docs for detailed information, here and here), but in this kind of situation, events are indeed handled essentially inline and in priority order. According to the docs:

Flex registers event listeners in the order in which the addEventListener() methods are called. Flex then calls the listener functions when the event occurs in the order in which they were registered. However, if you register some event listeners inline and some with the addEventListener() method, the order in which the listeners are called for a single event can be unpredictable.

Hope that helps!

这篇关于ActionScript事件处理程序执行顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆