diff options
| author | Fuwn <[email protected]> | 2024-07-25 04:14:50 -0700 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2024-07-25 04:14:50 -0700 |
| commit | 00a8c8f4846c349abcb5905e78790749826ae8ba (patch) | |
| tree | 7d4005b6b36f4f9608e878f2d56304e1b4d0deb4 /src | |
| parent | fix(service-worker): show default notification for chrome (diff) | |
| download | due.moe-00a8c8f4846c349abcb5905e78790749826ae8ba.tar.xz due.moe-00a8c8f4846c349abcb5905e78790749826ae8ba.zip | |
fix(service-worker): wrap push in waitUntil
Diffstat (limited to 'src')
| -rw-r--r-- | src/service-worker.ts | 105 |
1 files changed, 56 insertions, 49 deletions
diff --git a/src/service-worker.ts b/src/service-worker.ts index d7cc2cdd..4799a492 100644 --- a/src/service-worker.ts +++ b/src/service-worker.ts @@ -93,57 +93,64 @@ sw.addEventListener('fetch', (event) => { }); sw.addEventListener('push', async (event: PushEvent) => { - if (self.Notification && self.Notification.permission !== 'granted') { - return; - } - - try { - const user = (await database.users.toArray()).at(0); - - if (!user) return; - - const recentNotifications = await notifications(user.user.accessToken); - - if ( - recentNotifications && - recentNotifications.length > 0 && - (recentNotifications[0].id > (user.lastNotificationID as number) || - new Date(recentNotifications[0].createdAt * 1000).getTime() + 30000 > new Date().getTime()) - ) { - await database.users.update(user.id, { lastNotificationID: recentNotifications[0].id }); - - sw.registration.showNotification('due.moe', { - body: `${recentNotifications[0].user.name}${recentNotifications[0].context}`, - icon: recentNotifications[0].user.avatar.large || '/favicon-196x196.png', - // Ref. https://stackoverflow.com/a/50805868/14452787 - tag: 'notification-1' - }); + event.waitUntil( + (async () => { + if (self.Notification && self.Notification.permission !== 'granted') { + return; + } - return; - } - } catch (error) { - console.error(error); - } + try { + const user = (await database.users.toArray()).at(0); + + if (!user) return; + + const recentNotifications = await notifications(user.user.accessToken); + + if ( + recentNotifications && + recentNotifications.length > 0 && + (recentNotifications[0].id > (user.lastNotificationID as number) || + new Date(recentNotifications[0].createdAt * 1000).getTime() + 30000 > + new Date().getTime()) + ) { + await database.users.update(user.id, { lastNotificationID: recentNotifications[0].id }); + + sw.registration.showNotification('due.moe', { + body: `${recentNotifications[0].user.name}${recentNotifications[0].context}`, + icon: recentNotifications[0].user.avatar.large || '/favicon-196x196.png', + // Ref. https://stackoverflow.com/a/50805868/14452787 + tag: 'notification-1' + }); + + return; + } + } catch (error) { + console.error(error); + } - if (navigator.userAgent.includes('Chrome')) { - sw.registration.showNotification('due.moe', { - body: 'No new notifications', - icon: '/favicon-196x196.png', - tag: 'notification-1' - }); - - // Ref. https://github.com/firebase/quickstart-js/issues/126#issuecomment-504081087 - new Promise((resolve) => { - (resolve as unknown as () => void)(); - setTimeout( - () => - sw.registration - .getNotifications() - .then((notifications) => notifications.forEach((notification) => notification.close())), - 10 - ); - }); - } + if (navigator.userAgent.includes('Chrome')) { + sw.registration.showNotification('due.moe', { + body: 'No new notifications', + icon: '/favicon-196x196.png', + tag: 'notification-1' + }); + + // Ref. https://github.com/firebase/quickstart-js/issues/126#issuecomment-504081087 + new Promise((resolve) => { + (resolve as unknown as () => void)(); + setTimeout( + () => + sw.registration + .getNotifications() + .then((notifications) => + notifications.forEach((notification) => notification.close()) + ), + 10 + ); + }); + } + })() + ); }); sw.addEventListener('notificationclick', (event: NotificationEvent) => { |