mirror of
https://github.com/meshtastic/web.git
synced 2026-08-01 15:36:28 -04:00
useConnections went from 661 lines down to 264 by extracting three single-responsibility helpers under packages/web/src/core/connections/. The hook is now pure orchestration; transport / heartbeat / SDK-client / status-probe concerns live in their own files. New modules: - core/connections/heartbeat.ts (45 LOC): startConfigHeartbeat (5s), startMaintenanceHeartbeat (5min), stopHeartbeat. Owns the heartbeats Map; replaces the inline interval bookkeeping. Both helpers stop the prior heartbeat first so callers don't have to. - core/connections/sdkClient.ts (61 LOC): buildMeshDevice(connId, deviceId, transport) opens the OPFS DB, builds the four sqlocal repositories (chat / draft / nodes / telemetry), and constructs the MeshDevice with the canonical retention defaults (chat 90d / 1k per bucket; telemetry 30d / 500 per node). Falls back to in-memory when sqlocal is unavailable. - core/connections/transports.ts (236 LOC): per-transport openTransport factory (HTTP reachability check, BT permission re-acquisition with optional prompt, Serial port lookup with close-then-reopen), probeConnection for refreshStatuses, closeTransport for cleanup. Discriminates over conn.type so useConnections doesn't carry the switch logic. useConnections (now 264 LOC): - Owns the cachedTransports + configSubscriptions maps and the Zustand selectors. - teardown(id, conn) consolidates the heartbeat + config-sub + meshDevice.disconnect + transport-close cleanup that used to be duplicated across removeConnection / disconnect. - connect calls openTransport and forwards the resulting transport + cached BT/Serial handle into setupMeshDevice. The BT gattserverdisconnected listener is wired here (it needs the connId). - refreshStatuses simplifies to a filter + Promise.all over probeConnection. - syncConnectionStatuses unchanged. Behavior is unchanged. No new tests — the existing web suite (236) still passes; build clean.