Skip to content

Commit 7015a57

Browse files
committed
better comments
1 parent a37992e commit 7015a57

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

sw/eventListener.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { CLEAR_NOTIFICATIONS, clearAppBadge, setAppBadge } from '@/lib/badge'
44
import { ACTION_PORT, DELETE_SUBSCRIPTION, MESSAGE_PORT, STORE_OS, STORE_SUBSCRIPTION, SYNC_SUBSCRIPTION } from '@/components/serviceworker'
55
// import { getLogger } from '@/lib/logger'
66

7-
// we store existing push subscriptions to keep them in sync with server
7+
// we store existing push subscriptions and OS to keep them in sync with server
88
const storage = new ServiceWorkerStorage('sw:storage', 1)
99

1010
// for communication between app and service worker
@@ -24,17 +24,19 @@ async function getOS () {
2424
// current push notification count for badge purposes
2525
let activeCount = 0
2626

27+
// message event listener for communication between app and service worker
2728
const log = (message, level = 'info', context) => {
2829
messageChannelPort?.postMessage({ level, message, context })
2930
}
3031

3132
export function onPush (sw) {
3233
return (event) => {
34+
// in case of push notifications, make sure that the logger has an HTTPS endpoint
3335
// const logger = getLogger('sw:push', ['onPush'])
3436
let payload = event.data?.json()
35-
if (!payload) return
37+
if (!payload) return // ignore push events without payload, like isTrusted events
3638
const { tag } = payload.options
37-
const nid = crypto.randomUUID()
39+
const nid = crypto.randomUUID() // notification id for tracking
3840

3941
// iOS requirement: group all promises
4042
const promises = []
@@ -43,8 +45,9 @@ export function onPush (sw) {
4345
if (immediatelyShowNotification(tag)) {
4446
// logger.info(`[${nid}] showing immediate notification with title: ${payload.title}`)
4547
promises.push(setAppBadge(sw, ++activeCount))
46-
} else { // Check if there are already notifications with the same tag and merge them
48+
} else {
4749
// logger.info(`[${nid}] checking for existing notification with tag ${tag}`)
50+
// Check if there are already notifications with the same tag and merge them
4851
promises.push(sw.registration.getNotifications({ tag }).then((notifications) => {
4952
// logger.info(`[${nid}] found ${notifications.length} notifications with tag ${tag}`)
5053
if (notifications.length) {
@@ -54,20 +57,22 @@ export function onPush (sw) {
5457
}))
5558
}
5659

57-
// Apple requirement: wait for all promises to resolve
60+
// iOS requirement: wait for all promises to resolve before showing the notification
5861
event.waitUntil(Promise.all(promises).then(() => {
5962
sw.registration.showNotification(payload.title, payload.options)
6063
}))
6164
}
6265
}
6366

64-
// if there is no tag or it's a TIP, FORWARDEDTIP or EARN notification
65-
// we don't need to merge notifications and thus the notification should be immediately shown using `showNotification`
67+
// if there is no tag or the tag is one of the following
68+
// we show the notification immediately
6669
const immediatelyShowNotification = (tag) =>
6770
!tag || ['TIP', 'FORWARDEDTIP', 'EARN', 'STREAK', 'TERRITORY_TRANSFER'].includes(tag.split('-')[0])
6871

72+
// merge notifications with the same tag
6973
const mergeNotification = (event, sw, payload, currentNotifications, tag, nid) => {
7074
// const logger = getLogger('sw:push:mergeNotification', ['mergeNotification'])
75+
7176
// sanity check
7277
const otherTagNotifications = currentNotifications.filter(({ tag: nTag }) => nTag !== tag)
7378
if (otherTagNotifications.length > 0) {

0 commit comments

Comments
 (0)