fix: connection switching back and forth

This commit is contained in:
Rafael Saes 2024-05-24 11:15:33 -03:00
parent fb3c816817
commit da59860241
2 changed files with 14 additions and 9 deletions

View file

@ -41,7 +41,7 @@ class ElectrumClient {
bool get isConnected => _isConnected; bool get isConnected => _isConnected;
Socket? socket; Socket? socket;
void Function(bool)? onConnectionStatusChange; void Function(bool?)? onConnectionStatusChange;
int _id; int _id;
final Map<String, SocketTask> _tasks; final Map<String, SocketTask> _tasks;
Map<String, SocketTask> get tasks => _tasks; Map<String, SocketTask> get tasks => _tasks;
@ -91,7 +91,7 @@ class ElectrumClient {
_setIsConnected(false); _setIsConnected(false);
}, onDone: () { }, onDone: () {
unterminatedString = ''; unterminatedString = '';
_setIsConnected(false); _setIsConnected(null);
}); });
keepAlive(); keepAlive();
} }
@ -146,7 +146,7 @@ class ElectrumClient {
await callWithTimeout(method: 'server.ping'); await callWithTimeout(method: 'server.ping');
_setIsConnected(true); _setIsConnected(true);
} on RequestFailedTimeoutException catch (_) { } on RequestFailedTimeoutException catch (_) {
_setIsConnected(false); _setIsConnected(null);
} }
} }
@ -480,12 +480,12 @@ class ElectrumClient {
} }
} }
void _setIsConnected(bool isConnected) { void _setIsConnected(bool? isConnected) {
if (_isConnected != isConnected) { if (_isConnected != isConnected) {
onConnectionStatusChange?.call(isConnected); onConnectionStatusChange?.call(isConnected);
} }
_isConnected = isConnected; _isConnected = isConnected ?? false;
} }
void _handleResponse(Map<String, dynamic> response) { void _handleResponse(Map<String, dynamic> response) {

View file

@ -235,7 +235,10 @@ abstract class ElectrumWalletBase
} }
Future<int> getUpdatedChainTip() async { Future<int> getUpdatedChainTip() async {
_currentChainTip = await electrumClient.getCurrentBlockChainTip(); final newTip = await electrumClient.getCurrentBlockChainTip();
if (newTip != null && newTip > (_currentChainTip ?? 0)) {
_currentChainTip = newTip;
}
return _currentChainTip ?? 0; return _currentChainTip ?? 0;
} }
@ -444,13 +447,15 @@ abstract class ElectrumWalletBase
await electrumClient.close(); await electrumClient.close();
electrumClient.onConnectionStatusChange = (bool isConnected) async { electrumClient.onConnectionStatusChange = (bool? isConnected) async {
if (syncStatus is SyncingSyncStatus) return; if (syncStatus is SyncingSyncStatus) return;
if (isConnected && syncStatus is! SyncedSyncStatus) { if (isConnected == true && syncStatus is! SyncedSyncStatus) {
syncStatus = ConnectedSyncStatus(); syncStatus = ConnectedSyncStatus();
} else if (!isConnected) { } else if (isConnected == false) {
syncStatus = LostConnectionSyncStatus(); syncStatus = LostConnectionSyncStatus();
} else if (!(isConnected ?? false) && syncStatus is! ConnectingSyncStatus) {
syncStatus = NotConnectedSyncStatus();
} }
}; };