Skip to content

Commit

Permalink
Test screen lock
Browse files Browse the repository at this point in the history
  • Loading branch information
mauriciofierrom committed Apr 2, 2024
1 parent befa9f9 commit c582a9a
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions app/javascript/controllers/player_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ export default class extends Controller {
/** @property {PickingPointState} */
pickingPointState;

/** @property {WakeLockSentinel} */
#wakeLock;

static targets = [ "source", "duration" ]

connect() {
Expand All @@ -69,6 +72,18 @@ export default class extends Controller {

initialize() {
this.#initStates()

if(!this.editValue) {
this.#acquireScreenLock()
}
}

disconnect() {
if(this.#wakeLock !== null && this.#wakeLock !== undefined) {
this.#wakeLock.release().then(() => {
console.log("wake lock released on player controller disconnect")
})
}
}

/**
Expand Down Expand Up @@ -243,4 +258,28 @@ export default class extends Controller {
this.reset()
}
}

#acquireScreenLock = async () => {
if ("wakeLock" in navigator) {
console.log("wakeLock in navigator")
try {
this.#wakeLock = await navigator.wakeLock.request("screen")

this.#wakeLock.addEventListener("release", () => {
console.log("wake lock released")
})

document.addEventListener("visibilitychange", async () => {
if (this.#wakeLock !== null && document.visibilityState === "visible") {
console.log("reacquiring lock")
this.#wakeLock = await navigator.wakeLock.request("screen");
}
});
} catch (err) {
console.log(`${err.name}, ${err.message}`)
}
} else {
console.log("no wakeLock in navigator")
}
}
}

0 comments on commit c582a9a

Please sign in to comment.