ActionScript 事件处理程序执行顺序

发布于 2024-07-25 04:57:37 字数 835 浏览 3 评论 0原文

我一直试图理解 ActionScript 事件的实现方式,但我陷入了困境。

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

例如,考虑以下代码:

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 行上的断言可能有时失败,有时成功。

但由于 AS 不是多线程的,因此断言要么总是失败,要么总是成功。 或者,换句话说,事件将以确定性的方式处理。

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

注意:我关心由dispatchEvent分派的事件 - 我意识到“外部分派”事件(网络流量、用户输入、计时器滴答等)的行为有所不同。


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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

原来分手还会想你 2024-08-01 04:57:37

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

例如,如果您要替换 IEventDispatcher 来代替 foo 对象(在本例中,我使用我的 app 对象及其creationComplete 处理程序隐式执行此操作),您会看到:

<?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 文档以获取详细信息, 此处此处),但在这种情况下,事件确实基本上是内联处理的并按优先顺序。 根据文档:

Flex 在中注册事件监听器
addEventListener() 的顺序
方法被调用。 然后 Flex 调用
事件发生时侦听器起作用
按照它们出现的顺序发生
挂号的。 但是,如果您注册
一些内联事件侦听器和一些
使用 addEventListener() 方法,
听众的顺序
调用单个事件可以是
不可预测。

希望有帮助!

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!

过度放纵 2024-08-01 04:57:37

第 4 行将始终在事件处理程序之前执行,因此执行后将始终断言为 false。 函数完成后,处理程序将执行。

你的第2点是正确的看待它的方式。

line 4 will ALWAYS execute before the event handler, so executed will always assert as false. After the function has finished, the handler will execute.

You're point 2 is the correct way to look at it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文