121 lines
4.9 KiB
JavaScript
121 lines
4.9 KiB
JavaScript
// serviceWorkerManager.js - Service worker registration and Flic integration
|
|
import * as config from '../config.js';
|
|
import * as pushFlic from './pushFlicIntegration.js';
|
|
|
|
// Store the action handlers passed from app.js
|
|
let flicActionHandlers = {};
|
|
|
|
export function setFlicActionHandlers(handlers) {
|
|
if (handlers && Object.keys(handlers).length > 0) {
|
|
flicActionHandlers = handlers;
|
|
console.log('[ServiceWorkerManager] Stored action handlers:', Object.keys(flicActionHandlers));
|
|
|
|
// Always pass handlers to pushFlic, regardless of service worker state
|
|
pushFlic.initPushFlic(flicActionHandlers);
|
|
} else {
|
|
console.warn('[ServiceWorkerManager] No action handlers provided to setFlicActionHandlers!');
|
|
}
|
|
}
|
|
|
|
// --- Flic Integration Setup ---
|
|
export function initFlic() {
|
|
// Make sure we have handlers before initializing
|
|
if (Object.keys(flicActionHandlers).length === 0) {
|
|
console.warn('[ServiceWorkerManager] No Flic handlers registered before initFlic! Actions may not work.');
|
|
}
|
|
|
|
// This function is used by setupServiceWorker and relies on
|
|
// flicActionHandlers being set before this is called
|
|
console.log('[ServiceWorkerManager] Initializing PushFlic with handlers:', Object.keys(flicActionHandlers));
|
|
pushFlic.initPushFlic(flicActionHandlers);
|
|
}
|
|
|
|
// Export functions for manually triggering push notifications setup
|
|
export function setupPushNotifications() {
|
|
pushFlic.setupPushNotifications();
|
|
}
|
|
|
|
export function forceCredentialsPrompt() {
|
|
pushFlic.forceCredentialsPrompt();
|
|
}
|
|
|
|
// --- Handle Messages from Service Worker ---
|
|
|
|
export function flicMessageHandler(event) {
|
|
// This function is passed to setupServiceWorker and called when a message arrives from the service worker
|
|
console.log('[App] Message received from Service Worker:', event.data);
|
|
|
|
// Check if this is a Flic action message
|
|
if (event.data && event.data.type === 'flic-action') {
|
|
const { action, button, timestamp, batteryLevel } = event.data;
|
|
|
|
try {
|
|
// Pass to push-flic service to handle
|
|
pushFlic.handleFlicAction(action, button, timestamp, batteryLevel);
|
|
} catch (error) {
|
|
console.error('[App] Error handling flic action:', error);
|
|
}
|
|
}
|
|
}
|
|
|
|
// Global message handler function to ensure we catch all service worker messages
|
|
function handleServiceWorkerMessage(event) {
|
|
// Check if the message might be from our service worker
|
|
if (event.data && typeof event.data === 'object') {
|
|
console.log('[App] Potential window message received:', event.data);
|
|
|
|
// If it looks like a flic action message, handle it
|
|
if (event.data.type === 'flic-action') {
|
|
try {
|
|
// Process the message with our flicMessageHandler
|
|
flicMessageHandler(event);
|
|
} catch (error) {
|
|
console.error('[App] Error handling window message:', error);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// --- Service Worker and PWA Setup ---
|
|
export function setupServiceWorker(messageHandler) {
|
|
if ('serviceWorker' in navigator) {
|
|
console.log('[ServiceWorkerManager] Setting up service worker...');
|
|
|
|
// 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', event => {
|
|
console.log('[ServiceWorkerManager] Service worker message received:', event.data);
|
|
messageHandler(event);
|
|
});
|
|
|
|
window.addEventListener('load', () => {
|
|
console.log('[ServiceWorkerManager] Window loaded, registering service worker...');
|
|
navigator.serviceWorker.register('/sw.js')
|
|
.then(registration => {
|
|
console.log('[ServiceWorkerManager] ServiceWorker registered successfully.');
|
|
|
|
// Add an event listener that will work with service worker controlled clients
|
|
if (navigator.serviceWorker.controller) {
|
|
console.log('[ServiceWorkerManager] Service worker already controlling the page.');
|
|
}
|
|
|
|
// Initialize Flic integration
|
|
initFlic();
|
|
})
|
|
.catch(error => {
|
|
console.error('[ServiceWorkerManager] ServiceWorker registration failed:', error);
|
|
});
|
|
});
|
|
|
|
// Listen for SW controller changes
|
|
navigator.serviceWorker.addEventListener('controllerchange', () => {
|
|
console.log('[ServiceWorkerManager] Service Worker controller changed, potentially updated.');
|
|
});
|
|
|
|
} else {
|
|
console.warn('[ServiceWorkerManager] ServiceWorker not supported.');
|
|
}
|
|
}
|