@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.
Files changed (117) hide show
  1. package/dist/lib/commands/generate-guide.js +16 -16
  2. package/dist/lib/commands/generate-guide.js.map +1 -1
  3. package/dist/lib/commands/init.js +127 -17
  4. package/dist/lib/commands/init.js.map +1 -1
  5. package/dist/lib/config/validator.js +1 -1
  6. package/dist/lib/config/validator.js.map +1 -1
  7. package/dist/lib/generators/file-generators/client-injector-bundle.js +2 -1
  8. package/dist/lib/generators/file-generators/client-injector-bundle.js.map +1 -1
  9. package/dist/lib/generators/file-generators/client-injector.js +2 -1
  10. package/dist/lib/generators/file-generators/client-injector.js.map +1 -1
  11. package/dist/lib/generators/file-generators/context.js +1 -1
  12. package/dist/lib/generators/file-generators/context.js.map +1 -1
  13. package/dist/lib/generators/file-generators/generate-framework-adapters.js +30 -23
  14. package/dist/lib/generators/file-generators/generate-framework-adapters.js.map +1 -1
  15. package/dist/lib/generators/sw-sections/background-precache.js +8 -1
  16. package/dist/lib/generators/sw-sections/background-precache.js.map +1 -1
  17. package/dist/lib/generators/sw-sections/batch-refresh-queue.js +2 -5
  18. package/dist/lib/generators/sw-sections/batch-refresh-queue.js.map +1 -1
  19. package/dist/lib/generators/sw-sections/fetch-handler.js +29 -7
  20. package/dist/lib/generators/sw-sections/fetch-handler.js.map +1 -1
  21. package/dist/lib/generators/swoff-files-generator.js +2 -1
  22. package/dist/lib/generators/swoff-files-generator.js.map +1 -1
  23. package/dist/lib/shared/config-types.js +1 -0
  24. package/dist/lib/shared/config-types.js.map +1 -1
  25. package/dist/lib/utils/detect-framework.js +4 -1
  26. package/dist/lib/utils/detect-framework.js.map +1 -1
  27. package/dist/runtime/client-injector-bundle.js +2 -2
  28. package/dist/runtime/client-injector-bundle.js.map +1 -1
  29. package/dist/runtime/client-injector.js +3 -2
  30. package/dist/runtime/client-injector.js.map +1 -1
  31. package/dist/runtime/gql-wrapper.js +3 -1
  32. package/dist/runtime/gql-wrapper.js.map +1 -1
  33. package/dist/runtime/swoff-api-bundle.js +2 -1
  34. package/dist/runtime/swoff-api-bundle.js.map +1 -1
  35. package/package.json +1 -1
  36. package/templates/react/{useOfflineAnalytics.jsx → useSwoffAnalytics.jsx} +3 -3
  37. package/templates/react/{useOfflineAnalytics.tsx → useSwoffAnalytics.tsx} +3 -3
  38. package/templates/react/{useAuth.jsx → useSwoffAuth.jsx} +2 -2
  39. package/templates/react/{useAuth.tsx → useSwoffAuth.tsx} +2 -2
  40. package/templates/react/{useCachedFetch.jsx → useSwoffFetch.jsx} +6 -6
  41. package/templates/react/{useCachedFetch.tsx → useSwoffFetch.tsx} +7 -7
  42. package/templates/react/{useMutation.jsx → useSwoffMutation.jsx} +3 -3
  43. package/templates/react/{useMutation.tsx → useSwoffMutation.tsx} +3 -3
  44. package/templates/react/{useMutationState.jsx → useSwoffMutationState.jsx} +1 -1
  45. package/templates/react/{useMutationState.tsx → useSwoffMutationState.tsx} +2 -2
  46. package/templates/react/{useNetworkStatus.jsx → useSwoffNetwork.jsx} +2 -2
  47. package/templates/react/{useNetworkStatus.tsx → useSwoffNetwork.tsx} +2 -2
  48. package/templates/react/{usePrecacheProgress.jsx → useSwoffPrecache.jsx} +4 -7
  49. package/templates/react/{usePrecacheProgress.tsx → useSwoffPrecache.tsx} +4 -7
  50. package/templates/react/{usePrefetch.jsx → useSwoffPrefetch.jsx} +2 -2
  51. package/templates/react/{usePrefetch.tsx → useSwoffPrefetch.tsx} +2 -2
  52. package/templates/react/{usePushSubscription.jsx → useSwoffPush.jsx} +1 -1
  53. package/templates/react/{usePushSubscription.tsx → useSwoffPush.tsx} +1 -1
  54. package/templates/react/{usePwaInstall.jsx → useSwoffPwa.jsx} +1 -1
  55. package/templates/react/{usePwaInstall.tsx → useSwoffPwa.tsx} +1 -1
  56. package/templates/react/{useMutationQueue.jsx → useSwoffQueue.jsx} +2 -2
  57. package/templates/react/{useMutationQueue.tsx → useSwoffQueue.tsx} +2 -2
  58. package/templates/react/{useStorageEstimate.jsx → useSwoffStorage.jsx} +2 -2
  59. package/templates/react/{useStorageEstimate.tsx → useSwoffStorage.tsx} +2 -2
  60. package/templates/react/{useBackgroundSync.jsx → useSwoffSync.jsx} +1 -1
  61. package/templates/react/{useBackgroundSync.tsx → useSwoffSync.tsx} +1 -1
  62. package/templates/svelte/useSwoffAnalytics.js +31 -0
  63. package/templates/svelte/useSwoffAnalytics.ts +37 -0
  64. package/templates/svelte/useSwoffAuth.js +95 -0
  65. package/templates/svelte/useSwoffAuth.ts +96 -0
  66. package/templates/svelte/useSwoffFetch.js +97 -0
  67. package/templates/svelte/useSwoffFetch.ts +112 -0
  68. package/templates/svelte/useSwoffMutation.js +110 -0
  69. package/templates/svelte/useSwoffMutation.ts +141 -0
  70. package/templates/svelte/useSwoffMutationState.js +27 -0
  71. package/templates/svelte/useSwoffMutationState.ts +28 -0
  72. package/templates/svelte/useSwoffNetwork.js +75 -0
  73. package/templates/svelte/useSwoffNetwork.ts +75 -0
  74. package/templates/svelte/useSwoffPrecache.js +19 -0
  75. package/templates/svelte/useSwoffPrecache.ts +19 -0
  76. package/templates/svelte/useSwoffPrefetch.js +19 -0
  77. package/templates/svelte/useSwoffPrefetch.ts +20 -0
  78. package/templates/svelte/useSwoffPush.js +82 -0
  79. package/templates/svelte/useSwoffPush.ts +82 -0
  80. package/templates/svelte/useSwoffPwa.js +27 -0
  81. package/templates/svelte/useSwoffPwa.ts +27 -0
  82. package/templates/svelte/useSwoffQueue.js +57 -0
  83. package/templates/svelte/useSwoffQueue.ts +67 -0
  84. package/templates/svelte/useSwoffReset.js +21 -0
  85. package/templates/svelte/useSwoffReset.ts +32 -0
  86. package/templates/svelte/useSwoffStorage.js +47 -0
  87. package/templates/svelte/useSwoffStorage.ts +47 -0
  88. package/templates/svelte/useSwoffSync.js +31 -0
  89. package/templates/svelte/useSwoffSync.ts +31 -0
  90. package/templates/vue/useSwoffAnalytics.js +46 -0
  91. package/templates/vue/useSwoffAnalytics.ts +54 -0
  92. package/templates/vue/useSwoffAuth.js +106 -0
  93. package/templates/vue/useSwoffAuth.ts +118 -0
  94. package/templates/vue/useSwoffFetch.js +127 -0
  95. package/templates/vue/useSwoffFetch.ts +143 -0
  96. package/templates/vue/useSwoffMutation.js +124 -0
  97. package/templates/vue/useSwoffMutation.ts +155 -0
  98. package/templates/vue/useSwoffMutationState.js +35 -0
  99. package/templates/vue/useSwoffMutationState.ts +36 -0
  100. package/templates/vue/useSwoffNetwork.js +86 -0
  101. package/templates/vue/useSwoffNetwork.ts +89 -0
  102. package/templates/vue/useSwoffPrecache.js +19 -0
  103. package/templates/vue/useSwoffPrecache.ts +19 -0
  104. package/templates/vue/useSwoffPrefetch.js +33 -0
  105. package/templates/vue/useSwoffPrefetch.ts +41 -0
  106. package/templates/vue/useSwoffPush.js +81 -0
  107. package/templates/vue/useSwoffPush.ts +81 -0
  108. package/templates/vue/useSwoffPwa.js +27 -0
  109. package/templates/vue/useSwoffPwa.ts +27 -0
  110. package/templates/vue/useSwoffQueue.js +70 -0
  111. package/templates/vue/useSwoffQueue.ts +82 -0
  112. package/templates/vue/useSwoffReset.js +32 -0
  113. package/templates/vue/useSwoffReset.ts +39 -0
  114. package/templates/vue/useSwoffStorage.js +56 -0
  115. package/templates/vue/useSwoffStorage.ts +57 -0
  116. package/templates/vue/useSwoffSync.js +30 -0
  117. package/templates/vue/useSwoffSync.ts +30 -0
