简体   繁体   English

javascript 代码中的微任务作为宏任务

[英]Microtask as macrotask in javascript code

I've found this note in whatwg spec:我在 whatwg 规范中找到了这个注释

It is possible for a microtask to be moved to a regular task queue, if, during its initial execution, it spins the event loop.如果在初始执行期间,它会旋转事件循环,则可以将微任务移动到常规任务队列。 This is the only case in which the source, document, and script evaluation environment settings object set of the microtask are consulted;这是唯一参考微任务的源、文档和脚本评估环境设置object集的情况; they are ignored by the perform a microtask checkpoint algorithm.它们被执行微任务检查点算法忽略。

What is an example of it, that expresses this quote?什么是它的一个例子,它表达了这句话? How is it possible to move a microtask from the microtask stack to the macrotask stack and perform it like a macrotask?如何将微任务从微任务堆栈移动到宏任务堆栈并像执行宏任务一样执行?

This was here for showModalDialog , which used to load a document in a modal, blocking the initial document until this modal is closed... For this to happen the UA had to "spin the event loop" until the modal's document is closed.这是用于showModalDialog的,它用于在模态中加载文档,阻塞初始文档,直到此模态关闭......为此,UA 必须“旋转事件循环”直到模态的文档关闭。 This "spin the event loop" macro does convert the microtask to a "simple" task.这个“旋转事件循环”宏确实将微任务转换为“简单”任务。

The showModalDialog() method has been removed from the specs but this remained since it can also be used in other modals or to allow "long tasks" to be short circuited by the UA, so that they can update the rendering and eg show a message to the user about the long script. showModalDialog()方法已从规范中删除,但这仍然存在,因为它也可用于其他模式或允许“长任务”被 UA 短路,以便它们可以更新渲染并显示消息向用户介绍长脚本。
This is for instance advised in a note about pausing , called by all modals like alert() , prompt() , etc.:例如,在有关暂停的注释中建议了这一点,由所有模态调用,如alert()prompt()等:

Pausing is highly detrimental to the user experience, especially in scenarios where a single event loop is shared among multiple documents.暂停对用户体验非常不利,尤其是在多个文档之间共享单个事件循环的情况下。 User agents are encouraged to experiment with alternatives to pausing, such as spinning the event loop or even simply proceeding without any kind of suspended execution at all, insofar as it is possible to do so while preserving compatibility with existing content.鼓励用户代理尝试暂停的替代方法,例如旋转事件循环,甚至在没有任何暂停执行的情况下简单地继续,只要可以这样做,同时保持与现有内容的兼容性。

You also have that in some browsers with infinite loops, where they'll pause the script and show a message along the lines of " A script on this page is taking too long, do you want to wait or kill it with fire? ".在某些具有无限循环的浏览器中也有这种情况,它们会暂停脚本并显示一条消息,如“此页面上的脚本花费太长时间,您要等待还是用火杀死它? ”。

As an example, in Firefox, both cases will trigger a rendering update when the prompt is shown to the user, ie they do spin the event loop .例如,在 Firefox 中,两种情况都会在向用户显示提示时触发渲染更新,即它们确实会旋转事件循环 So if the "pause" was triggered from inside of a microtask, they should move it to a macrotask when it's resumed.因此,如果“暂停”是从微任务内部触发的,他们应该在恢复时将其移至宏任务。

However IIRC, they actually use a custom version of spin the event loop that only allow for the rendering phase (without callbacks) and nothing more.然而 IIRC,他们实际上使用了一个自定义版本的旋转事件循环,它只允许渲染阶段(没有回调),仅此而已。 And the microtask isn't really converted to a task even there. 即使在那里,微任务也没有真正转换为任务。
There are active discussions , where it is planned to "remove this concept" of reentering microtasks, since it's actually not really used by implementers.积极的讨论,计划“删除这个概念”重新进入微任务,因为它实际上并没有被实施者真正使用。 And even if it was, I'm not sure that'd be observable to us.即使是这样,我也不确定我们是否可以观察到。

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

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