From 46285d44ea0fe1ce8ed83f2930b523e874b3f7d9 Mon Sep 17 00:00:00 2001 From: sneurlax Date: Tue, 6 Feb 2024 20:55:31 -0600 Subject: [PATCH] don't subscribeBlockHeaders if subscription exists --- .../electrumx_interface.dart | 56 +++++++++---------- 1 file changed, 28 insertions(+), 28 deletions(-) diff --git a/lib/wallets/wallet/wallet_mixin_interfaces/electrumx_interface.dart b/lib/wallets/wallet/wallet_mixin_interfaces/electrumx_interface.dart index f6fba06ca..24b9f528e 100644 --- a/lib/wallets/wallet/wallet_mixin_interfaces/electrumx_interface.dart +++ b/lib/wallets/wallet/wallet_mixin_interfaces/electrumx_interface.dart @@ -833,39 +833,39 @@ mixin ElectrumXInterface on Bip39HDWallet { if (_latestHeight != null) { return _latestHeight!; } - } + } else { + // Make sure we only complete once. + bool isFirstResponse = true; - // Make sure we only complete once. - bool isFirstResponse = true; + // Subscribe to block headers. + final subscription = + subscribableElectrumXClient.subscribeToBlockHeaders(); - // Subscribe to block headers. - final subscription = - subscribableElectrumXClient.subscribeToBlockHeaders(); + // set stream subscription + ElectrumxChainHeightService.subscriptions[cryptoCurrency.coin] = + subscription.responseStream.asBroadcastStream().listen((event) { + final response = event; + if (response != null && + response is Map && + response.containsKey('height')) { + final int chainHeight = response['height'] as int; + // print("Current chain height: $chainHeight"); + _latestHeight = chainHeight; - // set stream subscription - ElectrumxChainHeightService.subscriptions[cryptoCurrency.coin] ??= - subscription.responseStream.asBroadcastStream().listen((event) { - final response = event; - if (response != null && - response is Map && - response.containsKey('height')) { - final int chainHeight = response['height'] as int; - // print("Current chain height: $chainHeight"); - _latestHeight = chainHeight; + if (isFirstResponse) { + isFirstResponse = false; - if (isFirstResponse) { - isFirstResponse = false; - - // Return the chain height. - completer.complete(chainHeight); + // Return the chain height. + completer.complete(chainHeight); + } + } else { + Logging.instance.log( + "blockchain.headers.subscribe returned malformed response\n" + "Response: $response", + level: LogLevel.Error); } - } else { - Logging.instance.log( - "blockchain.headers.subscribe returned malformed response\n" - "Response: $response", - level: LogLevel.Error); - } - }); + }); + } // Wait for first response. return completer.future;