@@ -0,0 +1,81 @@
1
+ import { ref, onMounted, onUnmounted } from "vue";
2
+ import {
3
+ subscribeToPush,
4
+ unsubscribeFromPush,
5
+ isSubscribed,
6
+ getPushSubscription,
7
+ } from "../realtime/notifications.js";
8
+
9
+ export function useSwoffPush() {
10
+ const subscribed = ref(false);
11
+ const subscription = ref(null);
12
+ const permission = ref(
13
+ typeof Notification !== "undefined" ? Notification.permission : "denied",
14
+ );
15
+ const loading = ref(true);
16
+
17
+ onMounted(() => {
18
+ let cancelled = false;
19
+
20
+ isSubscribed().then((subd) => {
21
+ if (cancelled) return;
22
+ if (subd) {
23
+ getPushSubscription().then((sub) => {
24
+ if (cancelled) return;
25
+ subscribed.value = true;
26
+ subscription.value = sub;
27
+ permission.value = Notification.permission;
28
+ loading.value = false;
29
+ });
30
+ } else {
31
+ subscribed.value = false;
32
+ subscription.value = null;
33
+ permission.value = Notification.permission;
34
+ loading.value = false;
35
+ }
36
+ });
37
+
38
+ function onSubChanged(e) {
39
+ if (cancelled) return;
40
+ if (!e.detail.subscribed) {
41
+ subscribed.value = false;
42
+ subscription.value = null;
43
+ permission.value = Notification.permission;
44
+ loading.value = false;
45
+ } else {
46
+ getPushSubscription().then((sub) => {
47
+ if (cancelled) return;
48
+ subscribed.value = true;
49
+ subscription.value = sub;
50
+ permission.value = Notification.permission;
51
+ loading.value = false;
52
+ });
53
+ }
54
+ }
55
+
56
+ function onPermissionChanged(e) {
57
+ if (cancelled) return;
58
+ permission.value = e.detail.permission;
59
+ }
60
+
61
+ window.addEventListener("push-subscription-changed", onSubChanged);
62
+ window.addEventListener("push-permission-changed", onPermissionChanged);
63
+
64
+ onUnmounted(() => {
65
+ cancelled = true;
66
+ window.removeEventListener("push-subscription-changed", onSubChanged);
67
+ window.removeEventListener("push-permission-changed", onPermissionChanged);
68
+ });
69
+ });
70
+
71
+ async function subscribe() {
72
+ const sub = await subscribeToPush();
73
+ return sub !== null;
74
+ }
75
+
76
+ async function unsubscribe() {
77
+ await unsubscribeFromPush();
78
+ }
79
+
80
+ return { subscribed, subscription, permission, loading, subscribe, unsubscribe };
81
+ }
@@ -0,0 +1,81 @@
1
+ import { ref, onMounted, onUnmounted } from "vue";
2
+ import {
3
+ subscribeToPush,
4
+ unsubscribeFromPush,
5
+ isSubscribed,
6
+ getPushSubscription,
7
+ } from "../realtime/notifications";
8
+
9
+ export function useSwoffPush() {
10
+ const subscribed = ref(false);
11
+ const subscription = ref<PushSubscription | null>(null);
12
+ const permission = ref(
13
+ typeof Notification !== "undefined" ? Notification.permission : "denied",
14
+ );
15
+ const loading = ref(true);
16
+
17
+ onMounted(() => {
18
+ let cancelled = false;
19
+
20
+ isSubscribed().then((subd) => {
21
+ if (cancelled) return;
22
+ if (subd) {
23
+ getPushSubscription().then((sub) => {
24
+ if (cancelled) return;
25
+ subscribed.value = true;
26
+ subscription.value = sub;
27
+ permission.value = Notification.permission;
28
+ loading.value = false;
29
+ });
30
+ } else {
31
+ subscribed.value = false;
32
+ subscription.value = null;
33
+ permission.value = Notification.permission;
34
+ loading.value = false;
35
+ }
36
+ });
37
+
38
+ function onSubChanged(e: CustomEvent) {
39
+ if (cancelled) return;
40
+ if (!e.detail.subscribed) {
41
+ subscribed.value = false;
42
+ subscription.value = null;
43
+ permission.value = Notification.permission;
44
+ loading.value = false;
45
+ } else {
46
+ getPushSubscription().then((sub) => {
47
+ if (cancelled) return;
48
+ subscribed.value = true;
49
+ subscription.value = sub;
50
+ permission.value = Notification.permission;
51
+ loading.value = false;
52
+ });
53
+ }
54
+ }
55
+
56
+ function onPermissionChanged(e: CustomEvent) {
57
+ if (cancelled) return;
58
+ permission.value = e.detail.permission;
59
+ }
60
+
61
+ window.addEventListener("push-subscription-changed", onSubChanged);
62
+ window.addEventListener("push-permission-changed", onPermissionChanged);
63
+
64
+ onUnmounted(() => {
65
+ cancelled = true;
66
+ window.removeEventListener("push-subscription-changed", onSubChanged);
67
+ window.removeEventListener("push-permission-changed", onPermissionChanged);
68
+ });
69
+ });
70
+
71
+ async function subscribe() {
72
+ const sub = await subscribeToPush();
73
+ return sub !== null;
74
+ }
75
+
76
+ async function unsubscribe() {
77
+ await unsubscribeFromPush();
78
+ }
79
+
80
+ return { subscribed, subscription, permission, loading, subscribe, unsubscribe };
81
+ }
@@ -0,0 +1,27 @@
1
+ import { ref, onMounted, onUnmounted } from "vue";
2
+ import { promptInstall } from "../pwa/prompt.js";
3
+
4
+ export function useSwoffPwa() {
5
+ const canInstall = ref(false);
6
+
7
+ function onReady() { canInstall.value = true; }
8
+ function onDone() { canInstall.value = false; }
9
+
10
+ onMounted(() => {
11
+ window.addEventListener("pwa-installable", onReady);
12
+ window.addEventListener("pwa-installed", onDone);
13
+ window.addEventListener("pwa-dismissed", onDone);
14
+ });
15
+
16
+ onUnmounted(() => {
17
+ window.removeEventListener("pwa-installable", onReady);
18
+ window.removeEventListener("pwa-installed", onDone);
19
+ window.removeEventListener("pwa-dismissed", onDone);
20
+ });
21
+
22
+ async function install() {
23
+ await promptInstall();
24
+ }
25
+
26
+ return { canInstall, install };
27
+ }
@@ -0,0 +1,27 @@
1
+ import { ref, onMounted, onUnmounted } from "vue";
2
+ import { promptInstall } from "../pwa/prompt";
3
+
4
+ export function useSwoffPwa() {
5
+ const canInstall = ref(false);
6
+
7
+ function onReady() { canInstall.value = true; }
8
+ function onDone() { canInstall.value = false; }
9
+
10
+ onMounted(() => {
11
+ window.addEventListener("pwa-installable", onReady);
12
+ window.addEventListener("pwa-installed", onDone);
13
+ window.addEventListener("pwa-dismissed", onDone);
14
+ });
15
+
16
+ onUnmounted(() => {
17
+ window.removeEventListener("pwa-installable", onReady);
18
+ window.removeEventListener("pwa-installed", onDone);
19
+ window.removeEventListener("pwa-dismissed", onDone);
20
+ });
21
+
22
+ async function install() {
23
+ await promptInstall();
24
+ }
25
+
26
+ return { canInstall, install };
27
+ }
@@ -0,0 +1,70 @@
1
+ import { ref, onMounted, onUnmounted } from "vue";
2
+ import {
3
+ getPendingCount,
4
+ getQueueItems,
5
+ processMutationQueue,
6
+ } from "../mutation/queue.js";
7
+
8
+ /**
9
+ * Reactive mutation queue state: pending count, items, last sync info, and processing flag.
10
+ *
11
+ * Usage:
12
+ * const { pending, items, lastSync, isProcessing, retryAll } = useSwoffQueue();
13
+ *
14
+ * // Show pending badge:
15
+ * <span v-if="pending > 0">{{ pending }} pending</span>
16
+ *
17
+ * // Retry all queued mutations:
18
+ * <button @click="retryAll" :disabled="isProcessing">
19
+ * {{ isProcessing ? "Syncing..." : "Retry All" }}
20
+ * </button>
21
+ */
22
+ export function useSwoffQueue() {
23
+ const pending = ref(0);
24
+ const items = ref([]);
25
+ const lastSync = ref(null);
26
+ const isProcessing = ref(false);
27
+
28
+ async function refresh() {
29
+ const [count, queueItems] = await Promise.all([
30
+ getPendingCount(),
31
+ getQueueItems(),
32
+ ]);
33
+ pending.value = count;
34
+ items.value = queueItems;
35
+ }
36
+
37
+ onMounted(() => {
38
+ queueMicrotask(() => refresh());
39
+
40
+ function onSync(e) {
41
+ isProcessing.value = false;
42
+ lastSync.value = { succeeded: e.detail.succeeded, failed: e.detail.failed };
43
+ refresh();
44
+ }
45
+ function onChange() { refresh(); }
46
+ function onProgress() { isProcessing.value = true; }
47
+
48
+ window.addEventListener("mutation-sync-complete", onSync);
49
+ window.addEventListener("mutation-queue-changed", onChange);
50
+ window.addEventListener("mutation-sync-progress", onProgress);
51
+
52
+ onUnmounted(() => {
53
+ window.removeEventListener("mutation-sync-complete", onSync);
54
+ window.removeEventListener("mutation-queue-changed", onChange);
55
+ window.removeEventListener("mutation-sync-progress", onProgress);
56
+ });
57
+ });
58
+
59
+ async function retryAll() {
60
+ isProcessing.value = true;
61
+ try {
62
+ await processMutationQueue();
63
+ } finally {
64
+ await refresh();
65
+ isProcessing.value = false;
66
+ }
67
+ }
68
+
69
+ return { pending, items, lastSync, isProcessing, retryAll };
70
+ }
@@ -0,0 +1,82 @@
1
+ import { ref, onMounted, onUnmounted } from "vue";
2
+ import {
3
+ getPendingCount,
4
+ getQueueItems,
5
+ processMutationQueue,
6
+ } from "../mutation/queue";
7
+ import type { MutationQueueItem } from "../swoff.d";
8
+
9
+ export interface MutationQueueState {
10
+ pending: number;
11
+ items: MutationQueueItem[];
12
+ lastSync: { succeeded: number; failed: number } | null;
13
+ isProcessing: boolean;
14
+ }
15
+
16
+ /**
17
+ * Reactive mutation queue state: pending count, items, last sync info, and processing flag.
18
+ *
19
+ * Usage:
20
+ * const { pending, items, lastSync, isProcessing, retryAll } = useSwoffQueue();
21
+ *
22
+ * // Show pending badge:
23
+ * <span v-if="pending > 0">{{ pending }} pending</span>
24
+ *
25
+ * // Retry all queued mutations:
26
+ * <button @click="retryAll" :disabled="isProcessing">
27
+ * {{ isProcessing ? "Syncing..." : "Retry All" }}
28
+ * </button>
29
+ *
30
+ * @returns {{ pending, items, lastSync, isProcessing, retryAll }}
31
+ */
32
+ export function useSwoffQueue(): MutationQueueState & {
33
+ retryAll: () => Promise<void>;
34
+ } {
35
+ const pending = ref(0);
36
+ const items = ref<MutationQueueItem[]>([]);
37
+ const lastSync = ref<{ succeeded: number; failed: number } | null>(null);
38
+ const isProcessing = ref(false);
39
+
40
+ async function refresh() {
41
+ const [count, queueItems] = await Promise.all([
42
+ getPendingCount(),
43
+ getQueueItems(),
44
+ ]);
45
+ pending.value = count;
46
+ items.value = queueItems;
47
+ }
48
+
49
+ onMounted(() => {
50
+ queueMicrotask(() => refresh());
51
+
52
+ function onSync(e: CustomEvent) {
53
+ isProcessing.value = false;
54
+ lastSync.value = { succeeded: e.detail.succeeded, failed: e.detail.failed };
55
+ refresh();
56
+ }
57
+ function onChange() { refresh(); }
58
+ function onProgress() { isProcessing.value = true; }
59
+
60
+ window.addEventListener("mutation-sync-complete", onSync);
61
+ window.addEventListener("mutation-queue-changed", onChange);
62
+ window.addEventListener("mutation-sync-progress", onProgress);
63
+
64
+ onUnmounted(() => {
65
+ window.removeEventListener("mutation-sync-complete", onSync);
66
+ window.removeEventListener("mutation-queue-changed", onChange);
67
+ window.removeEventListener("mutation-sync-progress", onProgress);
68
+ });
69
+ });
70
+
71
+ async function retryAll() {
72
+ isProcessing.value = true;
73
+ try {
74
+ await processMutationQueue();
75
+ } finally {
76
+ await refresh();
77
+ isProcessing.value = false;
78
+ }
79
+ }
80
+
81
+ return { pending, items, lastSync, isProcessing, retryAll };
82
+ }
@@ -0,0 +1,32 @@
1
+ import { ref } from "vue";
2
+ import { resetSwoff } from "../reset.js";
3
+
4
+ /**
5
+ * Composable exposing swoff's nuclear reset with reactive state.
6
+ *
7
+ * Usage:
8
+ * const { reset, isResetting, error } = useSwoffReset();
9
+ *
10
+ * <button @click="reset()" :disabled="isResetting">
11
+ * {{ isResetting ? "Resetting..." : "Reset Swoff" }}
12
+ * </button>
13
+ * <p v-if="error" style="color: red">{{ error.message }}</p>
14
+ */
15
+ export function useSwoffReset() {
16
+ const isResetting = ref(false);
17
+ const error = ref(null);
18
+
19
+ async function reset(options) {
20
+ isResetting.value = true;
21
+ error.value = null;
22
+ try {
23
+ await resetSwoff(options);
24
+ } catch (e) {
25
+ error.value = e instanceof Error ? e : new Error(String(e));
26
+ } finally {
27
+ isResetting.value = false;
28
+ }
29
+ }
30
+
31
+ return { reset, isResetting: isResetting.value, error: error.value };
32
+ }
@@ -0,0 +1,39 @@
1
+ import { ref } from "vue";
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
+ /**
12
+ * Composable exposing swoff's nuclear reset with reactive state.
13
+ *
14
+ * Usage:
15
+ * const { reset, isResetting, error } = useSwoffReset();
16
+ *
17
+ * <button @click="reset()" :disabled="isResetting">
18
+ * {{ isResetting ? "Resetting..." : "Reset Swoff" }}
19
+ * </button>
20
+ * <p v-if="error" style="color: red">{{ error.message }}</p>
21
+ */
22
+ export function useSwoffReset(): UseSwoffResetResult {
23
+ const isResetting = ref(false);
24
+ const error = ref<Error | null>(null);
25
+
26
+ async function reset(options?: ResetSwoffOptions) {
27
+ isResetting.value = true;
28
+ error.value = null;
29
+ try {
30
+ await resetSwoff(options);
31
+ } catch (e) {
32
+ error.value = e instanceof Error ? e : new Error(String(e));
33
+ } finally {
34
+ isResetting.value = false;
35
+ }
36
+ }
37
+
38
+ return { reset, isResetting: isResetting.value, error: error.value };
39
+ }
@@ -0,0 +1,56 @@
1
+ import { ref, onMounted, onUnmounted } from "vue";
2
+ import { getStorageEstimate, formatBytes } from "../storage.js";
3
+
4
+ /**
5
+ * Reactive storage estimate. Shows available quota and usage.
6
+ *
7
+ * Usage:
8
+ * const { usage, quota, percentUsed, formattedUsage, formattedQuota, loading } = useSwoffStorage();
9
+ *
10
+ * <div v-if="!loading && percentUsed > 80">Storage almost full</div>
11
+ *
12
+ * @param autoRefresh - Re-check on visibility change (default true)
13
+ */
14
+ export function useSwoffStorage(autoRefresh = true) {
15
+ const usage = ref(0);
16
+ const quota = ref(0);
17
+ const percentUsed = ref(0);
18
+ const formattedUsage = ref("0 B");
19
+ const formattedQuota = ref("0 B");
20
+ const loading = ref(true);
21
+ const error = ref(null);
22
+
23
+ async function check() {
24
+ try {
25
+ const result = await getStorageEstimate();
26
+ usage.value = result.usage;
27
+ quota.value = result.quota;
28
+ percentUsed.value = result.percentUsed;
29
+ formattedUsage.value = formatBytes(result.usage);
30
+ formattedQuota.value = formatBytes(result.quota);
31
+ loading.value = false;
32
+ error.value = null;
33
+ } catch (err) {
34
+ loading.value = false;
35
+ error.value = String(err);
36
+ }
37
+ }
38
+
39
+ onMounted(() => {
40
+ check();
41
+ });
42
+
43
+ if (autoRefresh) {
44
+ onMounted(() => {
45
+ function onVisible() {
46
+ if (document.visibilityState === "visible") check();
47
+ }
48
+ document.addEventListener("visibilitychange", onVisible);
49
+ onUnmounted(() => {
50
+ document.removeEventListener("visibilitychange", onVisible);
51
+ });
52
+ });
53
+ }
54
+
55
+ return { usage, quota, percentUsed, formattedUsage, formattedQuota, loading, error };
56
+ }
@@ -0,0 +1,57 @@
1
+ import { ref, onMounted, onUnmounted } from "vue";
2
+ import { getStorageEstimate, formatBytes } from "../storage";
3
+
4
+ /**
5
+ * Reactive storage estimate. Shows available quota and usage.
6
+ *
7
+ * Usage:
8
+ * const { usage, quota, percentUsed, formattedUsage, formattedQuota, loading } = useSwoffStorage();
9
+ *
10
+ * <div v-if="!loading && percentUsed > 80">Storage almost full</div>
11
+ *
12
+ * @param autoRefresh - Re-check on visibility change (default true)
13
+ * @returns {{ usage: Ref<number>, quota: Ref<number>, percentUsed: Ref<number>, formattedUsage: Ref<string>, formattedQuota: Ref<string>, loading: Ref<boolean>, error: Ref<string | null> }}
14
+ */
15
+ export function useSwoffStorage(autoRefresh = true) {
16
+ const usage = ref(0);
17
+ const quota = ref(0);
18
+ const percentUsed = ref(0);
19
+ const formattedUsage = ref("0 B");
20
+ const formattedQuota = ref("0 B");
21
+ const loading = ref(true);
22
+ const error = ref<string | null>(null);
23
+
24
+ async function check() {
25
+ try {
26
+ const result = await getStorageEstimate();
27
+ usage.value = result.usage;
28
+ quota.value = result.quota;
29
+ percentUsed.value = result.percentUsed;
30
+ formattedUsage.value = formatBytes(result.usage);
31
+ formattedQuota.value = formatBytes(result.quota);
32
+ loading.value = false;
33
+ error.value = null;
34
+ } catch (err) {
35
+ loading.value = false;
36
+ error.value = String(err);
37
+ }
38
+ }
39
+
40
+ onMounted(() => {
41
+ check();
42
+ });
43
+
44
+ if (autoRefresh) {
45
+ onMounted(() => {
46
+ function onVisible() {
47
+ if (document.visibilityState === "visible") check();
48
+ }
49
+ document.addEventListener("visibilitychange", onVisible);
50
+ onUnmounted(() => {
51
+ document.removeEventListener("visibilitychange", onVisible);
52
+ });
53
+ });
54
+ }
55
+
56
+ return { usage, quota, percentUsed, formattedUsage, formattedQuota, loading, error };
57
+ }
@@ -0,0 +1,30 @@
1
+ import { ref, onMounted, onUnmounted } from "vue";
2
+ import { retrySync } from "../mutation/sync.js";
3
+
4
+ export function useSwoffSync() {
5
+ const supported = ref(
6
+ typeof window !== "undefined" && typeof navigator !== "undefined"
7
+ ? "serviceWorker" in navigator && "SyncManager" in window
8
+ : false,
9
+ );
10
+ const registered = ref(false);
11
+ const lastSync = ref(null);
12
+
13
+ onMounted(() => {
14
+ function onSyncComplete(e) {
15
+ registered.value = true;
16
+ lastSync.value = { succeeded: e.detail.succeeded, failed: e.detail.failed };
17
+ }
18
+
19
+ window.addEventListener("background-sync-complete", onSyncComplete);
20
+ onUnmounted(() => {
21
+ window.removeEventListener("background-sync-complete", onSyncComplete);
22
+ });
23
+ });
24
+
25
+ async function triggerSync() {
26
+ await retrySync();
27
+ }
28
+
29
+ return { supported, registered, lastSync, triggerSync };
30
+ }
@@ -0,0 +1,30 @@
1
+ import { ref, onMounted, onUnmounted } from "vue";
2
+ import { retrySync } from "../mutation/sync";
3
+
4
+ export function useSwoffSync() {
5
+ const supported = ref(
6
+ typeof window !== "undefined" && typeof navigator !== "undefined"
7
+ ? "serviceWorker" in navigator && "SyncManager" in window
8
+ : false,
9
+ );
10
+ const registered = ref(false);
11
+ const lastSync = ref<{ succeeded: number; failed: number } | null>(null);
12
+
13
+ onMounted(() => {
14
+ function onSyncComplete(e: CustomEvent) {
15
+ registered.value = true;
16
+ lastSync.value = { succeeded: e.detail.succeeded, failed: e.detail.failed };
17
+ }
18
+
19
+ window.addEventListener("background-sync-complete", onSyncComplete);
20
+ onUnmounted(() => {
21
+ window.removeEventListener("background-sync-complete", onSyncComplete);
22
+ });
23
+ });
24
+
25
+ async function triggerSync() {
26
+ await retrySync();
27
+ }
28
+
29
+ return { supported, registered, lastSync, triggerSync };
30
+ }