关于HTML5-7.1.4.2中Event Loop的一些问题

涉及的资源:
Processing model:http://www.whatwg.org/specs/web-apps/current-work/multipage/webappapis.html#processing-model-2 
Spin the event loop:http://www.whatwg.org/specs/web-apps/current-work/multipage/webappapis.html#spin-the-event-loop 
Pause:http://www.whatwg.org/specs/web-apps/current-work/multipage/webappapis.html#pause 
Timers:http://www.whatwg.org/specs/web-apps/current-work/multipage/timers.html 
近期的精力主要集中在对这一块的理解,总得来说Event Loop – Task Queue – Task + Task Source的模型概念是清晰的,但是关于Event Loop的spin和pause这2个概念,有些疑问:
1. 在7.1.4.2 Processing model一章的第1步有提到以下语句
  ignoring tasks whose associated Documents are not fully active.
此处的ignore是一个怎么样的行为,对于这点有2个理解:
A. 不把关联的文档未达到完全激活状态的任务当oldest task来使用。在此情况下,意味着Event Loop可以从Task Queue中取得并非在队首的Task,那么这是不是还能称为一个Queue?
B. 在取到oldest task后,如果其关联的文档未达到完全激活状态,则抛弃之。则这里的“抛弃”会是什么意思,重新放入Task Queue中(出现在队尾),还是直接移除不执行?
2. 根据Google搜索["spin the event loop" site:whatwg.org]得到的结果,在HTML标准中,第12.2.6 The end章节(http://www.whatwg.org/specs/web-apps/current-work/multipage/the-end.html )的第3.1步有提到Spin the event loop,大意是*在Tree Construction完成后,等待(spin)待执行脚本的第1个达到可执行(ready to be parser-executed)状态,随后执行*。除了该章节以外,没有找到其他的Spin the event loop相关的内容,是否可以协助提供一些已知的案例,最好是和javascript脚本交互有关,而非浏览器的内部过程。
3. 关于Timers接口的setTimeout和setInterval这2个函数,在其执行的描述中有提到这样的过程:
  If the method context is a Window object, wait until the Document associated with the method context has been fully active for a further timeout milliseconds (not necessarily consecutively).
假定现只分析context为Window对象的情况,依照上述引用的意思,setTimeout调用后,会*等待*文档完全可用,并随后继续*等待*对应的延时(setTimeout的参数)
从这个行为上来看,与*Spin the event loop*非常相似,因此个人认为即使使用类似以下的句子也完全可以阐述:
  If the method context is a Window object, *spin the event loop* for a timeout miliseconds
上文没有讲述[until the Document has been fully active]这一段,是因为7.1.4.2 Processing model一章中已经提过Task必须在关联文档完全激活后才能使用。
在此如果把*a timeout miliseconds*作为setTimeout形成的一个Task的先决条件(condition goal),则完全可以利用Spin the event loop这一机理来实现。而事实Timers的描述中并没有使用这一行为,而是采用*wait until ...*的描述,这意味着实现Timers的对象必须自行维护一个“等待延迟”的效果,而是利用已经现成的Event Loop的相关机制,这又是出于什么考虑呢?
4. 关于*pause*这一行为,从whatwg上并没有找到会暂停Event Loop执行的实例,如果有相关资料的同学是否可以协助提供一下。
同时,对于以下代码:
  var dom = document.getElementById(‘whatever’);
  dom.style.width = document.body.offsetWidth;
众所周知的,由于这段代码使用了offsetWidth这一属性,有可能引起一次layout,而script必须等待layout完成后才可继续执行。这一现象与标准中的pause这一过程非常相似,同样在pause这一行为的描述中,标准有提到
  While a user agent has a paused task, the corresponding event loop must not run further tasks, and any script in the currently running task must block. 
那么是否可以使用event loop的pause这一行为,来解释这边的同步layout现象。或者说是否可以将script执行与引发同步layout这一问题,作为pause event loop的一个案例来使用?

Received on Thursday, 15 March 2012 08:00:47 UTC