Skip to content

Commit

Permalink
Astro - Make push more readable
Browse files Browse the repository at this point in the history
  • Loading branch information
smastrom committed Apr 14, 2024
1 parent 7ed383f commit 9f65f6e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
2 changes: 1 addition & 1 deletion packages/notivue/astro/Notivue.vue
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ function onPush(e: CustomEvent<PushAstroEvent>) {
// Create the notification as usual
const notification = push[e.detail.type](e.detail)
// Dispatch the push result
// Dispatch the result
window.dispatchEvent(
new CustomEvent(e.detail.resultEventName, {
detail: notification,
Expand Down
19 changes: 10 additions & 9 deletions packages/notivue/astro/push.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@ export function pushEvent<T extends Omit<PushAstroEvent, 'resultEventName'>>(
): MaybeAstroPushPromiseReturn<T> {
eventId++

// Listen for the result of the notification that will be created by NotivueAstro...
// Prepare to listen for the result of the notification that will be created by NotivueAstro...
let pushResult = {} as MaybeAstroPushPromiseReturn<T>
const resultEventName = `notivue:id:${eventId}`

// ...upon receival, save the result and remove the listener
window.addEventListener(resultEventName, saveResult as EventListener, {
once: true,
})

function saveResult(e: CustomEvent<MaybeAstroPushPromiseReturn<T>>) {
pushResult = e.detail
}
window.addEventListener(
resultEventName,
((e: CustomEvent<MaybeAstroPushPromiseReturn<T>>) => {
pushResult = e.detail
}) as EventListener,
{ once: true }
)

// Dispatch the incoming push options to the receiver to create the notification
// Dispatch the incoming push options to NotivueAstro to create the notification
window.dispatchEvent(
new CustomEvent('notivue:push', {
detail: {
Expand All @@ -32,6 +32,7 @@ export function pushEvent<T extends Omit<PushAstroEvent, 'resultEventName'>>(
})
)

// Return the result
return pushResult
}

Expand Down

0 comments on commit 9f65f6e

Please sign in to comment.