From 00c81ad85bef1a702485eb5fcd859b080efcc656 Mon Sep 17 00:00:00 2001 From: Robin Townsend Date: Fri, 21 Jul 2023 12:12:44 -0400 Subject: [PATCH] Add comments --- src/useWakeLock.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/useWakeLock.ts b/src/useWakeLock.ts index 5a8ccb6e..f4af4c52 100644 --- a/src/useWakeLock.ts +++ b/src/useWakeLock.ts @@ -26,10 +26,14 @@ export const useWakeLock = () => { let mounted = true; let lock: WakeLockSentinel | null = null; + // The lock is automatically released whenever the window goes invisible, + // so we need to reacquire it on visiblity changes const onVisiblityChange = async () => { if (document.visibilityState === "visible") { try { lock = await navigator.wakeLock.request("screen"); + // Handle the edge case where this component unmounts before the + // promise resolves if (!mounted) lock .release()