fix: auto-resubscribe persisted subscriptions on reconnect
The topic tree subscriptions were persisted in localStorage but never re-subscribed when the connection was re-established. Now when mqtt:status fires with 'connected', all saved subscriptions for that connection are automatically re-subscribed.
This commit is contained in:
@@ -14,6 +14,7 @@ import type { MqttMessage } from '../types/mqtt'
|
||||
import { useConnections } from './useConnections'
|
||||
import { useMessages } from './useMessages'
|
||||
import { useLogs } from './useLogs'
|
||||
import { useSubscriptions } from './useSubscriptions'
|
||||
import type { LogEntry } from './useLogs'
|
||||
|
||||
/**
|
||||
@@ -61,9 +62,18 @@ export function useMqttBridge() {
|
||||
case 'connecting':
|
||||
updateStatus(e.connection_id, 'connecting')
|
||||
break
|
||||
case 'connected':
|
||||
case 'connected': {
|
||||
updateStatus(e.connection_id, 'connected')
|
||||
// Auto-resubscribe all persisted subscriptions for this connection
|
||||
const { getSubscriptionsByConnection } = useSubscriptions()
|
||||
const subs = getSubscriptionsByConnection(e.connection_id)
|
||||
for (const sub of subs) {
|
||||
mqttSubscribe({ connection_id: e.connection_id, topic: sub.topic, qos: sub.qos }).catch(() => {
|
||||
// Silently ignore resubscribe errors — the user will see them in the log
|
||||
})
|
||||
}
|
||||
break
|
||||
}
|
||||
case 'disconnected':
|
||||
updateStatus(e.connection_id, 'disconnected')
|
||||
break
|
||||
|
||||
Reference in New Issue
Block a user