Log even more garbage
Test / test (push) Failing after 54s
Details
Test / test (push) Failing after 54s
Details
This commit is contained in:
parent
3ff16c58b2
commit
121cc90de9
|
|
@ -100,51 +100,63 @@ export default function start(self: ServiceWorkerGlobalScope) {
|
|||
})()
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
async function submitSubscription(
|
||||
registration: ServiceWorkerRegistration,
|
||||
subscription: PushSubscription | null
|
||||
) {
|
||||
const db = database()
|
||||
async function submitSubscription(
|
||||
registration: ServiceWorkerRegistration,
|
||||
subscription: PushSubscription | null
|
||||
) {
|
||||
const db = database()
|
||||
|
||||
if (subscription === null) return
|
||||
await sendMessage({
|
||||
message: `Is subscription null? ${subscription === null}`,
|
||||
})
|
||||
|
||||
const existingSubscriptionId = await db.subscriptions.get(1)
|
||||
if (subscription === null) return
|
||||
|
||||
const applicationServerKey = urlB64ToUint8Array(pushPublicKey)
|
||||
const newSubscription = await registration.pushManager.subscribe({
|
||||
userVisibleOnly: true,
|
||||
applicationServerKey: applicationServerKey,
|
||||
})
|
||||
const existingSubscriptionId = await db.subscriptions.get(1)
|
||||
|
||||
await (existingSubscriptionId === undefined
|
||||
? postSubscription(newSubscription, db)
|
||||
: putSubscription(newSubscription, existingSubscriptionId.subscriptionId))
|
||||
await sendMessage({
|
||||
message: `Existing subscription ID ${existingSubscriptionId}`,
|
||||
})
|
||||
|
||||
return newSubscription
|
||||
}
|
||||
const applicationServerKey = urlB64ToUint8Array(pushPublicKey)
|
||||
const newSubscription = await registration.pushManager.subscribe({
|
||||
userVisibleOnly: true,
|
||||
applicationServerKey: applicationServerKey,
|
||||
})
|
||||
|
||||
async function postSubscription(
|
||||
subscription: PushSubscription,
|
||||
db: ReturnType<typeof database>
|
||||
) {
|
||||
const response = await fetch(`${process.env.BASE_URL}/api/subscription/`, {
|
||||
method: "POST",
|
||||
body: JSON.stringify(subscription),
|
||||
})
|
||||
await (existingSubscriptionId === undefined
|
||||
? postSubscription(newSubscription, db)
|
||||
: putSubscription(newSubscription, existingSubscriptionId.subscriptionId))
|
||||
|
||||
db.subscriptions.put({
|
||||
id: 1,
|
||||
subscriptionId: (await response.json()).subscriptionId,
|
||||
})
|
||||
}
|
||||
return newSubscription
|
||||
}
|
||||
|
||||
function putSubscription(subscription: PushSubscription, id: number) {
|
||||
return fetch(`${process.env.BASE_URL}/api/subscription/${id}`, {
|
||||
method: "PUT",
|
||||
body: JSON.stringify(subscription),
|
||||
})
|
||||
async function postSubscription(
|
||||
subscription: PushSubscription,
|
||||
db: ReturnType<typeof database>
|
||||
) {
|
||||
const response = await fetch(`${process.env.BASE_URL}/api/subscription/`, {
|
||||
method: "POST",
|
||||
body: JSON.stringify(subscription),
|
||||
})
|
||||
|
||||
await sendMessage({
|
||||
message: `Response status ${response.status}`,
|
||||
})
|
||||
|
||||
db.subscriptions.put({
|
||||
id: 1,
|
||||
subscriptionId: (await response.json()).subscriptionId,
|
||||
})
|
||||
}
|
||||
|
||||
function putSubscription(subscription: PushSubscription, id: number) {
|
||||
return fetch(`${process.env.BASE_URL}/api/subscription/${id}`, {
|
||||
method: "PUT",
|
||||
body: JSON.stringify(subscription),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
function database() {
|
||||
|
|
|
|||
Loading…
Reference in New Issue