Skip to content

Commit

Permalink
WIP: feat: add periodicSync service
Browse files Browse the repository at this point in the history
  • Loading branch information
VChet committed Oct 13, 2024
1 parent 22b2d7c commit 127add8
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
22 changes: 22 additions & 0 deletions env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,25 @@ interface ImportMetaEnv {
interface ImportMeta {
readonly env: ImportMetaEnv
}

interface SyncManager {
getTags(): Promise<string[]>;

Check failure on line 13 in env.d.ts

View workflow job for this annotation

GitHub Actions / build

Shorthand method signature is forbidden. Use a function property instead

Check failure on line 13 in env.d.ts

View workflow job for this annotation

GitHub Actions / build

Unexpected separator (;)
register(tag: string, options?: { minInterval: number }): Promise<undefined>;

Check failure on line 14 in env.d.ts

View workflow job for this annotation

GitHub Actions / build

Shorthand method signature is forbidden. Use a function property instead

Check failure on line 14 in env.d.ts

View workflow job for this annotation

GitHub Actions / build

Unexpected separator (;)
unregister(tag: string): Promise<void>;

Check failure on line 15 in env.d.ts

View workflow job for this annotation

GitHub Actions / build

Shorthand method signature is forbidden. Use a function property instead

Check failure on line 15 in env.d.ts

View workflow job for this annotation

GitHub Actions / build

Unexpected separator (;)
getTags(): Promise<Array<string>>;

Check failure on line 16 in env.d.ts

View workflow job for this annotation

GitHub Actions / build

Shorthand method signature is forbidden. Use a function property instead

Check failure on line 16 in env.d.ts

View workflow job for this annotation

GitHub Actions / build

Array type using 'Array<string>' is forbidden. Use 'string[]' instead

Check failure on line 16 in env.d.ts

View workflow job for this annotation

GitHub Actions / build

Unexpected separator (;)
}

interface ServiceWorkerRegistration {
readonly sync: SyncManager;

Check failure on line 20 in env.d.ts

View workflow job for this annotation

GitHub Actions / build

Unexpected separator (;)
readonly periodicSync: SyncManager;
}

interface SyncEvent extends ExtendableEvent {
readonly lastChance: boolean;
readonly tag: string;
}

interface ServiceWorkerGlobalScopeEventMap {
sync: SyncEvent;
periodicSync: SyncEvent;
}
28 changes: 28 additions & 0 deletions src/services/periodicSync.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { ref } from "vue";

const MIN_INTERVAL = 24 * 60 * 60 * 1000;
const registration = ref<ServiceWorkerRegistration | null>(null);

async function isActive(tag: string) {
if (!registration.value) registration.value = await navigator.serviceWorker.ready;
const tags: string[] = await registration.value.periodicSync.getTags();
return tags.includes(tag);
}

async function register(tag: string, minInterval = MIN_INTERVAL) {
if (!registration.value) registration.value = await navigator.serviceWorker.ready;
return registration.value.periodicSync.register(tag, { minInterval });
}

async function unregister(tag: string) {
if (!registration.value) registration.value = await navigator.serviceWorker.ready;
return registration.value.periodicSync.unregister(tag);
}

export function usePeriodicSync(tag: string) {
return {
isActive: () => isActive(tag),
register: () => register(tag),
unregister: () => unregister(tag)
};
}
6 changes: 6 additions & 0 deletions src/sw.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import dayjs from "dayjs";
import { cleanupOutdatedCaches, createHandlerBoundToURL, precacheAndRoute } from "workbox-precaching";
import { NavigationRoute, registerRoute } from "workbox-routing";
import { CacheFirst } from "workbox-strategies";
Expand All @@ -8,6 +9,11 @@ self.addEventListener("message", (event) => {
if (event.data && event.data.type === "SKIP_WAITING") self.skipWaiting();
});

self.addEventListener("periodicsync", (event) => {
const promiseChain = self.registration.showNotification(`${dayjs().format("dddd HH:mm:ss")} Test`);
(event as ExtendableEvent).waitUntil(promiseChain);
});

// self.__WB_MANIFEST is default injection point
precacheAndRoute(self.__WB_MANIFEST);

Expand Down

0 comments on commit 127add8

Please sign in to comment.