@swoff/cli 0.4.3 → 0.4.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/lib/commands/generate-guide.js +16 -16
- package/dist/lib/commands/generate-guide.js.map +1 -1
- package/dist/lib/commands/init.js +127 -17
- package/dist/lib/commands/init.js.map +1 -1
- package/dist/lib/config/validator.js +1 -1
- package/dist/lib/config/validator.js.map +1 -1
- package/dist/lib/generators/file-generators/client-injector-bundle.js +2 -1
- package/dist/lib/generators/file-generators/client-injector-bundle.js.map +1 -1
- package/dist/lib/generators/file-generators/client-injector.js +2 -1
- package/dist/lib/generators/file-generators/client-injector.js.map +1 -1
- package/dist/lib/generators/file-generators/context.js +1 -1
- package/dist/lib/generators/file-generators/context.js.map +1 -1
- package/dist/lib/generators/file-generators/generate-framework-adapters.js +30 -23
- package/dist/lib/generators/file-generators/generate-framework-adapters.js.map +1 -1
- package/dist/lib/generators/sw-sections/background-precache.js +8 -1
- package/dist/lib/generators/sw-sections/background-precache.js.map +1 -1
- package/dist/lib/generators/sw-sections/batch-refresh-queue.js +2 -5
- package/dist/lib/generators/sw-sections/batch-refresh-queue.js.map +1 -1
- package/dist/lib/generators/sw-sections/fetch-handler.js +29 -7
- package/dist/lib/generators/sw-sections/fetch-handler.js.map +1 -1
- package/dist/lib/generators/swoff-files-generator.js +2 -1
- package/dist/lib/generators/swoff-files-generator.js.map +1 -1
- package/dist/lib/shared/config-types.js +1 -0
- package/dist/lib/shared/config-types.js.map +1 -1
- package/dist/lib/utils/detect-framework.js +4 -1
- package/dist/lib/utils/detect-framework.js.map +1 -1
- package/dist/runtime/client-injector-bundle.js +2 -2
- package/dist/runtime/client-injector-bundle.js.map +1 -1
- package/dist/runtime/client-injector.js +3 -2
- package/dist/runtime/client-injector.js.map +1 -1
- package/dist/runtime/gql-wrapper.js +3 -1
- package/dist/runtime/gql-wrapper.js.map +1 -1
- package/dist/runtime/swoff-api-bundle.js +2 -1
- package/dist/runtime/swoff-api-bundle.js.map +1 -1
- package/package.json +1 -1
- package/templates/react/{useOfflineAnalytics.jsx → useSwoffAnalytics.jsx} +3 -3
- package/templates/react/{useOfflineAnalytics.tsx → useSwoffAnalytics.tsx} +3 -3
- package/templates/react/{useAuth.jsx → useSwoffAuth.jsx} +2 -2
- package/templates/react/{useAuth.tsx → useSwoffAuth.tsx} +2 -2
- package/templates/react/{useCachedFetch.jsx → useSwoffFetch.jsx} +6 -6
- package/templates/react/{useCachedFetch.tsx → useSwoffFetch.tsx} +7 -7
- package/templates/react/{useMutation.jsx → useSwoffMutation.jsx} +3 -3
- package/templates/react/{useMutation.tsx → useSwoffMutation.tsx} +3 -3
- package/templates/react/{useMutationState.jsx → useSwoffMutationState.jsx} +1 -1
- package/templates/react/{useMutationState.tsx → useSwoffMutationState.tsx} +2 -2
- package/templates/react/{useNetworkStatus.jsx → useSwoffNetwork.jsx} +2 -2
- package/templates/react/{useNetworkStatus.tsx → useSwoffNetwork.tsx} +2 -2
- package/templates/react/{usePrecacheProgress.jsx → useSwoffPrecache.jsx} +4 -7
- package/templates/react/{usePrecacheProgress.tsx → useSwoffPrecache.tsx} +4 -7
- package/templates/react/{usePrefetch.jsx → useSwoffPrefetch.jsx} +2 -2
- package/templates/react/{usePrefetch.tsx → useSwoffPrefetch.tsx} +2 -2
- package/templates/react/{usePushSubscription.jsx → useSwoffPush.jsx} +1 -1
- package/templates/react/{usePushSubscription.tsx → useSwoffPush.tsx} +1 -1
- package/templates/react/{usePwaInstall.jsx → useSwoffPwa.jsx} +1 -1
- package/templates/react/{usePwaInstall.tsx → useSwoffPwa.tsx} +1 -1
- package/templates/react/{useMutationQueue.jsx → useSwoffQueue.jsx} +2 -2
- package/templates/react/{useMutationQueue.tsx → useSwoffQueue.tsx} +2 -2
- package/templates/react/{useStorageEstimate.jsx → useSwoffStorage.jsx} +2 -2
- package/templates/react/{useStorageEstimate.tsx → useSwoffStorage.tsx} +2 -2
- package/templates/react/{useBackgroundSync.jsx → useSwoffSync.jsx} +1 -1
- package/templates/react/{useBackgroundSync.tsx → useSwoffSync.tsx} +1 -1
- package/templates/svelte/useSwoffAnalytics.js +31 -0
- package/templates/svelte/useSwoffAnalytics.ts +37 -0
- package/templates/svelte/useSwoffAuth.js +95 -0
- package/templates/svelte/useSwoffAuth.ts +96 -0
- package/templates/svelte/useSwoffFetch.js +97 -0
- package/templates/svelte/useSwoffFetch.ts +112 -0
- package/templates/svelte/useSwoffMutation.js +110 -0
- package/templates/svelte/useSwoffMutation.ts +141 -0
- package/templates/svelte/useSwoffMutationState.js +27 -0
- package/templates/svelte/useSwoffMutationState.ts +28 -0
- package/templates/svelte/useSwoffNetwork.js +75 -0
- package/templates/svelte/useSwoffNetwork.ts +75 -0
- package/templates/svelte/useSwoffPrecache.js +19 -0
- package/templates/svelte/useSwoffPrecache.ts +19 -0
- package/templates/svelte/useSwoffPrefetch.js +19 -0
- package/templates/svelte/useSwoffPrefetch.ts +20 -0
- package/templates/svelte/useSwoffPush.js +82 -0
- package/templates/svelte/useSwoffPush.ts +82 -0
- package/templates/svelte/useSwoffPwa.js +27 -0
- package/templates/svelte/useSwoffPwa.ts +27 -0
- package/templates/svelte/useSwoffQueue.js +57 -0
- package/templates/svelte/useSwoffQueue.ts +67 -0
- package/templates/svelte/useSwoffReset.js +21 -0
- package/templates/svelte/useSwoffReset.ts +32 -0
- package/templates/svelte/useSwoffStorage.js +47 -0
- package/templates/svelte/useSwoffStorage.ts +47 -0
- package/templates/svelte/useSwoffSync.js +31 -0
- package/templates/svelte/useSwoffSync.ts +31 -0
- package/templates/vue/useSwoffAnalytics.js +46 -0
- package/templates/vue/useSwoffAnalytics.ts +54 -0
- package/templates/vue/useSwoffAuth.js +106 -0
- package/templates/vue/useSwoffAuth.ts +118 -0
- package/templates/vue/useSwoffFetch.js +127 -0
- package/templates/vue/useSwoffFetch.ts +143 -0
- package/templates/vue/useSwoffMutation.js +124 -0
- package/templates/vue/useSwoffMutation.ts +155 -0
- package/templates/vue/useSwoffMutationState.js +35 -0
- package/templates/vue/useSwoffMutationState.ts +36 -0
- package/templates/vue/useSwoffNetwork.js +86 -0
- package/templates/vue/useSwoffNetwork.ts +89 -0
- package/templates/vue/useSwoffPrecache.js +19 -0
- package/templates/vue/useSwoffPrecache.ts +19 -0
- package/templates/vue/useSwoffPrefetch.js +33 -0
- package/templates/vue/useSwoffPrefetch.ts +41 -0
- package/templates/vue/useSwoffPush.js +81 -0
- package/templates/vue/useSwoffPush.ts +81 -0
- package/templates/vue/useSwoffPwa.js +27 -0
- package/templates/vue/useSwoffPwa.ts +27 -0
- package/templates/vue/useSwoffQueue.js +70 -0
- package/templates/vue/useSwoffQueue.ts +82 -0
- package/templates/vue/useSwoffReset.js +32 -0
- package/templates/vue/useSwoffReset.ts +39 -0
- package/templates/vue/useSwoffStorage.js +56 -0
- package/templates/vue/useSwoffStorage.ts +57 -0
- package/templates/vue/useSwoffSync.js +30 -0
- package/templates/vue/useSwoffSync.ts +30 -0
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { writable } from "svelte/store";
|
|
2
|
+
import { onMount } from "svelte";
|
|
3
|
+
import {
|
|
4
|
+
CONNECTIVITY_EVENT,
|
|
5
|
+
forceRetry,
|
|
6
|
+
getCurrentOnlineStatus,
|
|
7
|
+
} from "../connectivity";
|
|
8
|
+
|
|
9
|
+
export function useSwoffNetwork() {
|
|
10
|
+
const online = writable(true);
|
|
11
|
+
const wasOffline = writable(false);
|
|
12
|
+
const lastChangedAt = writable<number | null>(null);
|
|
13
|
+
const effectiveType = writable<string | null>(null);
|
|
14
|
+
const downlink = writable<number | null>(null);
|
|
15
|
+
const isRetrying = writable(false);
|
|
16
|
+
|
|
17
|
+
let wasOfflineFlag = false;
|
|
18
|
+
|
|
19
|
+
onMount(() => {
|
|
20
|
+
if (!getCurrentOnlineStatus()) {
|
|
21
|
+
wasOfflineFlag = true;
|
|
22
|
+
online.set(false);
|
|
23
|
+
wasOffline.set(true);
|
|
24
|
+
lastChangedAt.set(Date.now());
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const connection = navigator.connection;
|
|
28
|
+
|
|
29
|
+
function handleConnectivityChange(e: Event) {
|
|
30
|
+
const isTrulyOnline = (e as CustomEvent).detail.online;
|
|
31
|
+
if (!isTrulyOnline) {
|
|
32
|
+
wasOfflineFlag = true;
|
|
33
|
+
} else {
|
|
34
|
+
wasOfflineFlag = false;
|
|
35
|
+
}
|
|
36
|
+
online.set(isTrulyOnline);
|
|
37
|
+
wasOffline.set(wasOfflineFlag);
|
|
38
|
+
lastChangedAt.set(Date.now());
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function onTypeChange() {
|
|
42
|
+
if (!connection) return;
|
|
43
|
+
effectiveType.set(connection.effectiveType);
|
|
44
|
+
downlink.set(connection.downlink);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
window.addEventListener(CONNECTIVITY_EVENT, handleConnectivityChange);
|
|
48
|
+
if (connection) {
|
|
49
|
+
connection.addEventListener("change", onTypeChange);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
return () => {
|
|
53
|
+
window.removeEventListener(CONNECTIVITY_EVENT, handleConnectivityChange);
|
|
54
|
+
if (connection) {
|
|
55
|
+
connection.removeEventListener("change", onTypeChange);
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
async function retry() {
|
|
61
|
+
isRetrying.set(true);
|
|
62
|
+
await forceRetry();
|
|
63
|
+
isRetrying.set(false);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
return {
|
|
67
|
+
online,
|
|
68
|
+
wasOffline,
|
|
69
|
+
lastChangedAt,
|
|
70
|
+
effectiveType,
|
|
71
|
+
downlink,
|
|
72
|
+
isRetrying,
|
|
73
|
+
retry,
|
|
74
|
+
};
|
|
75
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { writable } from "svelte/store";
|
|
2
|
+
import { onMount } from "svelte";
|
|
3
|
+
|
|
4
|
+
export function useSwoffPrecache() {
|
|
5
|
+
const progress = writable(0);
|
|
6
|
+
|
|
7
|
+
function onProgress(e) {
|
|
8
|
+
progress.set(e.detail.percent);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
onMount(() => {
|
|
12
|
+
window.addEventListener("sw-progress", onProgress);
|
|
13
|
+
return () => {
|
|
14
|
+
window.removeEventListener("sw-progress", onProgress);
|
|
15
|
+
};
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
return { progress };
|
|
19
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { writable } from "svelte/store";
|
|
2
|
+
import { onMount } from "svelte";
|
|
3
|
+
|
|
4
|
+
export function useSwoffPrecache() {
|
|
5
|
+
const progress = writable(0);
|
|
6
|
+
|
|
7
|
+
function onProgress(e: WindowEventMap["sw-progress"]) {
|
|
8
|
+
progress.set(e.detail.percent);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
onMount(() => {
|
|
12
|
+
window.addEventListener("sw-progress", onProgress);
|
|
13
|
+
return () => {
|
|
14
|
+
window.removeEventListener("sw-progress", onProgress);
|
|
15
|
+
};
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
return { progress };
|
|
19
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { writable } from "svelte/store";
|
|
2
|
+
import { prefetchCache } from "../fetch/core.js";
|
|
3
|
+
|
|
4
|
+
export function useSwoffPrefetch() {
|
|
5
|
+
const prefetchList = writable([]);
|
|
6
|
+
|
|
7
|
+
function prefetch(url, options) {
|
|
8
|
+
prefetchCache(url, options);
|
|
9
|
+
prefetchList.update((list) =>
|
|
10
|
+
list.includes(url) ? list : [...list, url],
|
|
11
|
+
);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function clear() {
|
|
15
|
+
prefetchList.set([]);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
return { prefetch, prefetchList, clear };
|
|
19
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { writable } from "svelte/store";
|
|
2
|
+
import { prefetchCache } from "../fetch/core";
|
|
3
|
+
import type { FetchWithCacheOptions } from "../fetch/core";
|
|
4
|
+
|
|
5
|
+
export function useSwoffPrefetch() {
|
|
6
|
+
const prefetchList = writable<string[]>([]);
|
|
7
|
+
|
|
8
|
+
function prefetch(url: string, options?: FetchWithCacheOptions) {
|
|
9
|
+
prefetchCache(url, options);
|
|
10
|
+
prefetchList.update((list) =>
|
|
11
|
+
list.includes(url) ? list : [...list, url],
|
|
12
|
+
);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function clear() {
|
|
16
|
+
prefetchList.set([]);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
return { prefetch, prefetchList, clear };
|
|
20
|
+
}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { writable } from "svelte/store";
|
|
2
|
+
import { onMount } from "svelte";
|
|
3
|
+
import {
|
|
4
|
+
subscribeToPush,
|
|
5
|
+
unsubscribeFromPush,
|
|
6
|
+
isSubscribed,
|
|
7
|
+
getPushSubscription,
|
|
8
|
+
} from "../realtime/notifications.js";
|
|
9
|
+
|
|
10
|
+
export function useSwoffPush() {
|
|
11
|
+
const subscribed = writable(false);
|
|
12
|
+
const subscription = writable(null);
|
|
13
|
+
const permission = writable(
|
|
14
|
+
typeof Notification !== "undefined" ? Notification.permission : "denied",
|
|
15
|
+
);
|
|
16
|
+
const loading = writable(true);
|
|
17
|
+
|
|
18
|
+
onMount(() => {
|
|
19
|
+
let cancelled = false;
|
|
20
|
+
|
|
21
|
+
isSubscribed().then((subd) => {
|
|
22
|
+
if (cancelled) return;
|
|
23
|
+
if (subd) {
|
|
24
|
+
getPushSubscription().then((sub) => {
|
|
25
|
+
if (cancelled) return;
|
|
26
|
+
subscribed.set(true);
|
|
27
|
+
subscription.set(sub);
|
|
28
|
+
permission.set(Notification.permission);
|
|
29
|
+
loading.set(false);
|
|
30
|
+
});
|
|
31
|
+
} else {
|
|
32
|
+
subscribed.set(false);
|
|
33
|
+
subscription.set(null);
|
|
34
|
+
permission.set(Notification.permission);
|
|
35
|
+
loading.set(false);
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
function onSubChanged(e) {
|
|
40
|
+
if (cancelled) return;
|
|
41
|
+
if (!e.detail.subscribed) {
|
|
42
|
+
subscribed.set(false);
|
|
43
|
+
subscription.set(null);
|
|
44
|
+
permission.set(Notification.permission);
|
|
45
|
+
loading.set(false);
|
|
46
|
+
} else {
|
|
47
|
+
getPushSubscription().then((sub) => {
|
|
48
|
+
if (cancelled) return;
|
|
49
|
+
subscribed.set(true);
|
|
50
|
+
subscription.set(sub);
|
|
51
|
+
permission.set(Notification.permission);
|
|
52
|
+
loading.set(false);
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function onPermissionChanged(e) {
|
|
58
|
+
if (cancelled) return;
|
|
59
|
+
permission.set(e.detail.permission);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
window.addEventListener("push-subscription-changed", onSubChanged);
|
|
63
|
+
window.addEventListener("push-permission-changed", onPermissionChanged);
|
|
64
|
+
|
|
65
|
+
return () => {
|
|
66
|
+
cancelled = true;
|
|
67
|
+
window.removeEventListener("push-subscription-changed", onSubChanged);
|
|
68
|
+
window.removeEventListener("push-permission-changed", onPermissionChanged);
|
|
69
|
+
};
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
async function subscribe() {
|
|
73
|
+
const sub = await subscribeToPush();
|
|
74
|
+
return sub !== null;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
async function unsubscribe() {
|
|
78
|
+
await unsubscribeFromPush();
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
return { subscribed, subscription, permission, loading, subscribe, unsubscribe };
|
|
82
|
+
}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { writable } from "svelte/store";
|
|
2
|
+
import { onMount } from "svelte";
|
|
3
|
+
import {
|
|
4
|
+
subscribeToPush,
|
|
5
|
+
unsubscribeFromPush,
|
|
6
|
+
isSubscribed,
|
|
7
|
+
getPushSubscription,
|
|
8
|
+
} from "../realtime/notifications";
|
|
9
|
+
|
|
10
|
+
export function useSwoffPush() {
|
|
11
|
+
const subscribed = writable(false);
|
|
12
|
+
const subscription = writable<PushSubscription | null>(null);
|
|
13
|
+
const permission = writable(
|
|
14
|
+
typeof Notification !== "undefined" ? Notification.permission : "denied",
|
|
15
|
+
);
|
|
16
|
+
const loading = writable(true);
|
|
17
|
+
|
|
18
|
+
onMount(() => {
|
|
19
|
+
let cancelled = false;
|
|
20
|
+
|
|
21
|
+
isSubscribed().then((subd) => {
|
|
22
|
+
if (cancelled) return;
|
|
23
|
+
if (subd) {
|
|
24
|
+
getPushSubscription().then((sub) => {
|
|
25
|
+
if (cancelled) return;
|
|
26
|
+
subscribed.set(true);
|
|
27
|
+
subscription.set(sub);
|
|
28
|
+
permission.set(Notification.permission);
|
|
29
|
+
loading.set(false);
|
|
30
|
+
});
|
|
31
|
+
} else {
|
|
32
|
+
subscribed.set(false);
|
|
33
|
+
subscription.set(null);
|
|
34
|
+
permission.set(Notification.permission);
|
|
35
|
+
loading.set(false);
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
function onSubChanged(e: CustomEvent) {
|
|
40
|
+
if (cancelled) return;
|
|
41
|
+
if (!e.detail.subscribed) {
|
|
42
|
+
subscribed.set(false);
|
|
43
|
+
subscription.set(null);
|
|
44
|
+
permission.set(Notification.permission);
|
|
45
|
+
loading.set(false);
|
|
46
|
+
} else {
|
|
47
|
+
getPushSubscription().then((sub) => {
|
|
48
|
+
if (cancelled) return;
|
|
49
|
+
subscribed.set(true);
|
|
50
|
+
subscription.set(sub);
|
|
51
|
+
permission.set(Notification.permission);
|
|
52
|
+
loading.set(false);
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function onPermissionChanged(e: CustomEvent) {
|
|
58
|
+
if (cancelled) return;
|
|
59
|
+
permission.set(e.detail.permission);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
window.addEventListener("push-subscription-changed", onSubChanged);
|
|
63
|
+
window.addEventListener("push-permission-changed", onPermissionChanged);
|
|
64
|
+
|
|
65
|
+
return () => {
|
|
66
|
+
cancelled = true;
|
|
67
|
+
window.removeEventListener("push-subscription-changed", onSubChanged);
|
|
68
|
+
window.removeEventListener("push-permission-changed", onPermissionChanged);
|
|
69
|
+
};
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
async function subscribe() {
|
|
73
|
+
const sub = await subscribeToPush();
|
|
74
|
+
return sub !== null;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
async function unsubscribe() {
|
|
78
|
+
await unsubscribeFromPush();
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
return { subscribed, subscription, permission, loading, subscribe, unsubscribe };
|
|
82
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { writable } from "svelte/store";
|
|
2
|
+
import { onMount } from "svelte";
|
|
3
|
+
import { promptInstall } from "../pwa/prompt.js";
|
|
4
|
+
|
|
5
|
+
export function useSwoffPwa() {
|
|
6
|
+
const canInstall = writable(false);
|
|
7
|
+
|
|
8
|
+
function onReady() { canInstall.set(true); }
|
|
9
|
+
function onDone() { canInstall.set(false); }
|
|
10
|
+
|
|
11
|
+
onMount(() => {
|
|
12
|
+
window.addEventListener("pwa-installable", onReady);
|
|
13
|
+
window.addEventListener("pwa-installed", onDone);
|
|
14
|
+
window.addEventListener("pwa-dismissed", onDone);
|
|
15
|
+
return () => {
|
|
16
|
+
window.removeEventListener("pwa-installable", onReady);
|
|
17
|
+
window.removeEventListener("pwa-installed", onDone);
|
|
18
|
+
window.removeEventListener("pwa-dismissed", onDone);
|
|
19
|
+
};
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
async function install() {
|
|
23
|
+
await promptInstall();
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
return { canInstall, install };
|
|
27
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { writable } from "svelte/store";
|
|
2
|
+
import { onMount } from "svelte";
|
|
3
|
+
import { promptInstall } from "../pwa/prompt";
|
|
4
|
+
|
|
5
|
+
export function useSwoffPwa() {
|
|
6
|
+
const canInstall = writable(false);
|
|
7
|
+
|
|
8
|
+
function onReady() { canInstall.set(true); }
|
|
9
|
+
function onDone() { canInstall.set(false); }
|
|
10
|
+
|
|
11
|
+
onMount(() => {
|
|
12
|
+
window.addEventListener("pwa-installable", onReady);
|
|
13
|
+
window.addEventListener("pwa-installed", onDone);
|
|
14
|
+
window.addEventListener("pwa-dismissed", onDone);
|
|
15
|
+
return () => {
|
|
16
|
+
window.removeEventListener("pwa-installable", onReady);
|
|
17
|
+
window.removeEventListener("pwa-installed", onDone);
|
|
18
|
+
window.removeEventListener("pwa-dismissed", onDone);
|
|
19
|
+
};
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
async function install() {
|
|
23
|
+
await promptInstall();
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
return { canInstall, install };
|
|
27
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { writable } from "svelte/store";
|
|
2
|
+
import { onMount } from "svelte";
|
|
3
|
+
import {
|
|
4
|
+
getPendingCount,
|
|
5
|
+
getQueueItems,
|
|
6
|
+
processMutationQueue,
|
|
7
|
+
} from "../mutation/queue.js";
|
|
8
|
+
|
|
9
|
+
export function useSwoffQueue() {
|
|
10
|
+
const pending = writable(0);
|
|
11
|
+
const items = writable([]);
|
|
12
|
+
const lastSync = writable(null);
|
|
13
|
+
const isProcessing = writable(false);
|
|
14
|
+
|
|
15
|
+
async function refresh() {
|
|
16
|
+
const [count, queueItems] = await Promise.all([
|
|
17
|
+
getPendingCount(),
|
|
18
|
+
getQueueItems(),
|
|
19
|
+
]);
|
|
20
|
+
pending.set(count);
|
|
21
|
+
items.set(queueItems);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
onMount(() => {
|
|
25
|
+
queueMicrotask(() => refresh());
|
|
26
|
+
|
|
27
|
+
function onSync(e) {
|
|
28
|
+
isProcessing.set(false);
|
|
29
|
+
lastSync.set({ succeeded: e.detail.succeeded, failed: e.detail.failed });
|
|
30
|
+
refresh();
|
|
31
|
+
}
|
|
32
|
+
function onChange() { refresh(); }
|
|
33
|
+
function onProgress() { isProcessing.set(true); }
|
|
34
|
+
|
|
35
|
+
window.addEventListener("mutation-sync-complete", onSync);
|
|
36
|
+
window.addEventListener("mutation-queue-changed", onChange);
|
|
37
|
+
window.addEventListener("mutation-sync-progress", onProgress);
|
|
38
|
+
|
|
39
|
+
return () => {
|
|
40
|
+
window.removeEventListener("mutation-sync-complete", onSync);
|
|
41
|
+
window.removeEventListener("mutation-queue-changed", onChange);
|
|
42
|
+
window.removeEventListener("mutation-sync-progress", onProgress);
|
|
43
|
+
};
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
async function retryAll() {
|
|
47
|
+
isProcessing.set(true);
|
|
48
|
+
try {
|
|
49
|
+
await processMutationQueue();
|
|
50
|
+
} finally {
|
|
51
|
+
await refresh();
|
|
52
|
+
isProcessing.set(false);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
return { pending, items, lastSync, isProcessing, retryAll };
|
|
57
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { writable } from "svelte/store";
|
|
2
|
+
import { onMount } from "svelte";
|
|
3
|
+
import {
|
|
4
|
+
getPendingCount,
|
|
5
|
+
getQueueItems,
|
|
6
|
+
processMutationQueue,
|
|
7
|
+
} from "../mutation/queue";
|
|
8
|
+
import type { MutationQueueItem } from "../swoff.d";
|
|
9
|
+
|
|
10
|
+
export interface MutationQueueState {
|
|
11
|
+
pending: number;
|
|
12
|
+
items: MutationQueueItem[];
|
|
13
|
+
lastSync: { succeeded: number; failed: number } | null;
|
|
14
|
+
isProcessing: boolean;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function useSwoffQueue(): MutationQueueState & {
|
|
18
|
+
retryAll: () => Promise<void>;
|
|
19
|
+
} {
|
|
20
|
+
const pending = writable(0);
|
|
21
|
+
const items = writable<MutationQueueItem[]>([]);
|
|
22
|
+
const lastSync = writable<{ succeeded: number; failed: number } | null>(null);
|
|
23
|
+
const isProcessing = writable(false);
|
|
24
|
+
|
|
25
|
+
async function refresh() {
|
|
26
|
+
const [count, queueItems] = await Promise.all([
|
|
27
|
+
getPendingCount(),
|
|
28
|
+
getQueueItems(),
|
|
29
|
+
]);
|
|
30
|
+
pending.set(count);
|
|
31
|
+
items.set(queueItems);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
onMount(() => {
|
|
35
|
+
queueMicrotask(() => refresh());
|
|
36
|
+
|
|
37
|
+
function onSync(e: CustomEvent) {
|
|
38
|
+
isProcessing.set(false);
|
|
39
|
+
lastSync.set({ succeeded: e.detail.succeeded, failed: e.detail.failed });
|
|
40
|
+
refresh();
|
|
41
|
+
}
|
|
42
|
+
function onChange() { refresh(); }
|
|
43
|
+
function onProgress() { isProcessing.set(true); }
|
|
44
|
+
|
|
45
|
+
window.addEventListener("mutation-sync-complete", onSync);
|
|
46
|
+
window.addEventListener("mutation-queue-changed", onChange);
|
|
47
|
+
window.addEventListener("mutation-sync-progress", onProgress);
|
|
48
|
+
|
|
49
|
+
return () => {
|
|
50
|
+
window.removeEventListener("mutation-sync-complete", onSync);
|
|
51
|
+
window.removeEventListener("mutation-queue-changed", onChange);
|
|
52
|
+
window.removeEventListener("mutation-sync-progress", onProgress);
|
|
53
|
+
};
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
async function retryAll() {
|
|
57
|
+
isProcessing.set(true);
|
|
58
|
+
try {
|
|
59
|
+
await processMutationQueue();
|
|
60
|
+
} finally {
|
|
61
|
+
await refresh();
|
|
62
|
+
isProcessing.set(false);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
return { pending, items, lastSync, isProcessing, retryAll };
|
|
67
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { writable } from "svelte/store";
|
|
2
|
+
import { resetSwoff } from "../reset.js";
|
|
3
|
+
|
|
4
|
+
export function useSwoffReset() {
|
|
5
|
+
const isResetting = writable(false);
|
|
6
|
+
const error = writable(null);
|
|
7
|
+
|
|
8
|
+
async function reset(options) {
|
|
9
|
+
isResetting.set(true);
|
|
10
|
+
error.set(null);
|
|
11
|
+
try {
|
|
12
|
+
await resetSwoff(options);
|
|
13
|
+
} catch (e) {
|
|
14
|
+
error.set(e instanceof Error ? e : new Error(String(e)));
|
|
15
|
+
} finally {
|
|
16
|
+
isResetting.set(false);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
return { reset, isResetting, error };
|
|
21
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { writable } from "svelte/store";
|
|
2
|
+
import { resetSwoff } from "../reset";
|
|
3
|
+
import type { ResetSwoffOptions } from "../reset";
|
|
4
|
+
|
|
5
|
+
interface UseSwoffResetResult {
|
|
6
|
+
reset: (options?: ResetSwoffOptions) => Promise<void>;
|
|
7
|
+
isResetting: boolean;
|
|
8
|
+
error: Error | null;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function useSwoffReset(): UseSwoffResetResult {
|
|
12
|
+
const isResetting = writable(false);
|
|
13
|
+
const error = writable<Error | null>(null);
|
|
14
|
+
|
|
15
|
+
async function reset(options?: ResetSwoffOptions) {
|
|
16
|
+
isResetting.set(true);
|
|
17
|
+
error.set(null);
|
|
18
|
+
try {
|
|
19
|
+
await resetSwoff(options);
|
|
20
|
+
} catch (e) {
|
|
21
|
+
error.set(e instanceof Error ? e : new Error(String(e)));
|
|
22
|
+
} finally {
|
|
23
|
+
isResetting.set(false);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
return {
|
|
28
|
+
reset,
|
|
29
|
+
isResetting: isResetting,
|
|
30
|
+
error: error,
|
|
31
|
+
};
|
|
32
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { writable } from "svelte/store";
|
|
2
|
+
import { onMount } from "svelte";
|
|
3
|
+
import { getStorageEstimate, formatBytes } from "../storage.js";
|
|
4
|
+
|
|
5
|
+
export function useSwoffStorage(autoRefresh = true) {
|
|
6
|
+
const usage = writable(0);
|
|
7
|
+
const quota = writable(0);
|
|
8
|
+
const percentUsed = writable(0);
|
|
9
|
+
const formattedUsage = writable("0 B");
|
|
10
|
+
const formattedQuota = writable("0 B");
|
|
11
|
+
const loading = writable(true);
|
|
12
|
+
const error = writable(null);
|
|
13
|
+
|
|
14
|
+
async function check() {
|
|
15
|
+
try {
|
|
16
|
+
const result = await getStorageEstimate();
|
|
17
|
+
usage.set(result.usage);
|
|
18
|
+
quota.set(result.quota);
|
|
19
|
+
percentUsed.set(result.percentUsed);
|
|
20
|
+
formattedUsage.set(formatBytes(result.usage));
|
|
21
|
+
formattedQuota.set(formatBytes(result.quota));
|
|
22
|
+
loading.set(false);
|
|
23
|
+
error.set(null);
|
|
24
|
+
} catch (err) {
|
|
25
|
+
loading.set(false);
|
|
26
|
+
error.set(String(err));
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
onMount(() => {
|
|
31
|
+
check();
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
if (autoRefresh) {
|
|
35
|
+
onMount(() => {
|
|
36
|
+
function onVisible() {
|
|
37
|
+
if (document.visibilityState === "visible") check();
|
|
38
|
+
}
|
|
39
|
+
document.addEventListener("visibilitychange", onVisible);
|
|
40
|
+
return () => {
|
|
41
|
+
document.removeEventListener("visibilitychange", onVisible);
|
|
42
|
+
};
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
return { usage, quota, percentUsed, formattedUsage, formattedQuota, loading, error };
|
|
47
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { writable } from "svelte/store";
|
|
2
|
+
import { onMount } from "svelte";
|
|
3
|
+
import { getStorageEstimate, formatBytes } from "../storage";
|
|
4
|
+
|
|
5
|
+
export function useSwoffStorage(autoRefresh = true) {
|
|
6
|
+
const usage = writable(0);
|
|
7
|
+
const quota = writable(0);
|
|
8
|
+
const percentUsed = writable(0);
|
|
9
|
+
const formattedUsage = writable("0 B");
|
|
10
|
+
const formattedQuota = writable("0 B");
|
|
11
|
+
const loading = writable(true);
|
|
12
|
+
const error = writable<string | null>(null);
|
|
13
|
+
|
|
14
|
+
async function check() {
|
|
15
|
+
try {
|
|
16
|
+
const result = await getStorageEstimate();
|
|
17
|
+
usage.set(result.usage);
|
|
18
|
+
quota.set(result.quota);
|
|
19
|
+
percentUsed.set(result.percentUsed);
|
|
20
|
+
formattedUsage.set(formatBytes(result.usage));
|
|
21
|
+
formattedQuota.set(formatBytes(result.quota));
|
|
22
|
+
loading.set(false);
|
|
23
|
+
error.set(null);
|
|
24
|
+
} catch (err) {
|
|
25
|
+
loading.set(false);
|
|
26
|
+
error.set(String(err));
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
onMount(() => {
|
|
31
|
+
check();
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
if (autoRefresh) {
|
|
35
|
+
onMount(() => {
|
|
36
|
+
function onVisible() {
|
|
37
|
+
if (document.visibilityState === "visible") check();
|
|
38
|
+
}
|
|
39
|
+
document.addEventListener("visibilitychange", onVisible);
|
|
40
|
+
return () => {
|
|
41
|
+
document.removeEventListener("visibilitychange", onVisible);
|
|
42
|
+
};
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
return { usage, quota, percentUsed, formattedUsage, formattedQuota, loading, error };
|
|
47
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { writable } from "svelte/store";
|
|
2
|
+
import { onMount } from "svelte";
|
|
3
|
+
import { retrySync } from "../mutation/sync.js";
|
|
4
|
+
|
|
5
|
+
export function useSwoffSync() {
|
|
6
|
+
const supported = writable(
|
|
7
|
+
typeof window !== "undefined" && typeof navigator !== "undefined"
|
|
8
|
+
? "serviceWorker" in navigator && "SyncManager" in window
|
|
9
|
+
: false,
|
|
10
|
+
);
|
|
11
|
+
const registered = writable(false);
|
|
12
|
+
const lastSync = writable(null);
|
|
13
|
+
|
|
14
|
+
onMount(() => {
|
|
15
|
+
function onSyncComplete(e) {
|
|
16
|
+
registered.set(true);
|
|
17
|
+
lastSync.set({ succeeded: e.detail.succeeded, failed: e.detail.failed });
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
window.addEventListener("background-sync-complete", onSyncComplete);
|
|
21
|
+
return () => {
|
|
22
|
+
window.removeEventListener("background-sync-complete", onSyncComplete);
|
|
23
|
+
};
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
async function triggerSync() {
|
|
27
|
+
await retrySync();
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
return { supported, registered, lastSync, triggerSync };
|
|
31
|
+
}
|