|
1 | 1 | <script lang="ts"> |
2 | | - import type { WakeLockSentinel } from 'src/types'; |
3 | | - import { onDestroy, onMount, createEventDispatcher } from 'svelte'; |
| 2 | + import { browser } from "$app/env"; |
4 | 3 |
|
5 | | - /** |
6 | | - * Disable the automatic request to lock |
7 | | - * the wake screen on mount of the component. |
8 | | - */ |
9 | | - export let lockOnMountDisabled: boolean = undefined; |
10 | | - /** |
11 | | - * Disable the automatic release of the lock. |
12 | | - * Note: using this prop can lead to memory leaks. |
13 | | - */ |
14 | | - export let unlockOnMountDisabled: boolean = undefined; |
| 4 | + import type { WakeLockSentinel } from "src/types"; |
| 5 | + import { onDestroy, onMount, createEventDispatcher } from "svelte"; |
15 | 6 |
|
16 | | - /** |
17 | | - * Captured reference to control and |
18 | | - * lsiten to lock. |
19 | | - */ |
20 | | - let sentinel: WakeLockSentinel = null; |
| 7 | + /** |
| 8 | + * Disable the automatic request to lock |
| 9 | + * the wake screen on mount of the component. |
| 10 | + */ |
| 11 | + export let lockOnMountDisabled: boolean = undefined; |
| 12 | + /** |
| 13 | + * Disable the automatic release of the lock. |
| 14 | + * Note: using this prop can lead to memory leaks. |
| 15 | + */ |
| 16 | + export let unlockOnMountDisabled: boolean = undefined; |
21 | 17 |
|
22 | | - const dispatch = createEventDispatcher(); |
| 18 | + /** |
| 19 | + * Captured reference to control and |
| 20 | + * lsiten to lock. |
| 21 | + */ |
| 22 | + let sentinel: WakeLockSentinel = null; |
23 | 23 |
|
24 | | - export async function requestWakeLock() { |
25 | | - if (!('wakeLock' in navigator)) { |
26 | | - dispatch('error', new Error('unsupported')); |
27 | | - return; |
28 | | - } |
| 24 | + const dispatch = createEventDispatcher(); |
29 | 25 |
|
30 | | - try { |
31 | | - sentinel = await navigator.wakeLock.request(); |
32 | | - sentinel.addEventListener('release', () => |
33 | | - dispatch('change', { released: sentinel.released }) |
34 | | - ); |
35 | | - dispatch('change', { released: sentinel.released }); |
36 | | - } catch (error) { |
37 | | - dispatch('error', error); |
38 | | - } |
39 | | - } |
| 26 | + export async function requestWakeLock() { |
| 27 | + if (!("wakeLock" in navigator)) { |
| 28 | + dispatch("error", new Error("unsupported")); |
| 29 | + return; |
| 30 | + } |
40 | 31 |
|
41 | | - /** |
42 | | - * Manually release the lock. Can be access by |
43 | | - * using `bind:this={ref}` and the calling |
44 | | - * `ref.release()`. |
45 | | - */ |
46 | | - export function release() { |
47 | | - sentinel?.release(); |
48 | | - sentinel = null; |
49 | | - } |
| 32 | + try { |
| 33 | + sentinel = await navigator.wakeLock.request(); |
| 34 | + sentinel.addEventListener("release", () => |
| 35 | + dispatch("change", { released: sentinel.released }) |
| 36 | + ); |
| 37 | + dispatch("change", { released: sentinel.released }); |
| 38 | + } catch (error) { |
| 39 | + dispatch("error", error); |
| 40 | + } |
| 41 | + } |
50 | 42 |
|
51 | | - /** |
52 | | - * React to visibiltiy changes, i.e. if the user |
53 | | - * selects another tab and later returns to your |
54 | | - * page that hosts the sentinel. |
55 | | - */ |
56 | | - async function onVisibilityChange() { |
57 | | - if (sentinel !== null && document.visibilityState === 'visible') { |
58 | | - await requestWakeLock(); |
59 | | - } |
60 | | - } |
| 43 | + /** |
| 44 | + * Manually release the lock. Can be access by |
| 45 | + * using `bind:this={ref}` and the calling |
| 46 | + * `ref.release()`. |
| 47 | + */ |
| 48 | + export function release() { |
| 49 | + sentinel?.release(); |
| 50 | + sentinel = null; |
| 51 | + } |
61 | 52 |
|
62 | | - /** |
63 | | - * Automatically use lock on mount, if not specifiied |
64 | | - * otherwise in props. |
65 | | - */ |
66 | | - onMount(async () => { |
67 | | - if (!lockOnMountDisabled) await requestWakeLock(); |
68 | | - document.addEventListener('visibilitychange', onVisibilityChange); |
69 | | - }); |
| 53 | + /** |
| 54 | + * React to visibiltiy changes, i.e. if the user |
| 55 | + * selects another tab and later returns to your |
| 56 | + * page that hosts the sentinel. |
| 57 | + */ |
| 58 | + async function onVisibilityChange() { |
| 59 | + if (sentinel !== null && document.visibilityState === "visible") { |
| 60 | + await requestWakeLock(); |
| 61 | + } |
| 62 | + } |
70 | 63 |
|
71 | | - /** |
72 | | - * Clean up on unmount, if not specified otherwise. |
73 | | - */ |
74 | | - onDestroy(() => { |
75 | | - if (!unlockOnMountDisabled) release(); |
76 | | - document.removeEventListener('visibilitychange', onVisibilityChange); |
77 | | - }); |
| 64 | + /** |
| 65 | + * Automatically use lock on mount, if not specifiied |
| 66 | + * otherwise in props. |
| 67 | + */ |
| 68 | + onMount(async () => { |
| 69 | + if (!lockOnMountDisabled) await requestWakeLock(); |
| 70 | + if (browser) document.addEventListener("visibilitychange", onVisibilityChange); |
| 71 | + }); |
| 72 | +
|
| 73 | + /** |
| 74 | + * Clean up on unmount, if not specified otherwise. |
| 75 | + */ |
| 76 | + onDestroy(() => { |
| 77 | + if (!unlockOnMountDisabled) release(); |
| 78 | + if (browser) document.removeEventListener("visibilitychange", onVisibilityChange); |
| 79 | + }); |
78 | 80 | </script> |
0 commit comments