WICG / scheduling-apis

APIs for scheduling and controlling prioritized tasks.
https://wicg.github.io/scheduling-apis/
Other
908 stars 45 forks source link

[Suggestion] isTaskPending #56

Open Jamesernator opened 2 years ago

Jamesernator commented 2 years ago

From other proposals we have isFramePending and isInputPending however it would also be useful to have a more general isTaskPending which returns true if any task of higher priority is scheduled to run. This would also include tasks created by the browser, i.e. isInputPending() and isFramePending() would both imply isTaskPending().

The usage would be similar to is{Input,Frame}Pending:

await scheduler.postTask(async function createNoiseTexture() {
    const texture = new Texture(1000, 1000);

    for (let x = 0; i < texture.width; x += 1) {
       for (let y = 0; i < texture.height; x += 1) {
           // Allow any higher priority tasks to run, or frame tasks
           // or input tasks as well (and possibly other browser tasks as well)
           if (scheduler.isTaskPending("background")) {
               await scheduler.yield("background");
           }
           texture.setPixel(x, y, generateNoiseRGB());
       }
    }
}, { priority: "background" });
mmocny commented 1 month ago

Although the symmetry is appealing, I think the current perspective is that "just yield" (at some target priority), is better than "conditionally yield" (but only if you think there is work with higher priority already queued).

The only difference would be if the overhead for the browser to "just yield" and resume is larger than the overhead to "see if it is worth yielding" and then not need to resume. But that is somewhat a premature optimization, that might be better solved directly by scheduler conventions.

(Note also that Chromium DevRel found enough negative examples of isInputPending api that they stopped recommending it)