@swoff/cli 0.4.1 → 0.4.4

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 (116) hide show
  1. package/dist/lib/commands/generate-guide.js +15 -15
  2. package/dist/lib/commands/generate-guide.js.map +1 -1
  3. package/dist/lib/commands/init.js +52 -6
  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 +4 -1
  12. package/dist/lib/generators/file-generators/context.js.map +1 -1
  13. package/dist/lib/generators/file-generators/generate-framework-adapters.js +28 -22
  14. package/dist/lib/generators/file-generators/generate-framework-adapters.js.map +1 -1
  15. package/dist/lib/generators/sw-sections/activate-handler.js +1 -5
  16. package/dist/lib/generators/sw-sections/activate-handler.js.map +1 -1
  17. package/dist/lib/generators/sw-sections/background-precache.js +8 -8
  18. package/dist/lib/generators/sw-sections/batch-refresh-queue.js +2 -5
  19. package/dist/lib/generators/sw-sections/batch-refresh-queue.js.map +1 -1
  20. package/dist/lib/generators/sw-sections/fetch-handler.js +14 -3
  21. package/dist/lib/generators/sw-sections/fetch-handler.js.map +1 -1
  22. package/dist/lib/shared/config-types.js +1 -0
  23. package/dist/lib/shared/config-types.js.map +1 -1
  24. package/dist/lib/utils/detect-framework.js +16 -0
  25. package/dist/lib/utils/detect-framework.js.map +1 -1
  26. package/dist/runtime/client-injector-bundle.js +2 -2
  27. package/dist/runtime/client-injector-bundle.js.map +1 -1
  28. package/dist/runtime/client-injector.js +3 -2
  29. package/dist/runtime/client-injector.js.map +1 -1
  30. package/dist/runtime/gql-wrapper.js +3 -1
  31. package/dist/runtime/gql-wrapper.js.map +1 -1
  32. package/dist/runtime/swoff-api-bundle.js +2 -1
  33. package/dist/runtime/swoff-api-bundle.js.map +1 -1
  34. package/package.json +1 -1
  35. package/templates/react/{useOfflineAnalytics.jsx → useSwoffAnalytics.jsx} +3 -3
  36. package/templates/react/{useOfflineAnalytics.tsx → useSwoffAnalytics.tsx} +3 -3
  37. package/templates/react/{useAuth.jsx → useSwoffAuth.jsx} +2 -2
  38. package/templates/react/{useAuth.tsx → useSwoffAuth.tsx} +2 -2
  39. package/templates/react/{useCachedFetch.jsx → useSwoffFetch.jsx} +6 -6
  40. package/templates/react/{useCachedFetch.tsx → useSwoffFetch.tsx} +7 -7
  41. package/templates/react/{useMutation.jsx → useSwoffMutation.jsx} +3 -3
  42. package/templates/react/{useMutation.tsx → useSwoffMutation.tsx} +3 -3
  43. package/templates/react/{useMutationState.jsx → useSwoffMutationState.jsx} +1 -1
  44. package/templates/react/{useMutationState.tsx → useSwoffMutationState.tsx} +2 -2
  45. package/templates/react/{useNetworkStatus.jsx → useSwoffNetwork.jsx} +2 -2
  46. package/templates/react/{useNetworkStatus.tsx → useSwoffNetwork.tsx} +2 -2
  47. package/templates/react/{usePrecacheProgress.jsx → useSwoffPrecache.jsx} +4 -7
  48. package/templates/react/{usePrecacheProgress.tsx → useSwoffPrecache.tsx} +4 -7
  49. package/templates/react/{usePrefetch.jsx → useSwoffPrefetch.jsx} +2 -2
  50. package/templates/react/{usePrefetch.tsx → useSwoffPrefetch.tsx} +2 -2
  51. package/templates/react/{usePushSubscription.jsx → useSwoffPush.jsx} +1 -1
  52. package/templates/react/{usePushSubscription.tsx → useSwoffPush.tsx} +1 -1
  53. package/templates/react/{usePwaInstall.jsx → useSwoffPwa.jsx} +1 -1
  54. package/templates/react/{usePwaInstall.tsx → useSwoffPwa.tsx} +1 -1
  55. package/templates/react/{useMutationQueue.jsx → useSwoffQueue.jsx} +2 -2
  56. package/templates/react/{useMutationQueue.tsx → useSwoffQueue.tsx} +2 -2
  57. package/templates/react/{useStorageEstimate.jsx → useSwoffStorage.jsx} +2 -2
  58. package/templates/react/{useStorageEstimate.tsx → useSwoffStorage.tsx} +2 -2
  59. package/templates/react/{useBackgroundSync.jsx → useSwoffSync.jsx} +1 -1
  60. package/templates/react/{useBackgroundSync.tsx → useSwoffSync.tsx} +1 -1
  61. package/templates/svelte/useSwoffAnalytics.js +31 -0
  62. package/templates/svelte/useSwoffAnalytics.ts +37 -0
  63. package/templates/svelte/useSwoffAuth.js +95 -0
  64. package/templates/svelte/useSwoffAuth.ts +96 -0
  65. package/templates/svelte/useSwoffFetch.js +97 -0
  66. package/templates/svelte/useSwoffFetch.ts +112 -0
  67. package/templates/svelte/useSwoffMutation.js +110 -0
  68. package/templates/svelte/useSwoffMutation.ts +141 -0
  69. package/templates/svelte/useSwoffMutationState.js +27 -0
  70. package/templates/svelte/useSwoffMutationState.ts +28 -0
  71. package/templates/svelte/useSwoffNetwork.js +75 -0
  72. package/templates/svelte/useSwoffNetwork.ts +75 -0
  73. package/templates/svelte/useSwoffPrecache.js +19 -0
  74. package/templates/svelte/useSwoffPrecache.ts +19 -0
  75. package/templates/svelte/useSwoffPrefetch.js +19 -0
  76. package/templates/svelte/useSwoffPrefetch.ts +20 -0
  77. package/templates/svelte/useSwoffPush.js +82 -0
  78. package/templates/svelte/useSwoffPush.ts +82 -0
  79. package/templates/svelte/useSwoffPwa.js +27 -0
  80. package/templates/svelte/useSwoffPwa.ts +27 -0
  81. package/templates/svelte/useSwoffQueue.js +57 -0
  82. package/templates/svelte/useSwoffQueue.ts +67 -0
  83. package/templates/svelte/useSwoffReset.js +21 -0
  84. package/templates/svelte/useSwoffReset.ts +32 -0
  85. package/templates/svelte/useSwoffStorage.js +47 -0
  86. package/templates/svelte/useSwoffStorage.ts +47 -0
  87. package/templates/svelte/useSwoffSync.js +31 -0
  88. package/templates/svelte/useSwoffSync.ts +31 -0
  89. package/templates/vue/useSwoffAnalytics.js +46 -0
  90. package/templates/vue/useSwoffAnalytics.ts +54 -0
  91. package/templates/vue/useSwoffAuth.js +106 -0
  92. package/templates/vue/useSwoffAuth.ts +118 -0
  93. package/templates/vue/useSwoffFetch.js +127 -0
  94. package/templates/vue/useSwoffFetch.ts +143 -0
  95. package/templates/vue/useSwoffMutation.js +124 -0
  96. package/templates/vue/useSwoffMutation.ts +155 -0
  97. package/templates/vue/useSwoffMutationState.js +35 -0
  98. package/templates/vue/useSwoffMutationState.ts +36 -0
  99. package/templates/vue/useSwoffNetwork.js +86 -0
  100. package/templates/vue/useSwoffNetwork.ts +89 -0
  101. package/templates/vue/useSwoffPrecache.js +19 -0
  102. package/templates/vue/useSwoffPrecache.ts +19 -0
  103. package/templates/vue/useSwoffPrefetch.js +33 -0
  104. package/templates/vue/useSwoffPrefetch.ts +41 -0
  105. package/templates/vue/useSwoffPush.js +81 -0
  106. package/templates/vue/useSwoffPush.ts +81 -0
  107. package/templates/vue/useSwoffPwa.js +27 -0
  108. package/templates/vue/useSwoffPwa.ts +27 -0
  109. package/templates/vue/useSwoffQueue.js +70 -0
  110. package/templates/vue/useSwoffQueue.ts +82 -0
  111. package/templates/vue/useSwoffReset.js +32 -0
  112. package/templates/vue/useSwoffReset.ts +39 -0
  113. package/templates/vue/useSwoffStorage.js +56 -0
  114. package/templates/vue/useSwoffStorage.ts +57 -0
  115. package/templates/vue/useSwoffSync.js +30 -0
  116. package/templates/vue/useSwoffSync.ts +30 -0
