Files
Immich/web/src/lib/utils/wakelock.svelte.ts
T
renovate[bot]andCodex c36be7692c fix(deps): update dependency react-email to v6 (#32143)
* fix(deps): update dependency react-email to v6

* fix: move all react email imports to react-email

* fix: linting

---------

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: Daniel Dietzler <mail@ddietzler.dev>
(cherry picked from commit 9755f64684de63f1e9c5d14d31d00f599f8925b9)
2026-10-15 14:33:10 +03:00

51 lines
1.1 KiB
TypeScript

import { on } from 'svelte/events';
import { browser } from '$app/environment';
const isSupported = browser && 'wakeLock' in navigator;
let sentinel: WakeLockSentinel | undefined;
let acquiring = false;
export async function acquireWakeLock() {
if (!isSupported) {
return;
}
if (sentinel && !sentinel.released) {
return;
}
if (acquiring) {
return;
}
acquiring = true;
try {
sentinel = await navigator.wakeLock.request('screen');
} catch (error) {
console.warn('Failed to acquire wake lock:', error);
} finally {
acquiring = false;
}
}
export async function releaseWakeLock() {
if (!sentinel) {
return;
}
const toReleaseSentinel = sentinel;
// Unset first to avoid race condition after await
sentinel = undefined;
await toReleaseSentinel.release();
}
if (isSupported) {
// Wake lock is cleared when user changes to a different tab,
// so we need to reacquire the wake lock when they come back.
on(globalThis, 'visibilitychange', () => {
if (sentinel && document.visibilityState === 'visible') {
void acquireWakeLock();
}
});
}