mirror of
https://github.com/zjs81/meshcore-open.git
synced 2026-07-11 19:17:04 +10:00
Merge pull request #239 from zjs81/dev-notifyListener
Implements a debounced notification listener in MeshCoreConnector
This commit is contained in:
@@ -130,10 +130,13 @@ class MeshCoreConnector extends ChangeNotifier {
|
|||||||
StreamSubscription<List<ScanResult>>? _scanSubscription;
|
StreamSubscription<List<ScanResult>>? _scanSubscription;
|
||||||
StreamSubscription<BluetoothConnectionState>? _connectionSubscription;
|
StreamSubscription<BluetoothConnectionState>? _connectionSubscription;
|
||||||
StreamSubscription<List<int>>? _notifySubscription;
|
StreamSubscription<List<int>>? _notifySubscription;
|
||||||
|
Timer? _notifyListenersTimer;
|
||||||
Timer? _selfInfoRetryTimer;
|
Timer? _selfInfoRetryTimer;
|
||||||
Timer? _reconnectTimer;
|
Timer? _reconnectTimer;
|
||||||
Timer? _batteryPollTimer;
|
Timer? _batteryPollTimer;
|
||||||
int _reconnectAttempts = 0;
|
int _reconnectAttempts = 0;
|
||||||
|
bool _notifyListenersDirty = false;
|
||||||
|
static const Duration _notifyListenersDebounce = Duration(milliseconds: 50);
|
||||||
|
|
||||||
final StreamController<Uint8List> _receivedFramesController =
|
final StreamController<Uint8List> _receivedFramesController =
|
||||||
StreamController<Uint8List>.broadcast();
|
StreamController<Uint8List>.broadcast();
|
||||||
@@ -3765,11 +3768,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
|
@override
|
||||||
void dispose() {
|
void dispose() {
|
||||||
_scanSubscription?.cancel();
|
_scanSubscription?.cancel();
|
||||||
_connectionSubscription?.cancel();
|
_connectionSubscription?.cancel();
|
||||||
_notifySubscription?.cancel();
|
_notifySubscription?.cancel();
|
||||||
|
_notifyListenersTimer?.cancel();
|
||||||
_reconnectTimer?.cancel();
|
_reconnectTimer?.cancel();
|
||||||
_batteryPollTimer?.cancel();
|
_batteryPollTimer?.cancel();
|
||||||
_receivedFramesController.close();
|
_receivedFramesController.close();
|
||||||
|
|||||||
Reference in New Issue
Block a user