ActionScript 事件处理程序执行顺序
我一直试图理解 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
除非我误会了——在这种情况下我深表歉意! -- 我必须不同意其他人的观点:您只需要测试您提交的代码即可看到 x.executed 的值每次都跟踪 true。
例如,如果您要替换 IEventDispatcher 来代替 foo 对象(在本例中,我使用我的 app 对象及其creationComplete 处理程序隐式执行此操作),您会看到:
当然,有多种控制方法事件处理顺序(使用 addEventListener 的优先级参数),以及显示列表上对象的事件传播的各个阶段(例如,捕获、定位、冒泡 - 请参阅 Flex 文档以获取详细信息, 此处和此处),但在这种情况下,事件确实基本上是内联处理的并按优先顺序。 根据文档:
希望有帮助!
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:
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:
Hope that helps!
第 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.