Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/use-wake-lock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export const useWakeLock = ({
);

const release = React.useCallback(async () => {
const isWakeLockUndefined = wakeLock.current == null;
const isWakeLockUndefined = wakeLock.current === null;
if (!isSupported) {
return warn(
"Calling the `release` function has no effect, Wake Lock Screen API isn't supported"
Expand All @@ -73,9 +73,9 @@ export const useWakeLock = ({
React.useEffect(() => {
if (reacquireOnPageVisible) {
const handleVisibilityChange = async () => {
if (wakeLock.current && document.visibilityState === 'visible') {
if (wakeLock.current === null && document.visibilityState === 'visible') {
try {
wakeLock.current = await navigator.wakeLock.request('screen');
await request();
} catch (error: any) {
onError?.(error);
}
Expand All @@ -88,7 +88,7 @@ export const useWakeLock = ({
};
}
return undefined;
}, [reacquireOnPageVisible, onError]);
}, [reacquireOnPageVisible, request, onError]);

return {
isSupported,
Expand Down
10 changes: 8 additions & 2 deletions test/use-wake-lock.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ test('useWakeLock should call `onRelease` when wakeLockSentinel is released', as
expect(handleRelease).toHaveBeenCalledWith(expect.any(Event));
});

test('useWakeLock reacquires wake lock on page visibility change when `reacquireOnPageVisible` is true', async () => {
test('after releasing, useWakeLock reacquires wake lock on page visibility change when `reacquireOnPageVisible` is true', async () => {
const handleError = jest.fn();
const { result } = renderHook(() => useWakeLock({ reacquireOnPageVisible: true, onError: handleError }));

Expand All @@ -184,6 +184,12 @@ test('useWakeLock reacquires wake lock on page visibility change when `reacquire
expect(result.current.type).toEqual('screen');
expect(result.current.released).toBe(false);

await act(async () => {
await result.current.release();
});

expect(result.current.released).toBe(true);

// Mock document.visibilityState to 'hidden'
Object.defineProperty(document, 'visibilityState', {
value: 'hidden',
Expand All @@ -204,4 +210,4 @@ test('useWakeLock reacquires wake lock on page visibility change when `reacquire
expect(result.current.type).toEqual('screen');
expect(result.current.released).toBe(false);
expect(handleError).not.toHaveBeenCalled();
});
});
Loading