fix: harden frontend reconnect callbacks

This commit is contained in:
Graham McIntire 2026-03-23 14:29:51 -05:00
parent 85858d8fd8
commit 464496222f
No known key found for this signature in database
2 changed files with 33 additions and 3 deletions

View file

@ -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;
}

View file

@ -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",