hasPendingMacrotasks和hasPendingMicrotasks检查了什么? [英] What does hasPendingMacrotasks and hasPendingMicrotasks check for?

查看:75
本文介绍了hasPendingMacrotasks和hasPendingMicrotasks检查了什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

NgZone中的hasPendingMacrotasks或hasPendingMicrotasks有什么区别?文档似乎缺乏信息。我所知道的是他们返回一个布尔值。但究竟他们​​究竟要检查什么?什么是微观任务?什么被认为是宏任务?

  class NgZone {
static isInAngularZone():boolean
static assertInAngularZone():void
static assertNotInAngularZone():void
构造函数({enableLongStackTrace = false}:any)
run(fn :()=> any):any
runGuarded(fn:()=> any):任何
runOutsideAngular(fn :()=> any):任何
onUnstable:EventEmitter< any>
onMicrotaskEmpty:EventEmitter< any>
onStable:EventEmitter< any>
onError:EventEmitter< any>
isStable:boolean
hasPendingMicrotasks:boolean
hasPendingMacrotasks:boolean
}

我最好的猜测是,micro指的是来自特定类的任务,而macro可能指的是与整个应用程序有关的任务。有人可以验证或确认这个假设吗?或详细说明具体细节?



NgZone文件:





参见




What is the difference between hasPendingMacrotasks or hasPendingMicrotasks within NgZone? The documentation seems to be lacking information. All I know is that they return a boolean. But what exactly are they checking for? What is considered a micro task? And what is considered a macro task?

class NgZone {
  static isInAngularZone() : boolean
  static assertInAngularZone() : void
  static assertNotInAngularZone() : void
  constructor({enableLongStackTrace = false}: any)
  run(fn: () => any) : any
  runGuarded(fn: () => any) : any
  runOutsideAngular(fn: () => any) : any
  onUnstable : EventEmitter<any>
  onMicrotaskEmpty : EventEmitter<any>
  onStable : EventEmitter<any>
  onError : EventEmitter<any>
  isStable : boolean
  hasPendingMicrotasks : boolean
  hasPendingMacrotasks : boolean
}

My best guess is that micro refers to tasks from within a specific class whereas macro probably refers to a task in regards to the whole application. Can anyone verify or confirm this assumption? Or shed some light on the specifics?

NgZone Docs:

https://angular.io/docs/ts/latest/api/core/index/NgZone-class.html#!#hasPendingMicrotasks-anchor

解决方案

There are three kinds of tasks

1) MicroTask:

A microtask is work which will execute as soon as possible on empty stack frame. A microtask is guaranteed to run before host environment performs rendering or I/O operations. A microtask queue must be empty before another MacroTask or EventTask runs.

i.e. Promise.then() executes in microtask

2) MacroTask

MacroTasks are interleaved with rendering and I/O operations of the host environment. They are guaranteed to run at least once or canceled (some can run repeatedly such as setInterval). Macro tasks have an implied execution order.

i.e. setTimeout, setInterval, setImmediate

3) EventTask

EventTasks are similar to macro tasks, but unlike macro tasks they may never run. When an EventTask is run, it pre-empts whatever the next task is the macro task queue. Event tasks do not create a queue.

i.e. user click, mousemove, XHR state change.

Why is it useful to know if any of the tasks are currently being performed?

  • Knowing when a task has executed and a microtask queue is empty allows frameworks to know when it is time to render the UI.

  • Tracking when all scheduled tasks are executed allows a test framework to know when an asynchronous test has completed.

ng_zone.ts

private checkStable() {
  if (this._nesting == 0 && !this._hasPendingMicrotasks && !this._isStable) {
    try {
      this._nesting++;
      this._onMicrotaskEmpty.emit(null);
    } finally {
      this._nesting--;
      if (!this._hasPendingMicrotasks) {
        try {
          this.runOutsideAngular(() => this._onStable.emit(null));
        } finally {
          this._isStable = true;
        }
      }
    }
  }
}

See also

这篇关于hasPendingMacrotasks和hasPendingMicrotasks检查了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