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

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

问题描述

我一直试图了解 ActionScript 事件的实现方式,但我被卡住了.

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

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

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*.

例如,考虑以下代码:

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

如果 ActionScript 是多线程的,则第 4 行的断言可能有时会失败而其他人会成功.

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

但是因为 AS 不是多线程的,所以断言要么总是失败²,要么总是成功³.或者,换句话说,事件将以一种确定性的方式进行处理.

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.

那么,这个假设(事件被确定性地处理)是否正确?Adobe 是否提供有关此事的任何权威文档?

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

注意:我关心由 dispatchEvent 调度的事件——我意识到外部调度"事件(网络流量、用户输入、计时器滴答等)表现不同.

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.

*:当然,由用户输入或网络流量等不确定因素触发的事件除外.
²:例如,如果事件处理算法是:将新事件推入堆栈,然后不断地将顶部事件从堆栈中弹出,执行它直到它终止,然后继续下一个事件",它总是会失败.
³:如果dispatchEvent 调度的事件在调度后立即处理,它总是会成功.

*: 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.

推荐答案

除非我误会了——在这种情况下,我深表歉意!-- 我不得不不同意其他人的观点:您只需要测试您提交的代码即可看到 x.executed traces 的值每次都为真.

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.

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

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>

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

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 在addEventListener() 的顺序方法被调用.Flex 然后调用当事件发生时侦听器起作用发生的顺序挂号的.但是,如果您注册一些事件侦听器内联和一些使用 addEventListener() 方法,听众的顺序调用单个事件可以不可预测.

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.

希望有帮助!

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

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