w3c / battery

Battery Status API
https://www.w3.org/TR/battery-status/
Other
24 stars 15 forks source link

Explore a high-level API #52

Open anssiko opened 2 years ago

anssiko commented 2 years ago

I believe these valid use-cases can be satisfied with onlowpower and maybe onfullpower events, rather than the current API. The existing API can have its precision reduced, likely without impacting valid use-cases. If absolutely necessary, we could imagine a 3rd, onverylowpower event, which sites could use to e.g. save current state to localStorage, attempt to flush the send buffers in any Service Workers, and otherwise prepare to shut down safely.

via https://bugs.chromium.org/p/chromium/issues/detail?id=661792#c49

See also https://github.com/w3c/battery/issues/25#issuecomment-1027316300

beaufortfrancois commented 2 years ago

For info, I've played a bit with this high-level API and created a quick demo at https://high-level-battery-api.glitch.me/

You can look at the source code at https://glitch.com/edit/#!/high-level-battery-api?path=index.js

It looks like this:

const battery = await navigator.getBattery();

battery.addEventListener("levelchange", () => {
  if (battery.level < 0.1) {
    battery.dispatchEvent(new Event("lowpower"));
  } else if (battery.level === 1) {
    battery.dispatchEvent(new Event("fullpower"));
  }
});
anssiko commented 1 year ago

@beaufortfrancois thanks! This is on our TPAC agenda https://github.com/w3c/devicesensors-wg/issues/56

tomayac commented 1 year ago

Here’s a rather hilarious use case that would be enabled by such a high-level API: Die With Me, “[t]he chat app you can only use when you have less than 5% battery”.