From 0a924ab042f89090dcb431d34eed34964229637c Mon Sep 17 00:00:00 2001 From: Fuwn Date: Thu, 25 Jul 2024 02:47:04 -0700 Subject: chore(service-worker): fix types --- src/service-worker.ts | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) (limited to 'src') diff --git a/src/service-worker.ts b/src/service-worker.ts index 56927647..d7629073 100644 --- a/src/service-worker.ts +++ b/src/service-worker.ts @@ -7,7 +7,7 @@ import { build, files, version } from '$service-worker'; import { database } from './lib/Database/IndexedDB/user'; import { notifications } from './lib/Data/AniList/notifications'; -const sw = /** @type {ServiceWorkerGlobalScope} */ /** @type {unknown} */ self; +const sw = self as unknown as ServiceWorkerGlobalScope; // Create a unique cache name for this deployment const CACHE = `cache-${version}`; @@ -17,7 +17,7 @@ const ASSETS = [ ...files // everything in `static` ]; -self.addEventListener('install', (event) => { +sw.addEventListener('install', (event: ExtendableEvent) => { // Create a new cache and add all files to it async function addFilesToCache() { const cache = await caches.open(CACHE); @@ -27,7 +27,7 @@ self.addEventListener('install', (event) => { event.waitUntil(addFilesToCache()); }); -self.addEventListener('activate', (event) => { +sw.addEventListener('activate', (event) => { // Remove previous cached data from disk async function deleteOldCaches() { for (const key of await caches.keys()) { @@ -38,7 +38,7 @@ self.addEventListener('activate', (event) => { event.waitUntil(deleteOldCaches()); }); -self.addEventListener('fetch', (event) => { +sw.addEventListener('fetch', (event) => { // ignore POST requests etc if (event.request.method !== 'GET') return; @@ -92,13 +92,11 @@ self.addEventListener('fetch', (event) => { event.respondWith(respond()); }); -self.addEventListener('push', async (e: Event) => { +sw.addEventListener('push', async (event: PushEvent) => { if (self.Notification && self.Notification.permission !== 'granted') { return; } - const event = e as PushEvent; - try { const user = (await database.users.toArray()).at(0); @@ -114,7 +112,7 @@ self.addEventListener('push', async (e: Event) => { ) { await database.users.update(user.id, { lastNotificationID: recentNotifications[0].id }); - (self as unknown as ServiceWorkerGlobalScope).registration.showNotification('due.moe', { + sw.registration.showNotification('due.moe', { body: `${recentNotifications[0].user.name}${recentNotifications[0].context}`, icon: recentNotifications[0].user.avatar.large || '/favicon-196x196.png' }); @@ -132,9 +130,7 @@ self.addEventListener('push', async (e: Event) => { ); }); -self.addEventListener('notificationclick', (e: Event) => { - const event = e as NotificationEvent; - +sw.addEventListener('notificationclick', (event: NotificationEvent) => { event.notification.close(); - event.waitUntil(clients.openWindow('https://anilist.co/notifications')); + event.waitUntil(sw.clients.openWindow('https://anilist.co/notifications')); }); -- cgit v1.2.3