From 2a62390903e3b9327e9df5499f7a0d38396c7da3 Mon Sep 17 00:00:00 2001 From: Winston Lowe Date: Wed, 25 Feb 2026 21:58:35 -0800 Subject: [PATCH] Implement debounced notification listener updates in MeshCoreConnector --- lib/connector/meshcore_connector.dart | 38 +++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/lib/connector/meshcore_connector.dart b/lib/connector/meshcore_connector.dart index ef19f02..4c7dbd8 100644 --- a/lib/connector/meshcore_connector.dart +++ b/lib/connector/meshcore_connector.dart @@ -127,10 +127,13 @@ class MeshCoreConnector extends ChangeNotifier { StreamSubscription>? _scanSubscription; StreamSubscription? _connectionSubscription; StreamSubscription>? _notifySubscription; + Timer? _notifyListenersTimer; Timer? _selfInfoRetryTimer; Timer? _reconnectTimer; Timer? _batteryPollTimer; int _reconnectAttempts = 0; + bool _notifyListenersDirty = false; + static const Duration _notifyListenersDebounce = Duration(milliseconds: 50); final StreamController _receivedFramesController = StreamController.broadcast(); @@ -3648,11 +3651,46 @@ class MeshCoreConnector extends ChangeNotifier { } } + void markNotifyDirty() { + if (_notifyListenersDirty && _notifyListenersTimer != null) { + return; + } + + _notifyListenersDirty = true; + _notifyListenersTimer ??= Timer( + _notifyListenersDebounce, + _flushBatchedNotify, + ); + } + + void _flushBatchedNotify() { + _notifyListenersTimer = null; + if (!_notifyListenersDirty) { + return; + } + + _notifyListenersDirty = false; + super.notifyListeners(); + + if (_notifyListenersDirty && _notifyListenersTimer == null) { + _notifyListenersTimer = Timer( + _notifyListenersDebounce, + _flushBatchedNotify, + ); + } + } + + @override + void notifyListeners() { + markNotifyDirty(); + } + @override void dispose() { _scanSubscription?.cancel(); _connectionSubscription?.cancel(); _notifySubscription?.cancel(); + _notifyListenersTimer?.cancel(); _reconnectTimer?.cancel(); _batteryPollTimer?.cancel(); _receivedFramesController.close();