Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Explore a high-level API #52

Open
anssiko opened this issue Feb 1, 2022 · 3 comments
Open

Explore a high-level API #52

anssiko opened this issue Feb 1, 2022 · 3 comments

Comments

@anssiko
Copy link
Member

anssiko commented Feb 1, 2022

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 #25 (comment)

@beaufortfrancois
Copy link

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
Copy link
Member Author

anssiko commented Aug 8, 2022

@beaufortfrancois thanks! This is on our TPAC agenda w3c/devicesensors-wg#56

@tomayac
Copy link

tomayac commented Oct 7, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants