简体   繁体   English

在Dart中,Dart2JS和Dart VM之间的事件队列/事件循环如何不同?

[英]In Dart, how does the event queue/event loop differ between Dart2JS and the Dart VM?

Is there any difference in the order of execution? 执行顺序有什么不同吗?

Or does the event queue/loop work differently in JavaScript than Dart? 或者事件队列/循环在JavaScript中的工作方式与Dart不同?

DOM events are handled by the Blink. DOM事件由Blink处理。 These events should therefore be handled the same way. 因此,应以相同的方式处理这些事件。 In JavaScript there is no other event loop (afaik). 在JavaScript中没有其他事件循环(afaik)。 For example, a common pattern for yielding execution is window.setTimeout(0, continuation) . 例如,产生执行的常见模式是window.setTimeout(0, continuation)

In Dart we also have asynchronous events that are handled by dart:async . 在Dart中,我们还有由dart:async处理的异步事件。 There we can distinguish between instants and cycles . 在那里我们可以区分瞬间周期 An instant is composed of one or more cycles. 瞬间由一个或多个周期组成。 An instant executes all its cycles until there is none left and then moves to the next instant. 瞬间执行其所有循环,直到没有剩余,然后移动到下一个瞬间。 DOM events are at the same level as instants. DOM事件与瞬间处于同一级别。 That is, a DOM event will never be interleaved with cycles of the same instant. 也就是说,DOM事件永远不会与同一时刻的循环交错。 [^1] (This also means that piling up cycles within the same instant can starve the DOM.) [^ 1](这也意味着在同一时刻内堆积周期会使DOM饿死。)

runAsync queues a new cycle. runAsync将新周期排队。 Timer.run queues a new instant. Timer.run排队新的瞬间。

Futures and Streams use cycles to queue events that can be executed immediately. Futures和Streams使用循环对可以立即执行的事件进行排队。 In the following example both then s will be scheduled within the same instant and therefore run before any DOM event has the chance to interfere. 在下面的例子都then旨意要在同一时刻安排,因此运行之前任何DOM事件都无权干涉的机会。

var future = new Future.value(499);
future.then(print);
future.then(print);

There are other small differences between Dart and JavaScript: Dart does not have a minimal sleep time for Timer runs. Dart和JavaScript之间还有其他一些细微差别:Dart没有最小的Timer运行休眠时间。 In JavaScript window.setTimeout is not allowed to execute the computation before 5ms. 在JavaScript中, window.setTimeout不允许在5ms之前执行计算。 (This is due to unfortunate historic circumstances). (这是由于不幸的历史情况)。 Dart doesn't have this restriction. Dart没有这个限制。 [^2] [^ 2]

The VM's Timer functionality is not based on DOM, and has its own implementation. VM的Timer功能不是基于DOM,而是有自己的实现。 As of May 2013 the ordering of scheduled timer callbacks is not consistent between JavaScript (and thus dart2js) and the VM. 截至2013年5月,计划的计时器回调的顺序在JavaScript(以及dart2js)和VM之间不一致。 (I'm not sure about Dartium, but I believe it uses the DOM's version and is thus similar to JavaScript). (我不确定Dartium,但我相信它使用DOM的版本,因此类似于JavaScript)。

We are currently discussing changes to the library to guarantee that Timer events are executed in the "correct" order and not just after the waiting-time has elapsed. 我们目前正在讨论对库的更改,以确保Timer事件以“正确”的顺序执行,而不是仅在等待时间结束后执行。


[^1] This is not correctly implemented. [^ 1]这未正确实施。 As of May 2013 ever asynchronous operation is built on top of Timer . 截至2013年5月,异步操作建立在Timer之上。

[^2] This, too, is not done yet (May 2013). [^ 2]这也没有完成(2013年5月)。 I'm not sure about Dartium, but dart2js currently still uses window.setTimeout . 我不确定Dartium,但是dart2js目前仍然使用window.setTimeout It will eventually switch to newer primitives (on browsers that support it) that allow for more precise timeouts. 它最终将切换到更新的原语(在支持它的浏览器上),允许更精确的超时。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM