From 464496222fd7fe37afa6fcbcba6856f5782729f4 Mon Sep 17 00:00:00 2001 From: Graham McIntire Date: Mon, 23 Mar 2026 14:29:51 -0500 Subject: [PATCH] fix: harden frontend reconnect callbacks --- assets/js/app.ts | 15 ++++++++++++--- assets/js/map.ts | 21 +++++++++++++++++++++ 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/assets/js/app.ts b/assets/js/app.ts index de42894..dfbf8d9 100644 --- a/assets/js/app.ts +++ b/assets/js/app.ts @@ -280,7 +280,7 @@ if (topbar) { // Handle connection draining reconnect events window.addEventListener("phx:reconnect", ((e: CustomEvent<{ delay?: number }>) => { - const delay = e.detail.delay || 1000; + const delay = e.detail?.delay || 1000; setTimeout(() => { liveSocket.disconnect(); setTimeout(() => { @@ -297,7 +297,11 @@ liveSocket.connect(); document.addEventListener("visibilitychange", () => { if (document.visibilityState === "visible") { const socket = (liveSocket as any).socket; - if (socket && socket.connectionState() !== "open") { + if ( + socket && + typeof socket.connectionState === "function" && + socket.connectionState() !== "open" + ) { socket.disconnect(() => socket.connect()); } } @@ -316,7 +320,12 @@ window.addEventListener("phx:live_socket:connect", (_info) => { // Also check periodically in case the event doesn't fire setTimeout(() => { const socket = (liveSocket as any).socket; - if (socket && socket.isConnected() && socket.fallbackTimer) { + if ( + socket && + typeof socket.isConnected === "function" && + socket.isConnected() && + socket.fallbackTimer + ) { clearTimeout(socket.fallbackTimer); socket.fallbackTimer = null; } diff --git a/assets/js/map.ts b/assets/js/map.ts index 63cf41e..ebdec4f 100644 --- a/assets/js/map.ts +++ b/assets/js/map.ts @@ -1018,15 +1018,36 @@ let MapAPRSMap = { if ("geolocation" in navigator) { navigator.geolocation.getCurrentPosition( (position) => { + if ( + !self.pushEvent || + typeof self.pushEvent !== "function" || + self.isDestroyed + ) { + return; + } const { latitude, longitude } = position.coords; self.pushEvent("set_location", { lat: latitude, lng: longitude }); }, (error) => { + if ( + !self.pushEvent || + typeof self.pushEvent !== "function" || + self.isDestroyed + ) { + return; + } console.warn("Geolocation error:", error.message); self.pushEvent("geolocation_error", { error: error.message }); }, ); } else { + if ( + !self.pushEvent || + typeof self.pushEvent !== "function" || + self.isDestroyed + ) { + return; + } console.warn("Geolocation not available"); self.pushEvent("geolocation_error", { error: "Geolocation not supported",