-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontroller.mjs
More file actions
36 lines (33 loc) · 979 Bytes
/
controller.mjs
File metadata and controls
36 lines (33 loc) · 979 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
export class WakeLockController {
/** @type {import("./model.mjs").MutableReference<import('./model.mjs').WakeLockState>} */
#state;
#wakeLock = null;
/**
* @param {import("./model.mjs").MutableReference<import('./model.mjs').WakeLockState>} state
*/
constructor(state) {
this.#state = state;
}
async requestLock() {
try {
this.#wakeLock = await navigator.wakeLock.request("screen");
this.#state.set({ isActive: true, error: null });
this.#wakeLock.addEventListener("release", () => {
this.#state.set({ isActive: false, error: null });
});
} catch (error) {
this.#state.set({ isActive: false, error });
}
}
async releaseLock() {
try {
if (this.#wakeLock != null) {
await this.#wakeLock.release();
this.#wakeLock = null;
this.#state.set({ isActive: false, error: null });
}
} catch (error) {
this.#state.set({ isActive: false, error });
}
}
}