diff --git a/views/settings/settingsBase.html b/views/settings/settingsBase.html index 88f7949..02ed177 100644 --- a/views/settings/settingsBase.html +++ b/views/settings/settingsBase.html @@ -38,6 +38,43 @@ }, 50); // Small delay to ensure Alpine has finished rendering }); }); + + // Create a singleton SSE manager + window.SSEManager = window.SSEManager || { + eventSource: null, + notificationCallbacks: new Set(), + + init() { + if (this.eventSource) { + return; // Already initialized + } + + this.eventSource = new EventSource('/sse'); + this.eventSource.onmessage = (event) => { + const notification = JSON.parse(event.data); + this.notificationCallbacks.forEach(callback => callback(notification)); + }; + + // Clean up on page unload + window.addEventListener('unload', () => this.cleanup()); + }, + + cleanup() { + if (this.eventSource) { + this.eventSource.close(); + this.eventSource = null; + } + this.notificationCallbacks.clear(); + }, + + subscribe(callback) { + this.notificationCallbacks.add(callback); + }, + + unsubscribe(callback) { + this.notificationCallbacks.delete(callback); + } + };