w3c / requestidlecallback

Cooperative Scheduling of Background Tasks
https://w3c.github.io/requestidlecallback/
Other
50 stars 19 forks source link

The invoke idle callbacks algorithm should check if list of runnable idle callbacks is empty or not. #83

Closed rniwa closed 4 years ago

rniwa commented 4 years ago

In the following example, the invoke idle callbacks algorithm would be called with an empty list of runnable idle callbacks for the iframe's content window.

const iframe = document.createElement('iframe');
const logs = [];

iframe.onload = () => {
    requestIdleCallback(() => {
        logs.push('A');
        iframe.contentWindow.cancelIdleCallback(b1);
    });

    const b1 = iframe.contentWindow.requestIdleCallback(() => logs.push('B'));
}
document.body.appendChild(iframe);

In this example, the following events would take place:

  1. Callback A is added to the list of idle request callbacks, and the task to start an idle period algorithm (SIPA-A) is queued. Currently schedule tasks are: [SIPA-A].
  2. Callback B is added to the list of idle request callbacks, and the task to start an idle period algorithm (SIPA-B) is queued. Currently schedule tasks are: [SIPA-A, SIPA-B].
  3. SIPA-A runs. A is moved from top-level window's list of idle request callbacks to the list of runnable idle callbacks, and a new task to invoke idle callbacks (IIC-A) is scheduled. Currently schedule tasks are: [SIPA-B, IIC-A].
  4. SIPA-B runs. B is moved from iframe's content window's list of idle request callbacks to the list of runnable idle callbacks, and a new task to invoke idle callbacks (IIC-B) is scheduled. Currently schedule tasks are: [IIC-A, IIC-B].
  5. IIC-A runs. The callback A is called and B is removed from the list of runnable idle callbacks of iframe's content window. Currently schedule tasks are: [IIC-B].
  6. IIC-B runs. The step to pop the top callback from window's list of runnable idle callbacks fails because there is nothing left in the list of runnable idle callbacks of the iframe's content window.
rmcilroy commented 4 years ago

Yeah this seems like a spec bug. Should be easy enough to fix by adding "and the window's list of runnable idle callbacks is not empty:" to step 3 of the invoke idle callbacks algorithm, WDYT?

rniwa commented 4 years ago

Yeah, that would work.