mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-25 16:38:44 +00:00
51 lines
1.4 KiB
TypeScript
51 lines
1.4 KiB
TypeScript
let wakeLockSentinel: WakeLockSentinel | null = null
|
|||
|
|
let wakeLockCount = 0
|
||
|
|
|
||
|
|
export async function acquireWakeLock(): Promise<void> {
|
||
|
|
wakeLockCount++
|
||
|
|
if (wakeLockCount > 1 && wakeLockSentinel) {
|
||
|
|
return
|
||
|
|
}
|
||
|
|
|
||
|
|
if (typeof navigator !== 'undefined' && 'wakeLock' in navigator) {
|
||
|
|
try {
|
||
|
|
wakeLockSentinel = await navigator.wakeLock.request('screen')
|
||
|
|
wakeLockSentinel.addEventListener('release', () => {
|
||
|
|
wakeLockSentinel = null
|
||
|
|
})
|
||
|
|
} catch (err) {
|
||
|
|
console.warn('Screen Wake Lock request failed:', err)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
export async function releaseWakeLock(): Promise<void> {
|
||
|
|
wakeLockCount = Math.max(0, wakeLockCount - 1)
|
||
|
|
if (wakeLockCount === 0 && wakeLockSentinel) {
|
||
|
|
try {
|
||
|
|
await wakeLockSentinel.release()
|
||
|
|
} catch (err) {
|
||
|
|
console.warn('Screen Wake Lock release failed:', err)
|
||
|
|
} finally {
|
||
|
|
wakeLockSentinel = null
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
if (typeof document !== 'undefined') {
|
||
|
|
document.addEventListener('visibilitychange', async () => {
|
||
|
|
if (document.visibilityState === 'visible' && wakeLockCount > 0 && !wakeLockSentinel) {
|
||
|
|
try {
|
||
|
|
if (typeof navigator !== 'undefined' && 'wakeLock' in navigator) {
|
||
|
|
wakeLockSentinel = await navigator.wakeLock.request('screen')
|
||
|
|
wakeLockSentinel.addEventListener('release', () => {
|
||
|
|
wakeLockSentinel = null
|
||
|
|
})
|
||
|
|
}
|
||
|
|
} catch (err) {
|
||
|
|
console.warn('Screen Wake Lock re-acquisition failed:', err)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
})
|
||
|
|
}
|