zephyr.js
zephyr.js copied to clipboard
Support something like requestIdleCallback
Maybe it makes sense to support something like
https://developers.google.com/web/updates/2015/08/using-requestidlecallback
on the global object.
We could probably do something analogous at least. Since we don't have a "user" we can't really detect when the "user is inactive". But one sign of how active the system is, is to look at the depth of the callback ring buffer. For example, if it's empty, that's probably a good time to run an idle callback. So maybe if we get to the end of the main loop we could check for whether the callback list is empty, and if so run the idle.
But it's probably trivial to write an app that never has an empty callback, in which case the idle would never get called. And there are maybe other cases where a lot of work is done in timers / service routines and so the empty callbacks aren't a good indicator, and the idle would run too much.
Another passing thought is that the idle callback (and any callbacks it generates, I suppose) could be marked in the ringbuf as "low priority" and be skipped over at some ratio. For example, always run 2, 3, 5, 10, 20, or some number of normal callbacks after running one low priority callback that made it to the front of the queue. (A separate low priority queue is probably what is really needed. Perhaps any that are skipped are moved to the low-priority queue, and a low-priority item in the main queue is only executed if the low-priority queue is empty.)