diff options
| author | sandbank52641 <153552626+sandbank52641@users.noreply.github.com> | 2024-09-21 13:31:14 +0200 |
|---|---|---|
| committer | Daniel Micay <daniel.micay@grapheneos.org> | 2024-09-21 23:07:45 -0400 |
| commit | d0cbf321a8c61521c592c8a80aebcd925b1d101d (patch) | |
| tree | ba2df92d26a0da4d9c2a29074a02fa72252d9da5 /static/js/web-install.js | |
| parent | e53724dd4c048b631214119e0424c1f5dc266771 (diff) | |
add wake locks to web installer
Sets wake locks during long-running operations, such as downloading and
flashing the release.
Close: https://github.com/GrapheneOS/grapheneos.org/issues/837
Test: Manual
Diffstat (limited to 'static/js/web-install.js')
| -rw-r--r-- | static/js/web-install.js | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/static/js/web-install.js b/static/js/web-install.js index a8402759..0f8353b2 100644 --- a/static/js/web-install.js +++ b/static/js/web-install.js @@ -20,6 +20,36 @@ const InstallerState = { INSTALLING_RELEASE: 0x2 }; +let wakeLock = null; + +const requestWakeLock = async () => { + try { + wakeLock = await navigator.wakeLock.request("screen"); + console.log("Wake lock has been set"); + wakeLock.addEventListener("release", async () => { + console.log("Wake lock has been released"); + }); + } catch (err) { + // if wake lock request fails - usually system related, such as battery + throw new Error(`${err.name}, ${err.message}`); + } +}; + +const releaseWakeLock = async () => { + if (wakeLock !== null) { + wakeLock.release().then(() => { + wakeLock = null; + }); + } +}; + +// reacquires the wake lock should the visibility of the document change and the wake lock is released +document.addEventListener("visibilitychange", async () => { + if (wakeLock !== null && document.visibilityState === "visible") { + await requestWakeLock(); + } +}); + // This wraps XHR because getting progress updates with fetch() is overly complicated. function fetchBlobWithProgress(url, onProgress) { let xhr = new XMLHttpRequest(); @@ -212,6 +242,7 @@ async function getLatestRelease() { } async function downloadRelease(setProgress) { + await requestWakeLock(); await ensureConnected(setProgress); setProgress("Finding latest release..."); @@ -227,6 +258,7 @@ async function downloadRelease(setProgress) { }); } finally { setInstallerState({ state: InstallerState.DOWNLOADING_RELEASE, active: false }); + await releaseWakeLock(); } setProgress(`Downloaded ${latestZip} release.`, 1.0); } @@ -251,6 +283,7 @@ async function reconnectCallback() { } async function flashRelease(setProgress) { + await requestWakeLock(); await ensureConnected(setProgress); // Need to do this again because the user may not have clicked download if @@ -297,6 +330,7 @@ async function flashRelease(setProgress) { } } finally { setInstallerState({ state: InstallerState.INSTALLING_RELEASE, active: false }); + await releaseWakeLock(); } return `Flashed ${latestZip} to device.`; @@ -365,6 +399,7 @@ function addButtonHook(id, callback) { } catch (error) { statusCallback(`Error: ${error.message}`); statusField.className = "error-text"; + await releaseWakeLock(); // Rethrow the error so it shows up in the console throw error; } |
