Skip to content

Commit

Permalink
Enhance wakeLock consistency
Browse files Browse the repository at this point in the history
  • Loading branch information
pehala committed Jan 28, 2024
1 parent 1b1d8d9 commit efc5edf
Showing 1 changed file with 27 additions and 12 deletions.
39 changes: 27 additions & 12 deletions backend/templates/songs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -184,22 +184,37 @@ <h6 class="d-inline" ><%:author%></h6>
$.fn.DataTable.ext.pager.numbers_length = 5;
$.templates("song", "#songTemplate")

let wakelock;
let wakeLock = null;
const requestWakeLock = async () => {
wakeLock = await navigator.wakeLock
.request('screen')
.catch(err => console.error('Failed to lock wake state with reason:', err.message))

console.log("Screen WakeLock turned on")

// listen for our release event
wakeLock.onrelease = function(ev) {
console.log("Screen WakeLock released")
}
}
const handleVisibilityChange = () => {
if (wakeLock !== null && document.visibilityState === 'visible') {
requestWakeLock();
}
}

async function preventSleep(value) {
if(!canWakeLock()) return;
if (value) {
try {
wakelock = await navigator.wakeLock.request();
wakelock.addEventListener('release', () => {
console.log('Screen Wake State Locked:', !wakelock.released);
});
console.log('Screen Wake State Locked:', !wakelock.released);
} catch(e) {
console.error('Failed to lock wake state with reason:', e.message);
}
await requestWakeLock()
document.addEventListener('visibilitychange', handleVisibilityChange);
} else {
if(wakelock) wakelock.release();
wakelock = null;
if(wakeLock) {
wakeLock.release().then(() => {
wakeLock = null;
document.removeEventListener('visibilitychange', handleVisibilityChange);
});
}
}
}
let hidden = false
Expand Down

0 comments on commit efc5edf

Please sign in to comment.