Keep the screen on and ask before leaving while a session is busy
While a share waits or a transfer runs, the page holds a Screen Wake Lock, requested again once the page is visible, since the browser releases it whenever it's hidden. A beforeunload handler makes the browser ask before a reload or a closed tab ends the session, only while busy so the page stays eligible for the back/forward cache otherwise. Covered by awake: with a fake wake lock, a headless browser may not grant a real one. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
870d55b44f
commit
44a8a72261
3 files changed
+128
-6
No files matched your search
+51
@@ -293,6 +293,57 @@ function onReceive() {
|
||||
const saltLocked = computed(
|
||||
() => viaTracker.value && side.value === 'send' && !['error', 'done', 'stopped'].includes(phase.value!),
|
||||
);
|
||||
|
||||
// While a share waits or a transfer runs. A phone that dims and locks may suspend the tab and the
|
||||
// connection dies, a reload or a closed tab ends it for sure.
|
||||
const busy = computed(() => ['start', 'wait', 'retry', 'transfer'].includes(phase.value!));
|
||||
|
||||
// The screen's, held while busy. The browser releases it whenever the page is hidden, so it's
|
||||
// requested again once the page is visible.
|
||||
let wakeLock: Promise<WakeLockSentinel | undefined> | undefined;
|
||||
|
||||
function holdWakeLock() {
|
||||
// Missing outside a secure context, e.g. the dev server opened by its LAN address.
|
||||
if (!busy.value || document.hidden || wakeLock || !navigator.wakeLock) return;
|
||||
const request = navigator.wakeLock.request('screen').then(
|
||||
(sentinel) => {
|
||||
sentinel.addEventListener('release', () => {
|
||||
if (wakeLock === request) wakeLock = undefined;
|
||||
});
|
||||
// Unless let go of meanwhile, releaseWakeLock releases it then.
|
||||
if (wakeLock === request) events.log('keeping the screen on');
|
||||
return sentinel;
|
||||
},
|
||||
(failure) => {
|
||||
if (wakeLock === request) wakeLock = undefined;
|
||||
events.log(`screen may lock: ${failure instanceof Error ? failure.message : failure}`);
|
||||
return undefined;
|
||||
},
|
||||
);
|
||||
wakeLock = request;
|
||||
}
|
||||
|
||||
// Forgotten right away, a session started before the browser released it requests a new one.
|
||||
function releaseWakeLock() {
|
||||
const request = wakeLock;
|
||||
wakeLock = undefined;
|
||||
request?.then((sentinel) => sentinel?.release());
|
||||
}
|
||||
|
||||
// The browser asks before a reload or a closed tab ends it. Only while busy, a page with the handler
|
||||
// may be kept out of the back/forward cache.
|
||||
const confirmLeave = (event: BeforeUnloadEvent) => event.preventDefault();
|
||||
|
||||
watch(busy, (busy) => {
|
||||
if (busy) {
|
||||
holdWakeLock();
|
||||
addEventListener('beforeunload', confirmLeave);
|
||||
} else {
|
||||
releaseWakeLock();
|
||||
removeEventListener('beforeunload', confirmLeave);
|
||||
}
|
||||
});
|
||||
document.addEventListener('visibilitychange', holdWakeLock);
|
||||
</script>
|
||||
|
||||
<template>
|
||||
|
||||
Reference in new issue
Block a user