@@ -0,0 +1,36 @@
1
+ import { ref, onMounted, onUnmounted } from "vue";
2
+ import { getMutationState, onMutationStateChange } from "../mutation/state";
3
+ import type { MutationState } from "../mutation/state";
4
+
5
+ /**
6
+ * Composable that subscribes to a specific mutation's state changes.
7
+ * Useful for showing per-mutation loading spinners, error states, etc.
8
+ *
9
+ * Usage:
10
+ * const mutation = useSwoffMutationState(mutationId);
11
+ * <div v-if="mutation?.status === 'error'">{{ mutation?.error?.message }}</div>
12
+ */
13
+ export function useSwoffMutationState(id: string | null) {
14
+ const state = ref<MutationState | null>(null);
15
+
16
+ function update() {
17
+ state.value = id ? (getMutationState(id) ?? null) : null;
18
+ }
19
+
20
+ onMounted(update);
21
+
22
+ let cleanup: (() => void) | undefined;
23
+
24
+ onMounted(() => {
25
+ const unsub = onMutationStateChange(update);
26
+ if (typeof unsub === "function") {
27
+ cleanup = unsub;
28
+ }
29
+
30
+ onUnmounted(() => {
31
+ cleanup?.();
32
+ });
33
+ });
34
+
35
+ return state;
36
+ }
@@ -0,0 +1,86 @@
1
+ import { ref, onMounted, onUnmounted } from "vue";
2
+ import {
3
+ CONNECTIVITY_EVENT,
4
+ forceRetry,
5
+ getCurrentOnlineStatus,
6
+ } from "../connectivity.js";
7
+
8
+ /**
9
+ * Reactive network information: online status, connection type, and bandwidth.
10
+ *
11
+ * Usage:
12
+ * const { online, wasOffline, lastChangedAt, effectiveType, downlink } = useSwoffNetwork();
13
+ *
14
+ * // Show offline indicator:
15
+ * <div v-if="!online">You are offline</div>
16
+ *
17
+ * // Warn on slow connection:
18
+ * <div v-if="online && effectiveType === '2g'">Slow connection detected</div>
19
+ */
20
+ export function useSwoffNetwork() {
21
+ const online = ref(true);
22
+ const wasOffline = ref(false);
23
+ const lastChangedAt = ref(null);
24
+ const effectiveType = ref(null);
25
+ const downlink = ref(null);
26
+ const isRetrying = ref(false);
27
+
28
+ let wasOfflineFlag = false;
29
+
30
+ onMounted(() => {
31
+ if (!getCurrentOnlineStatus()) {
32
+ wasOfflineFlag = true;
33
+ online.value = false;
34
+ wasOffline.value = true;
35
+ lastChangedAt.value = Date.now();
36
+ }
37
+
38
+ const connection = navigator.connection;
39
+
40
+ function handleConnectivityChange(e) {
41
+ const isTrulyOnline = e.detail.online;
42
+ if (!isTrulyOnline) {
43
+ wasOfflineFlag = true;
44
+ } else {
45
+ wasOfflineFlag = false;
46
+ }
47
+ online.value = isTrulyOnline;
48
+ wasOffline.value = wasOfflineFlag;
49
+ lastChangedAt.value = Date.now();
50
+ }
51
+
52
+ function onTypeChange() {
53
+ if (!connection) return;
54
+ effectiveType.value = connection.effectiveType;
55
+ downlink.value = connection.downlink;
56
+ }
57
+
58
+ window.addEventListener(CONNECTIVITY_EVENT, handleConnectivityChange);
59
+ if (connection) {
60
+ connection.addEventListener("change", onTypeChange);
61
+ }
62
+
63
+ onUnmounted(() => {
64
+ window.removeEventListener(CONNECTIVITY_EVENT, handleConnectivityChange);
65
+ if (connection) {
66
+ connection.removeEventListener("change", onTypeChange);
67
+ }
68
+ });
69
+ });
70
+
71
+ async function retry() {
72
+ isRetrying.value = true;
73
+ await forceRetry();
74
+ isRetrying.value = false;
75
+ }
76
+
77
+ return {
78
+ online,
79
+ wasOffline,
80
+ lastChangedAt,
81
+ effectiveType,
82
+ downlink,
83
+ isRetrying,
84
+ retry,
85
+ };
86
+ }
@@ -0,0 +1,89 @@
1
+ import { ref, onMounted, onUnmounted } from "vue";
2
+ import {
3
+ CONNECTIVITY_EVENT,
4
+ forceRetry,
5
+ getCurrentOnlineStatus,
6
+ } from "../connectivity";
7
+
8
+ /**
9
+ * Reactive network information: online status, connection type, and bandwidth.
10
+ *
11
+ * Usage:
12
+ * const { online, wasOffline, lastChangedAt, effectiveType, downlink } = useSwoffNetwork();
13
+ *
14
+ * // Show offline indicator:
15
+ * <div v-if="!online">You are offline</div>
16
+ *
17
+ * // Warn on slow connection:
18
+ * <div v-if="online && effectiveType === '2g'">Slow connection detected</div>
19
+ *
20
+ * @returns {{ online: Ref<boolean>, wasOffline: Ref<boolean>, lastChangedAt: Ref<number | null>, effectiveType: Ref<string | null>, downlink: Ref<number | null>, isRetrying: Ref<boolean>, retry: () => Promise<void> }}
21
+ */
22
+ export function useSwoffNetwork() {
23
+ const online = ref(true);
24
+ const wasOffline = ref(false);
25
+ const lastChangedAt = ref<number | null>(null);
26
+ const effectiveType = ref<string | null>(null);
27
+ const downlink = ref<number | null>(null);
28
+ const isRetrying = ref(false);
29
+
30
+ let wasOfflineFlag = false;
31
+
32
+ onMounted(() => {
33
+ // Sync with actual status after mount — prevents SSR hydration mismatch
34
+ if (!getCurrentOnlineStatus()) {
35
+ wasOfflineFlag = true;
36
+ online.value = false;
37
+ wasOffline.value = true;
38
+ lastChangedAt.value = Date.now();
39
+ }
40
+
41
+ const connection = navigator.connection;
42
+
43
+ function handleConnectivityChange(e: Event) {
44
+ const isTrulyOnline = (e as CustomEvent).detail.online;
45
+ if (!isTrulyOnline) {
46
+ wasOfflineFlag = true;
47
+ } else {
48
+ wasOfflineFlag = false;
49
+ }
50
+ online.value = isTrulyOnline;
51
+ wasOffline.value = wasOfflineFlag;
52
+ lastChangedAt.value = Date.now();
53
+ }
54
+
55
+ function onTypeChange() {
56
+ if (!connection) return;
57
+ effectiveType.value = connection.effectiveType;
58
+ downlink.value = connection.downlink;
59
+ }
60
+
61
+ window.addEventListener(CONNECTIVITY_EVENT, handleConnectivityChange);
62
+ if (connection) {
63
+ connection.addEventListener("change", onTypeChange);
64
+ }
65
+
66
+ onUnmounted(() => {
67
+ window.removeEventListener(CONNECTIVITY_EVENT, handleConnectivityChange);
68
+ if (connection) {
69
+ connection.removeEventListener("change", onTypeChange);
70
+ }
71
+ });
72
+ });
73
+
74
+ async function retry() {
75
+ isRetrying.value = true;
76
+ await forceRetry();
77
+ isRetrying.value = false;
78
+ }
79
+
80
+ return {
81
+ online,
82
+ wasOffline,
83
+ lastChangedAt,
84
+ effectiveType,
85
+ downlink,
86
+ isRetrying,
87
+ retry,
88
+ };
89
+ }
@@ -0,0 +1,19 @@
1
+ import { ref, onMounted, onUnmounted } from "vue";
2
+
3
+ export function useSwoffPrecache() {
4
+ const progress = ref(0);
5
+
6
+ function onProgress(e) {
7
+ progress.value = e.detail.percent;
8
+ }
9
+
10
+ onMounted(() => {
11
+ window.addEventListener("sw-progress", onProgress);
12
+ });
13
+
14
+ onUnmounted(() => {
15
+ window.removeEventListener("sw-progress", onProgress);
16
+ });
17
+
18
+ return { progress };
19
+ }
@@ -0,0 +1,19 @@
1
+ import { ref, onMounted, onUnmounted } from "vue";
2
+
3
+ export function useSwoffPrecache() {
4
+ const progress = ref(0);
5
+
6
+ function onProgress(e: WindowEventMap["sw-progress"]) {
7
+ progress.value = e.detail.percent;
8
+ }
9
+
10
+ onMounted(() => {
11
+ window.addEventListener("sw-progress", onProgress);
12
+ });
13
+
14
+ onUnmounted(() => {
15
+ window.removeEventListener("sw-progress", onProgress);
16
+ });
17
+
18
+ return { progress };
19
+ }
@@ -0,0 +1,33 @@
1
+ import { ref } from "vue";
2
+ import { prefetchCache } from "../fetch/core.js";
3
+ /**
4
+ * Composable that returns a stable prefetch callback, observable prefetch list, and clear function.
5
+ *
6
+ * Usage:
7
+ * const { prefetch, prefetchList, clear } = useSwoffPrefetch();
8
+ *
9
+ * // Prefetch when user hovers a link:
10
+ * <button @mouseenter="prefetch('/api/todos')">Load</button>
11
+ *
12
+ * // Show how many URLs are prefetched:
13
+ * <span>{{ prefetchList.length }} routes cached</span>
14
+ *
15
+ * // Clear prefetched data:
16
+ * <button @click="clear">Clear prefetch</button>
17
+ */
18
+ export function useSwoffPrefetch() {
19
+ const prefetchList = ref([]);
20
+
21
+ function prefetch(url, options) {
22
+ prefetchCache(url, options);
23
+ if (!prefetchList.value.includes(url)) {
24
+ prefetchList.value = [...prefetchList.value, url];
25
+ }
26
+ }
27
+
28
+ function clear() {
29
+ prefetchList.value = [];
30
+ }
31
+
32
+ return { prefetch, prefetchList: prefetchList.value, clear };
33
+ }
@@ -0,0 +1,41 @@
1
+ import { ref } from "vue";
2
+ import { prefetchCache } from "../fetch/core";
3
+ import type { FetchWithCacheOptions } from "../fetch/core";
4
+
5
+ /**
6
+ * Composable that returns a stable prefetch callback, observable prefetch list, and clear function.
7
+ *
8
+ * Usage:
9
+ * const { prefetch, prefetchList, clear } = useSwoffPrefetch();
10
+ *
11
+ * // Prefetch when user hovers a link:
12
+ * <button @mouseenter="prefetch('/api/todos')">Load</button>
13
+ *
14
+ * // Show how many URLs are prefetched:
15
+ * <span>{{ prefetchList.length }} routes cached</span>
16
+ *
17
+ * // Clear prefetched data:
18
+ * <button @click="clear">Clear prefetch</button>
19
+ *
20
+ * @returns {{ prefetch: (url: string, options?: FetchWithCacheOptions) => void, prefetchList: string[], clear: () => void }}
21
+ */
22
+ export function useSwoffPrefetch(): {
23
+ prefetch: (url: string, options?: FetchWithCacheOptions) => void;
24
+ prefetchList: string[];
25
+ clear: () => void;
26
+ } {
27
+ const prefetchList = ref<string[]>([]);
28
+
29
+ function prefetch(url: string, options?: FetchWithCacheOptions) {
30
+ prefetchCache(url, options);
31
+ if (!prefetchList.value.includes(url)) {
32
+ prefetchList.value = [...prefetchList.value, url];
33
+ }
34
+ }
35
+
36
+ function clear() {
37
+ prefetchList.value = [];
38
+ }
39
+
40
+ return { prefetch, prefetchList: prefetchList.value, clear };
41
+ }
@@ -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
+ }