battery icon indicating copy to clipboard operation
battery copied to clipboard

Explore a high-level API

Open anssiko opened this issue 3 years ago • 3 comments

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

anssiko avatar Feb 01 '22 21:02 anssiko

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"));
  }
});

beaufortfrancois avatar Jul 04 '22 12:07 beaufortfrancois

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

anssiko avatar Aug 08 '22 16:08 anssiko

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”.

tomayac avatar Oct 07 '22 07:10 tomayac