Changeset View
Changeset View
Standalone View
Standalone View
web/push-notif/service-worker.js
// @flow | // @flow | ||||
declare class PushMessageData { | |||||
json(): Object; | |||||
tomek: If this is specific to push notifications, maybe we can have a more concrete type (with body… | |||||
} | |||||
declare class PushEvent extends ExtendableEvent { | |||||
+data: PushMessageData; | |||||
} | |||||
michalAuthorUnsubmitted Done Inline ActionsUnfortunately, these types don't exist in flow so I had to type them myself based on MDN. michal: Unfortunately, these types don't exist in flow so I had to type them myself based on [MDN]… | |||||
declare var clients: Clients; | |||||
declare function skipWaiting(): Promise<void>; | |||||
michalAuthorUnsubmitted Done Inline ActionsWhile these exist in flow, eslint complains that they aren't defined. Instead of this, we could add them in .eslintrc in globals but then they would be available in the whole web which isn't great. michal: While these exist in flow, eslint complains that they aren't defined. Instead of this, we could… | |||||
self.addEventListener('install', () => { | self.addEventListener('install', () => { | ||||
console.log('service worker installed'); | skipWaiting(); | ||||
}); | |||||
self.addEventListener('activate', (event: ExtendableEvent) => { | |||||
event.waitUntil(clients.claim()); | |||||
}); | |||||
self.addEventListener('push', (event: PushEvent) => { | |||||
const data = event.data.json(); | |||||
tomekUnsubmitted Not Done Inline ActionsCan we cast data so that all the fields have types assigned? tomek: Can we cast `data` so that all the fields have types assigned? | |||||
event.waitUntil( | |||||
(async () => { | |||||
let body = data.body; | |||||
if (data.prefix) { | |||||
body = `${data.prefix} ${body}`; | |||||
} | |||||
await self.registration.showNotification(data.title, { | |||||
body, | |||||
badge: 'https://web.comm.app/favicon.ico', | |||||
icon: 'https://web.comm.app/favicon.ico', | |||||
tag: data.id, | |||||
data: { | |||||
unreadCount: data.unreadCount, | |||||
threadID: data.threadID, | |||||
}, | |||||
}); | |||||
})(), | |||||
); | |||||
}); | }); | ||||
self.addEventListener('activate', () => { | self.addEventListener('notificationclick', (event: NotificationEvent) => { | ||||
console.log('service worker activated'); | event.notification.close(); | ||||
}); | }); |
If this is specific to push notifications, maybe we can have a more concrete type (with body, prefix, etc.)? Otherwise we should keep Object here and cast a result of calling json() immediately in push event handler.