Repository: angular/angular
File: packages/core/src/defer/idle_scheduler.ts (TypeScript)

Code before:
    this.scheduleIdleCallback();
  }

  remove(callback: VoidFunction) {
    this.queue.delete(callback);

    // If the last callback was removed and there is a pending
    // idle callback - cancel it.
    if (this.queue.size === 0) {
      this.cancelIdleCallback();
    }
  }

  private scheduleIdleCallback() {
    if (this.idleId !== null) {
      return;
    }

    const callback = (deadline?: IdleDeadline) => {
      this.cancelIdleCallback();

      for (const callbackFn of this.queue) {
        callbackFn();
        // _tick here is an optimized change detection check and is safe to call here.
        // calling the callback triggers fetching the view, but we also need to run change detection
        // to ensure that the view is updated.
        this.applicationRef._tick();
        this.queue.delete(callbackFn);

        if (deadline && deadline.timeRemaining() === 0 && !deadline.didTimeout) {
          break;
        }
      }

      if (this.queue.size > 0) {
        this.scheduleIdleCallback();
      }
    };
    // Ensure that the callback runs in the NgZone since
    // the `requestIdleCallback` is not currently patched by Zone.js.
    this.idleId = this.idleService.requestOnIdle((deadline) =>
      this.ngZone.run(() => callback(deadline)),
    ) as number;
  }

  private cancelIdleCallback() {
    if (this.idleId !== null) {
      this.idleService.cancelOnIdle(this.idleId);
      this.idleId = null;
    }
  }

Review comment (suggestion):
```suggestion
        // We also account for the time it takes to run change detection
        // for the newly-created view as a part of the same idle callback.
```

Code after:
    this.scheduleIdleCallback();
  }

  remove(callback: VoidFunction) {
    this.queue.delete(callback);

    // If the last callback was removed and there is a pending
    // idle callback - cancel it.
    if (this.queue.size === 0) {
      this.cancelIdleCallback();
    }
  }

  private scheduleIdleCallback() {
    if (this.idleId !== null) {
      return;
    }

    const callback = (deadline?: IdleDeadline) => {
      this.cancelIdleCallback();

      for (const callbackFn of this.queue) {
        callbackFn();
        // _tick here is an optimized change detection check and is safe to call here.
        // We also account for the time it takes to run change detection
        // for the newly-created view as a part of the same idle callback.
        this.applicationRef._tick();
        this.queue.delete(callbackFn);

        if (deadline && deadline.timeRemaining() === 0 && !deadline.didTimeout) {
          break;
        }
      }

      if (this.queue.size > 0) {
        this.scheduleIdleCallback();
      }
    };
    // Ensure that the callback runs in the NgZone since
    // the `requestIdleCallback` is not currently patched by Zone.js.
    this.idleId = this.idleService.requestOnIdle((deadline) =>
      this.ngZone.run(() => callback(deadline)),
    ) as number;
  }

  private cancelIdleCallback() {
    if (this.idleId !== null) {
      this.idleService.cancelOnIdle(this.idleId);
      this.idleId = null;
    }
  }