Skip to content

Commit ca72860

Browse files
Fix prerender error
1 parent 3de37ec commit ca72860

File tree

4 files changed

+72
-71
lines changed

4 files changed

+72
-71
lines changed

.prettierrc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
2-
"useTabs": true,
3-
"singleQuote": true,
2+
"bracketSpacing": true,
3+
"singleQuote": false,
44
"trailingComma": "none",
55
"printWidth": 100
66
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"version": "1.0.6",
2+
"version": "1.0.7",
33
"name": "svelte-screen-wake-lock",
44
"description": "Svelte component to use the Screen Wake Lock API to progressively enhance the app",
55
"keywords": [

src/lib/ScreenWakeLock.svelte

Lines changed: 69 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,80 @@
11
<script lang="ts">
2-
import type { WakeLockSentinel } from 'src/types';
3-
import { onDestroy, onMount, createEventDispatcher } from 'svelte';
2+
import { browser } from "$app/env";
43
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";
156
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;
2117
22-
const dispatch = createEventDispatcher();
18+
/**
19+
* Captured reference to control and
20+
* lsiten to lock.
21+
*/
22+
let sentinel: WakeLockSentinel = null;
2323
24-
export async function requestWakeLock() {
25-
if (!('wakeLock' in navigator)) {
26-
dispatch('error', new Error('unsupported'));
27-
return;
28-
}
24+
const dispatch = createEventDispatcher();
2925
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+
}
4031
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+
}
5042
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+
}
6152
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+
}
7063
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+
});
7880
</script>

src/lib/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
import ScreenWakeLock from "$lib/ScreenWakeLock.svelte";
2-
32
export { ScreenWakeLock }

0 commit comments

Comments
 (0)