From 9f65f6e386e975c8baf3c7373e222b947e1ecfee Mon Sep 17 00:00:00 2001 From: smastrom Date: Sun, 14 Apr 2024 12:05:35 +0200 Subject: [PATCH] Astro - Make push more readable --- packages/notivue/astro/Notivue.vue | 2 +- packages/notivue/astro/push.ts | 19 ++++++++++--------- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/packages/notivue/astro/Notivue.vue b/packages/notivue/astro/Notivue.vue index 0c150a3..d65458d 100644 --- a/packages/notivue/astro/Notivue.vue +++ b/packages/notivue/astro/Notivue.vue @@ -17,7 +17,7 @@ function onPush(e: CustomEvent) { // 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, diff --git a/packages/notivue/astro/push.ts b/packages/notivue/astro/push.ts index fcdfdd2..92df2cc 100644 --- a/packages/notivue/astro/push.ts +++ b/packages/notivue/astro/push.ts @@ -8,20 +8,20 @@ export function pushEvent>( ): MaybeAstroPushPromiseReturn { 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 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>) { - pushResult = e.detail - } + window.addEventListener( + resultEventName, + ((e: CustomEvent>) => { + 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: { @@ -32,6 +32,7 @@ export function pushEvent>( }) ) + // Return the result return pushResult }