Skip to content

Instantly share code, notes, and snippets.

@github0013
Last active March 14, 2024 00:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save github0013/7bba37c108cb8983ff3f0dd2f7102cc7 to your computer and use it in GitHub Desktop.
Save github0013/7bba37c108cb8983ff3f0dd2f7102cc7 to your computer and use it in GitHub Desktop.
const { awake, request, release } = useWakeLock()
...
...
return (
...
onClick={() => {
// can't mix with other functions to make sure
// the video is played under a user interaction?
awake ? release() : request()
}}
...
)
import NoSleep from "nosleep.js"
import React from "react"
const useWakeLock = () => {
const noSleep = React.useMemo(() => new NoSleep(), [])
const [awake, set] = React.useState(noSleep.isEnabled)
const request = React.useCallback(() => {
noSleep.enable().then(() => set(noSleep.isEnabled))
}, [])
const release = React.useCallback(() => {
noSleep.disable()
set(noSleep.isEnabled)
}, [])
return {
awake,
request,
release,
}
}
export { useWakeLock }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment