Keep the screen awake while a workout is running

Locking and unlocking the phone between every set was the worst part of logging on
one. Settings -> During a workout -> Keep screen awake, on by default; the lock is
held only while S.active exists, so it follows the workout rather than the route and
lets go when you finish.

The browser drops the lock whenever the document stops being visible, which is what
happens the moment you glance at a message, so wakelock.js keeps its own 'wanted'
intent and re-acquires on every visibilitychange. It also releases a sentinel that
arrives after a release landed mid-request, and swallows the rejection iOS gives in
Low Power Mode. Where the API is missing the row shows disabled instead of offering
a switch that does nothing.

Settings is regrouped by what each thing affects while the row needed a home:
General (language, units), During a workout (rest timer, keep awake, sounds, effort),
Notifications, Appearance, Data. Nothing removed, no setting changed meaning.
This commit is contained in:
Duarte Santos committed 2026-07-31 15:35:18 +02:00
1 parent 42d3659615
commit 248da53dca
16 files changed
+258 -43

No files matched your search

+57
View File
@@ -0,0 +1,57 @@
// Screen Wake Lock — keeps the display on while a workout is running, so nobody has to
// unlock their phone between sets.
//
// The browser drops the lock on its own whenever the document stops being visible (tab
// switch, app backgrounded, screen locked by hand). A one-shot request therefore works
// exactly once and then silently dies, which is why `wanted` is kept as our own intent
// and the lock is re-acquired on every visibilitychange for as long as it holds.
import { useEffect } from 'react'
export const wakeLockSupported = () => 'wakeLock' in navigator
let sentinel = null // the live WakeLockSentinel, null when we hold nothing
let wanted = false // do we currently want the screen to stay on?
let pending = false // a request() is in flight — don't stack a second one
async function acquire() {
if (!wanted || sentinel || pending || !wakeLockSupported()) return
if (document.visibilityState !== 'visible') return // request() rejects on a hidden document
pending = true
try {
const s = await navigator.wakeLock.request('screen')
if (!wanted) { s.release().catch(() => {}); return } // released while we were awaiting
sentinel = s
s.addEventListener('release', () => { if (sentinel === s) sentinel = null })
} catch (e) {
// iOS refuses in Low Power Mode, and some browsers refuse on low battery. Nothing to
// do about it and nothing the user could act on — stay quiet and try again next time
// the document becomes visible.
sentinel = null
} finally { pending = false }
}
const onVisible = () => { if (document.visibilityState === 'visible') acquire() }
export function requestWakeLock() {
if (wanted) return
wanted = true
document.addEventListener('visibilitychange', onVisible)
acquire()
}
export function releaseWakeLock() {
wanted = false
document.removeEventListener('visibilitychange', onVisible)
const s = sentinel
sentinel = null
if (s) s.release().catch(() => {})
}
// Holds the lock for as long as `enabled` stays true.
export function useWakeLock(enabled) {
useEffect(() => {
if (!enabled) return
requestWakeLock()
return releaseWakeLock
}, [enabled])
}
+109
View File
@@ -0,0 +1,109 @@
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'
// The whole point of the module is surviving what the browser does on its own: it drops the
// lock every time the document goes hidden. So the fake browser here has to do that too,
// otherwise the tests pass against a one-shot implementation that dies on the first tab switch.
let requests, released, live, listeners, reject
function fakeBrowser() {
requests = 0; released = 0; live = null; reject = false
listeners = new Set()
globalThis.document = {
visibilityState: 'visible',
addEventListener: (type, fn) => { if (type === 'visibilitychange') listeners.add(fn) },
removeEventListener: (type, fn) => { if (type === 'visibilitychange') listeners.delete(fn) },
}
globalThis.navigator = {
wakeLock: {
request: async () => {
requests++
if (reject) throw new Error('NotAllowedError') // iOS Low Power Mode
const subs = new Set()
const s = { addEventListener: (_, fn) => subs.add(fn), release: async () => { if (live === s) live = null; released++; subs.forEach(fn => fn()) } }
live = s
return s
},
},
}
}
const fire = () => listeners.forEach(fn => fn())
const settle = () => new Promise(r => setTimeout(r, 0))
const background = async () => { globalThis.document.visibilityState = 'hidden'; if (live) await live.release(); fire(); await settle() }
const foreground = async () => { globalThis.document.visibilityState = 'visible'; fire(); await settle() }
let requestWakeLock, releaseWakeLock, wakeLockSupported
beforeEach(async () => {
fakeBrowser()
vi.resetModules() // the module holds process-wide state; each test gets a fresh one
;({ requestWakeLock, releaseWakeLock, wakeLockSupported } = await import('./wakelock.js'))
})
afterEach(() => { delete globalThis.document; delete globalThis.navigator })
describe('wakeLockSupported', () => {
it('is false when the browser has no wakeLock at all', () => {
globalThis.navigator = {}
expect(wakeLockSupported()).toBe(false)
})
it('is true when it does', () => {
expect(wakeLockSupported()).toBe(true)
})
})
describe('requestWakeLock', () => {
it('takes a lock', async () => {
requestWakeLock(); await settle()
expect(requests).toBe(1)
expect(live).not.toBeNull()
})
it('does not stack a second lock while one is held', async () => {
requestWakeLock(); await settle()
requestWakeLock(); await settle()
expect(requests).toBe(1)
})
it('does not request one on a hidden document', async () => {
globalThis.document.visibilityState = 'hidden'
requestWakeLock(); await settle()
expect(requests).toBe(0)
})
it('re-acquires after the browser drops it on backgrounding', async () => {
requestWakeLock(); await settle()
await background()
expect(live).toBeNull()
await foreground()
expect(requests).toBe(2)
expect(live).not.toBeNull()
})
it('survives a rejection and retries when the document comes back', async () => {
reject = true
requestWakeLock(); await settle()
expect(live).toBeNull()
reject = false
await foreground()
expect(live).not.toBeNull()
})
})
describe('releaseWakeLock', () => {
it('releases the held lock and stops re-acquiring', async () => {
requestWakeLock(); await settle()
releaseWakeLock(); await settle()
expect(released).toBe(1)
expect(live).toBeNull()
await background(); await foreground()
expect(requests).toBe(1)
})
it('does not leak a lock when it lands while a request is in flight', async () => {
requestWakeLock() // no await — the request is still pending
releaseWakeLock()
await settle()
expect(live).toBeNull() // the arriving sentinel was released immediately
await foreground()
expect(requests).toBe(1) // and we did not start wanting it again
})
})