Refator _manageChainHeightSubscription so we are not calling the listener multiple times

This commit is contained in:
likho 2024-02-14 17:46:01 +02:00
parent bc0d011d15
commit 7363438279

View file

@ -807,8 +807,57 @@ mixin ElectrumXInterface<T extends Bip39HDCurrency> on Bip39HDWallet<T> {
// Don't set a stream subscription if one already exists. // Don't set a stream subscription if one already exists.
if (ElectrumxChainHeightService.subscriptions[cryptoCurrency.coin] == if (ElectrumxChainHeightService.subscriptions[cryptoCurrency.coin] ==
null) { null) {
final Completer<int> completer = Completer<int>(); return _manageChainHeightSubscription();
}
// Don't set a stream subscription if one already exists.
else {
//IF there's already a wallet for a coin the chain height might not be
// stored for current wallet
// Check if the stream subscription is paused.
if (ElectrumxChainHeightService
.subscriptions[cryptoCurrency.coin]!.isPaused) {
// If it's paused, resume it.
ElectrumxChainHeightService.subscriptions[cryptoCurrency.coin]!
.resume();
}
if (_latestHeight == null) {
//Get the chain height
return _manageChainHeightSubscription();
}
// Causes synchronization to stall.
// // Check if the stream subscription is active by pinging it.
// if (!(await subscribableElectrumXClient.ping())) {
// // If it's not active, reconnect it.
// final node = await getCurrentElectrumXNode();
//
// await subscribableElectrumXClient.connect(
// host: node.address, port: node.port);
//
// // Wait for first response.
// return completer.future;
// }
if (_latestHeight != null) {
return _latestHeight!;
}
}
// Probably waiting on the subscription to receive the latest block height
// fallback to cached value
return info.cachedChainHeight;
} catch (e, s) {
Logging.instance.log(
"Exception rethrown in fetchChainHeight\nError: $e\nStack trace: $s",
level: LogLevel.Error);
// completer.completeError(e, s);
// return Future.error(e, s);
rethrow;
}
}
Future<int> _manageChainHeightSubscription() async {
final Completer<int> completer = Completer<int>();
// Make sure we only complete once. // Make sure we only complete once.
final isFirstResponse = _latestHeight == null; final isFirstResponse = _latestHeight == null;
@ -842,46 +891,6 @@ mixin ElectrumXInterface<T extends Bip39HDCurrency> on Bip39HDWallet<T> {
return _latestHeight ?? await completer.future; return _latestHeight ?? await completer.future;
} }
// Don't set a stream subscription if one already exists.
else {
// Check if the stream subscription is paused.
if (ElectrumxChainHeightService
.subscriptions[cryptoCurrency.coin]!.isPaused) {
// If it's paused, resume it.
ElectrumxChainHeightService.subscriptions[cryptoCurrency.coin]!
.resume();
}
// Causes synchronization to stall.
// // Check if the stream subscription is active by pinging it.
// if (!(await subscribableElectrumXClient.ping())) {
// // If it's not active, reconnect it.
// final node = await getCurrentElectrumXNode();
//
// await subscribableElectrumXClient.connect(
// host: node.address, port: node.port);
//
// // Wait for first response.
// return completer.future;
// }
if (_latestHeight != null) {
return _latestHeight!;
}
}
// Probably waiting on the subscription to receive the latest block height
// fallback to cached value
return info.cachedChainHeight;
} catch (e, s) {
Logging.instance.log(
"Exception rethrown in fetchChainHeight\nError: $e\nStack trace: $s",
level: LogLevel.Error);
// completer.completeError(e, s);
// return Future.error(e, s);
rethrow;
}
}
Future<int> fetchTxCount({required String addressScriptHash}) async { Future<int> fetchTxCount({required String addressScriptHash}) async {
final transactions = final transactions =