Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow requesting a minimum time budget #49

Open
cramforce opened this issue Oct 19, 2016 · 8 comments
Open

Allow requesting a minimum time budget #49

cramforce opened this issue Oct 19, 2016 · 8 comments
Milestone

Comments

@cramforce
Copy link

cramforce commented Oct 19, 2016

I my experience every use of requestIdleCallback will eventually have a wrapper like this one:

function requestIdleCallback(win, minimumTimeRemaining, timeout, fn) {
  const before = Date.now();
  win.requestIdleCallback(info => {
    if (info.timeRemaining() < minimumTimeRemaining) {
      const remainingTimeout = timeout - (Date.now() - before);
      if (remainingTimeout <= 0) {
        fn();
      } else {
        requestIdleCallback(win, minimumTimeRemaining, remainingTimeout, fn);
      }
    } else {
      fn();
    }
  });
}

…where rIC is called recursively until a time budget under some requirement is reached. Among other things this requires managing the timeout in user code, because one will typically want a timeout across these calls.

This would be fixed by allowing the caller to pass in something like minimumTimeRemaining into the rIC options object.

@igrigorik igrigorik added this to the Level 2 milestone Oct 27, 2016
@igrigorik
Copy link
Member

@cramforce you probably want to use performance.now() instead of Date.now()... :-)

We did discuss ~this earlier: #28 (comment).

@cramforce
Copy link
Author

@igrigorik the point is that the code is complicated and buggy. This needs to be solved in the platform.

@igrigorik
Copy link
Member

Yep, no disagreement there. I'm just linking to previous discussions on this for context.

@farre
Copy link

farre commented Oct 23, 2017

I see the need for this, but I was thinking of how this would work when a script using minimum budget runs in contexts with different idle profiles. Wouldn't a minimum time budget make a developer less encouraged in splitting up the work a callback performs? This would favor platforms with less idle time, which could be a problem. This is of course the case with rIC already, but could this make it worse?

@cramforce
Copy link
Author

cramforce commented Oct 23, 2017 via email

@farre
Copy link

farre commented Nov 1, 2017

@cramforce
Right, I didn't notice that the timeout parameter to the rIC-wrapper was a "soft" timeout. This would introduce another type of timeout, how do you see the two timeouts interacting if both are present? Would the hard timeout count from 'now' or from when we give up trying to get a sufficiently large value from timeRemaining()?

@cramforce
Copy link
Author

I think that if requestIdleCallback supported something like minimumTimeRemaining, then we would not need another timeout type.

@clelland
Copy link

@shaseley, is this something that we should add to the spec?

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

No branches or pull requests

4 participants