magicEchoConnectionEnricher function
Enricher: emits magicEchoConnection: connected|reconnecting|disconnected
for the current broadcast connection state.
The state is read from a module-private cache (MagicDuskIntegration
static field _lastEchoState) that is updated by a StreamSubscription
installed in MagicDuskIntegration.install. When no stream event has
fired yet, falls back to BroadcastDriver.isConnected to derive the
initial state without spinning up a new subscription.
Returns null when the 'broadcasting' binding is not present in the
Magic container (i.e. BroadcastServiceProvider was not registered).
Implementation
String? magicEchoConnectionEnricher(Element element, RefRegistry refs) {
if (!Magic.bound('broadcasting')) return null;
// 1. Prefer the stream-cached state (updated in install()).
final String? cached = MagicDuskIntegration._lastEchoState;
if (cached != null) return 'magicEchoConnection: $cached';
// 2. Fall back to the sync isConnected heuristic when no stream event
// has fired yet (driver starts up or subscription not yet installed).
final bool connected = Echo.manager.connection().isConnected;
final String state = connected ? 'connected' : 'disconnected';
return 'magicEchoConnection: $state';
}