WICG / idle-detection

A proposal for an idle detection and notification API for the web
Other
68 stars 22 forks source link

API shape - modernize the API #5

Closed inexorabletash closed 5 years ago

inexorabletash commented 6 years ago

There are several problems with the chrome.idle.* API shape:

  1. 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
  2. when using the event, there's no obvious place to hang a permission request, which is usually done on a method call.
  3. 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)
});