There are several problems with the chrome.idle.* API shape:
it requires all code running within a context (e.g. window) to share the same idle threshold. This could be problematic for multiple libraries / widgets on the same page
when using the event, there's no obvious place to hang a permission request, which is usually done on a method call.
doing a query() and registering an event listener can race - it would be possible for both to return the same result or different results. Ideally you'd get a known state and change sequences relative to that.
Here's a proposal based on the shape in the Permissions API which addresses all three issues:
const status = await navigator.idle.query({threshold: 10}); // threshold is per query (#1)
// permission prompt here, if necessary (#2)
console.log(status.state); // current status
status.addEventListener('change', e => {
console.log(status.state); // update changes the status object (#3)
});
There are several problems with the chrome.idle.* API shape:
query()
and registering an event listener can race - it would be possible for both to return the same result or different results. Ideally you'd get a known state and change sequences relative to that.Here's a proposal based on the shape in the Permissions API which addresses all three issues: