From 6bbade2a2901def3d8d81de7fc5bc4375d7e437b Mon Sep 17 00:00:00 2001 From: Jose Olarte III Date: Wed, 6 May 2026 17:11:10 +0800 Subject: [PATCH] feat(notifications): refresh on mount and resume with debounce Trigger refreshNotifications on composable mount and document resume, using a debounced/in-flight guarded wrapper to avoid rapid duplicate refresh calls. Expose the debounced refresh function from useNotifications. --- src/composables/useNotifications.ts | 36 ++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/src/composables/useNotifications.ts b/src/composables/useNotifications.ts index 78790324..2f04ec59 100644 --- a/src/composables/useNotifications.ts +++ b/src/composables/useNotifications.ts @@ -1,7 +1,8 @@ /* eslint-disable @typescript-eslint/no-unused-vars */ -import { inject } from "vue"; +import { inject, onBeforeUnmount, onMounted } from "vue"; import { NotificationIface } from "../constants/app"; import { registerToken } from "@/services/notifications/NotificationService"; +import { refreshNotifications } from "@/services/notifications/NativeNotificationService"; /** * Vue 3 composable for notifications @@ -30,6 +31,38 @@ export function useNotifications() { ); } + let refreshTimer: number | undefined = undefined; + let refreshInFlight: Promise | null = null; + + async function refreshNotificationsDebounced(): Promise { + if (refreshTimer != null) { + window.clearTimeout(refreshTimer); + } + refreshTimer = window.setTimeout(() => { + if (!refreshInFlight) { + refreshInFlight = refreshNotifications().finally(() => { + refreshInFlight = null; + }); + } + }, 300); + } + + const onResume = () => { + void refreshNotificationsDebounced(); + }; + + onMounted(() => { + void refreshNotificationsDebounced(); + document.addEventListener("resume", onResume); + }); + + onBeforeUnmount(() => { + document.removeEventListener("resume", onResume); + if (refreshTimer != null) { + window.clearTimeout(refreshTimer); + } + }); + // eslint-disable-next-line @typescript-eslint/no-unused-vars function success(_notification: NotificationIface, _timeout?: number) {} // eslint-disable-next-line @typescript-eslint/no-unused-vars @@ -96,5 +129,6 @@ export function useNotifications() { downloadStarted, /** POST FCM token to `/notifications/register` (same as startup native hook). */ registerFcmToken: registerToken, + refreshNotifications: refreshNotificationsDebounced, }; }