From 26c6a74641ed47277c1dfcc98b27ea009eb95c5b Mon Sep 17 00:00:00 2001 From: cpu Date: Sat, 29 Mar 2025 06:34:38 +0100 Subject: [PATCH] fix for handling messages --- src/js/app.js | 8 ++++++++ src/js/services/pushFlicIntegration.js | 15 +++++++++++---- src/js/services/serviceWorkerManager.js | 12 ++++-------- sw.js | 16 ++++++---------- 4 files changed, 29 insertions(+), 22 deletions(-) diff --git a/src/js/app.js b/src/js/app.js index f4db2f6..e603bab 100644 --- a/src/js/app.js +++ b/src/js/app.js @@ -80,6 +80,14 @@ async function initialize() { [config.FLIC_ACTIONS.DOUBLE_CLICK]: playerManager.previousPlayer, [config.FLIC_ACTIONS.HOLD]: gameActions.togglePauseResume, }; + + // Log the registered handlers for debugging + console.log("Registering Flic action handlers:", { + "SingleClick": config.FLIC_ACTIONS.SINGLE_CLICK, + "DoubleClick": config.FLIC_ACTIONS.DOUBLE_CLICK, + "Hold": config.FLIC_ACTIONS.HOLD + }); + serviceWorkerManager.setFlicActionHandlers(flicActionHandlers); // 8. Setup Service Worker (which also initializes Flic) diff --git a/src/js/services/pushFlicIntegration.js b/src/js/services/pushFlicIntegration.js index 142c0c9..26012ff 100644 --- a/src/js/services/pushFlicIntegration.js +++ b/src/js/services/pushFlicIntegration.js @@ -250,7 +250,7 @@ export function handleFlicAction(action, buttonId, timestamp, batteryLevel) { console.log(`[PushFlic] Received Action: ${action} from Button: ${buttonId} battery: ${batteryLevel}% at ${timestamp}`); // Check if battery is below threshold and show warning if needed - if (batteryLevel < FLIC_BATTERY_THRESHOLD) { + if (batteryLevel !== undefined && batteryLevel < FLIC_BATTERY_THRESHOLD) { showBatteryWarning(batteryLevel); } @@ -262,12 +262,19 @@ export function handleFlicAction(action, buttonId, timestamp, batteryLevel) { // Find the registered handler for this action const handler = actionHandlers[action]; + if (handler && typeof handler === 'function') { console.log(`[PushFlic] Executing handler for ${action}`); - // Execute the handler registered in app.js - handler(); // Use the handler function directly instead of hardcoded function calls + try { + // Execute the handler registered in app.js + handler(); + // Log success + console.log(`[PushFlic] Successfully executed handler for ${action}`); + } catch (error) { + console.error(`[PushFlic] Error executing handler for ${action}:`, error); + } } else { - console.warn(`[PushFlic] No handler registered for action: ${action}`); + console.warn(`[PushFlic] No handler registered for action: ${action}. Available handlers:`, Object.keys(actionHandlers)); } } diff --git a/src/js/services/serviceWorkerManager.js b/src/js/services/serviceWorkerManager.js index 092cea4..4f98fd4 100644 --- a/src/js/services/serviceWorkerManager.js +++ b/src/js/services/serviceWorkerManager.js @@ -55,18 +55,18 @@ export function setupServiceWorker(messageHandler) { // Set up global message event listener on window object window.addEventListener('message', handleServiceWorkerMessage); + // Listen for messages FROM the Service Worker + // This is the main way messages from the service worker are received + navigator.serviceWorker.addEventListener('message', messageHandler); + window.addEventListener('load', () => { navigator.serviceWorker.register('/sw.js') .then(registration => { console.log('ServiceWorker registered successfully.'); - - // Listen for messages FROM the Service Worker - navigator.serviceWorker.addEventListener('message', messageHandler); // Add an event listener that will work with service worker controlled clients if (navigator.serviceWorker.controller) { console.log('Service worker already controlling the page, setting up message listener'); - navigator.serviceWorker.controller.addEventListener('message', messageHandler); } // Initialize Flic integration @@ -80,10 +80,6 @@ export function setupServiceWorker(messageHandler) { // Listen for SW controller changes navigator.serviceWorker.addEventListener('controllerchange', () => { console.log('Service Worker controller changed, potentially updated.'); - // Re-attach event listener to the new controller - if (navigator.serviceWorker.controller) { - navigator.serviceWorker.controller.addEventListener('message', messageHandler); - } }); } else { diff --git a/sw.js b/sw.js index 3953e71..76740a4 100644 --- a/sw.js +++ b/sw.js @@ -151,10 +151,8 @@ self.addEventListener('push', event => { client.postMessage(messagePayload); messageSent = true; - // Also try to focus the client to ensure it gets the message - if ('focus' in client) { - return client.focus().then(() => true); - } + // REMOVED: Don't try to focus the client as it causes errors + // Just return true to indicate message was sent return Promise.resolve(true); } catch (error) { console.error('[ServiceWorker] Error posting message to client:', error); @@ -162,17 +160,15 @@ self.addEventListener('push', event => { } }); - // Decide whether to still show a notification even if a window is open. - // Generally good practice unless you are SURE the app will handle it visibly. - // You might choose *not* to show a notification if a client was found and focused. - // For simplicity here, we'll still show one. Adjust as needed. - if (!messageSent) { // Only show notification if no message was sent? Or always show? + return Promise.all(sendPromises).then(() => { + // Always show a notification unless we're sure the app can handle it visibly + // This ensures the user gets notified even if the app doesn't process the message return self.registration.showNotification(pushData.title, { body: pushData.body, icon: '/icons/android-chrome-192x192.png', data: pushData.data }); - } + }); }) );