Cleanup wake lock

This commit is contained in:
schroda
2026-07-16 11:37:18 +02:00
parent e53d064c92
commit 98ee3d46c8
3 changed files with 13 additions and 11 deletions

View File

@@ -28,7 +28,7 @@ import { getErrorMessage } from '@/lib/HelperFunctions.ts';
import type { NavbarContextType } from '@/features/navigation-bar/NavigationBar.types.ts';
import { withPropsFrom } from '@/base/hoc/withPropsFrom.tsx';
import { useReaderResetStates } from '@/features/reader/hooks/useReaderResetStates.ts';
import { useReaderWakeLock } from '@/features/reader/wake-lock/useReaderWakeLock.ts';
import { useWakeLock } from '@/base/hooks/useWakeLock.ts';
import { useReaderSetSettingsState } from '@/features/reader/hooks/useReaderSetSettingsState.ts';
import { useReaderShowSettingPreviewOnChange } from '@/features/reader/hooks/useReaderShowSettingPreviewOnChange.ts';
import { useReaderSetChaptersState } from '@/features/reader/hooks/useReaderSetChaptersState.ts';
@@ -160,7 +160,7 @@ const BaseReader = ({
}, [mangaResponse.data?.manga]);
useReaderResetStates();
useReaderWakeLock(shouldWakeLockScreen);
useWakeLock(shouldWakeLockScreen);
useReaderSetSettingsState(
mangaResponse,
defaultSettingsResponse,

View File

@@ -1,56 +0,0 @@
/*
* Copyright (C) Contributors to the Suwayomi project
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
import { useEffect, useRef } from 'react';
export function useReaderWakeLock(shouldLock: boolean) {
const wakeLockSentinelRef = useRef<WakeLockSentinel | null>(null);
useEffect(() => {
if (!('wakeLock' in navigator)) {
return;
}
const requestWakeLock = async () => {
try {
if (wakeLockSentinelRef.current) {
return;
}
wakeLockSentinelRef.current = await navigator.wakeLock.request('screen');
wakeLockSentinelRef.current.addEventListener('release', () => {
wakeLockSentinelRef.current = null;
});
} catch {
wakeLockSentinelRef.current = null;
}
};
const releaseWakeLock = async () => {
if (wakeLockSentinelRef.current) {
await wakeLockSentinelRef.current.release();
wakeLockSentinelRef.current = null;
}
};
const handleVisibilityChange = async () => {
if (document.visibilityState === 'visible' && shouldLock) {
await requestWakeLock();
}
};
if (shouldLock) {
requestWakeLock();
document.addEventListener('visibilitychange', handleVisibilityChange);
} else {
releaseWakeLock();
}
return () => {
document.removeEventListener('visibilitychange', handleVisibilityChange);
releaseWakeLock();
};
}, [shouldLock]);
}