aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorFuwn <[email protected]>2025-01-11 16:34:56 -0800
committerFuwn <[email protected]>2025-01-11 16:34:56 -0800
commitaf0824b32dfd7e499450ba4f41735da955a2b488 (patch)
tree329c6cc6363c7cf575a0e43f19b074b661c9a829 /src
parentdocs(service-worker): clean up comments (diff)
downloaddue.moe-af0824b32dfd7e499450ba4f41735da955a2b488.tar.xz
due.moe-af0824b32dfd7e499450ba4f41735da955a2b488.zip
refactor(service-worker): clean up code
Diffstat (limited to 'src')
-rw-r--r--src/service-worker.ts52
1 files changed, 21 insertions, 31 deletions
diff --git a/src/service-worker.ts b/src/service-worker.ts
index 2aa76a3f..2f70ced2 100644
--- a/src/service-worker.ts
+++ b/src/service-worker.ts
@@ -12,22 +12,22 @@ const CACHE = `cache-${version}`;
const ASSETS = [...build, ...files];
sw.addEventListener('install', (event: ExtendableEvent) => {
- async function addFilesToCache() {
- const cache = await caches.open(CACHE);
- await cache.addAll(ASSETS);
- }
-
- event.waitUntil(addFilesToCache());
+ event.waitUntil(
+ (async () => {
+ const cache = await caches.open(CACHE);
+ await cache.addAll(ASSETS);
+ })()
+ );
});
sw.addEventListener('activate', (event) => {
- async function deleteOldCaches() {
- for (const key of await caches.keys()) {
- if (key !== CACHE) await caches.delete(key);
- }
- }
-
- event.waitUntil(deleteOldCaches());
+ event.waitUntil(
+ (async () => {
+ for (const key of await caches.keys()) {
+ if (key !== CACHE) await caches.delete(key);
+ }
+ })()
+ );
});
sw.addEventListener('fetch', (event) => {
@@ -45,31 +45,23 @@ sw.addEventListener('fetch', (event) => {
if (ASSETS.includes(url.pathname)) {
const response = await cache.match(url.pathname);
- if (response) {
- return response;
- }
+ if (response) return response;
}
try {
const response = await fetch(event.request);
- if (!(response instanceof Response)) {
- throw new Error('invalid response from fetch');
- }
+ if (!(response instanceof Response)) throw new Error('invalid response from fetch');
- if (response.status === 200) {
- cache.put(event.request, response.clone());
- }
+ if (response.status === 200) cache.put(event.request, response.clone());
return response;
- } catch (err) {
+ } catch (error) {
const response = await cache.match(event.request);
- if (response) {
- return response;
- }
+ if (response) return response;
- throw err;
+ throw error;
}
}
@@ -79,9 +71,7 @@ sw.addEventListener('fetch', (event) => {
sw.addEventListener('push', async (event: PushEvent) => {
event.waitUntil(
(async () => {
- if (self.Notification && self.Notification.permission !== 'granted') {
- return;
- }
+ if (self.Notification && self.Notification.permission !== 'granted') return;
try {
const user = (await database.users.toArray()).at(0);
@@ -95,7 +85,7 @@ sw.addEventListener('push', async (event: PushEvent) => {
recentNotifications.length > 0 &&
(recentNotifications[0].id > (user.lastNotificationID as number) ||
new Date(recentNotifications[0].createdAt * 1000).getTime() + 30000 >
- new Date().getTime())
+ new Date().getTime())
) {
await database.users.update(user.id, { lastNotificationID: recentNotifications[0].id });
await sw.registration.showNotification('due.moe', {