Skip to content
This repository was archived by the owner on Feb 26, 2024. It is now read-only.
This repository was archived by the owner on Feb 26, 2024. It is now read-only.

[Discuss]should onHasTask being notified by periodMacroTask #1017

Closed

Description

@JiaLiPassion

based on #999, current setInterval 's behavior is

  • when call setInterval will schedule a macroTask with isPeriodic=true, and trigger onHasTask with macroTask count +1.
  • and when interval callback is invoked, onHasTask will not be invoked because it is periodic.
  • when cancelInterval is called, will trigger onHasTask with macroTask count -1.

It is the correct behavior.

But in onHasTask callback, we can only get

type HasTaskState = {
  microTask: boolean; macroTask: boolean; eventTask: boolean; change: TaskType;
};

macroTask is true or false, but we can not get whether the macroTask isPeriodic or not.
In some case, we may want to consider

  • all microTasks
  • all non-periodic macroTasks

are stable.

For example, in some application, we start a interval to polling from server at background, and basically we should ignore this interval task when we want to check the application is stable or not. otherwise the application will not stable at all.

@mhevery , do you think we need to provide something like

type HasTaskState = {
  microTask: boolean; 
  macroTask: boolean; 
  eventTask: boolean;
  change: TaskType;
  isAllMacroTasksPeriodic: boolean;
};

to provide isPeriodic information and not making breaking changes?

Please review, thank you!

Activity

  1. mhevery commented on Feb 12, 2018

    @mhevery
    Contributor

    What is the need to know if there are periodic tasks? I think the answers should be just to run the periodicTask outside the current zone so that it does not show up here.

  2. JiaLiPassion commented on Feb 12, 2018

    @JiaLiPassion
    CollaboratorAuthor

    @mhevery , the reason about this question is angular/angular#20970,
    service worker will not register because ngZone will never stable when setInetrval is called.

    run setInterval outsideAngular will cause the callback outside of ngZone, so the walk around is

    this.ngZone.runOutsideAngular(() => {
      setInterval(() => {
        this.ngZone.run(() => {
            // do something
        });
      }, 1000);
    });

    to make the setInterval not trigger onHasTask of ngZone but can also run callback inside ngZone.

    So I think maybe a isPeriod option may help? such as

    this.ngZone.isStable.subscrible((taskStates) => {
      if (taskStates.isAllPeriodTasks || !(taskStates.hasPendingMicroTasks || taskState.hasPendingMacroTasks) {
        // consider ngZone is stable
      }
    });

    or we can provide some helper method to make this simple wrap version(no task) of setInterval more easier

    this.ngZone.runOutsideAngular(() => {
      setInterval(() => {
        this.ngZone.run(() => {
            // do something
        });
      }, 1000);
    });

    something like setIntervalWrap()...?

  3. mhevery commented on Feb 13, 2018

    @mhevery
    Contributor

    I feel that your proposal is a fix to a specific issue of angular/angular#20970 rather than a general need. setInterval is no different from work = () => setInterval(work) and in that case the issue would persist. If there is outstanding work than zone will point this out. To special case long lived work just feels weird since it may be that that work is needed. For example animation may temporarily create setInterval only to cancel it later.

  4. JiaLiPassion commented on Feb 14, 2018

    @JiaLiPassion
    CollaboratorAuthor

    @mhevery , got it, thank you. I agree this is not a general need, and should not be handled in zone.js. I will try to add it to documentation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions