mirror of
https://github.com/cypherstack/stack_wallet.git
synced 2024-12-24 12:29:37 +00:00
Refator _manageChainHeightSubscription so we are not calling the listener multiple times
This commit is contained in:
parent
bc0d011d15
commit
7363438279
1 changed files with 43 additions and 34 deletions
|
@ -807,43 +807,12 @@ 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();
|
||||||
|
|
||||||
// Make sure we only complete once.
|
|
||||||
final isFirstResponse = _latestHeight == null;
|
|
||||||
|
|
||||||
// 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;
|
|
||||||
|
|
||||||
if (isFirstResponse && !completer.isCompleted) {
|
|
||||||
// Return the chain height.
|
|
||||||
completer.complete(chainHeight);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
Logging.instance.log(
|
|
||||||
"blockchain.headers.subscribe returned malformed response\n"
|
|
||||||
"Response: $response",
|
|
||||||
level: LogLevel.Error);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
return _latestHeight ?? await completer.future;
|
|
||||||
}
|
}
|
||||||
// Don't set a stream subscription if one already exists.
|
// Don't set a stream subscription if one already exists.
|
||||||
else {
|
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.
|
// Check if the stream subscription is paused.
|
||||||
if (ElectrumxChainHeightService
|
if (ElectrumxChainHeightService
|
||||||
.subscriptions[cryptoCurrency.coin]!.isPaused) {
|
.subscriptions[cryptoCurrency.coin]!.isPaused) {
|
||||||
|
@ -851,6 +820,10 @@ mixin ElectrumXInterface<T extends Bip39HDCurrency> on Bip39HDWallet<T> {
|
||||||
ElectrumxChainHeightService.subscriptions[cryptoCurrency.coin]!
|
ElectrumxChainHeightService.subscriptions[cryptoCurrency.coin]!
|
||||||
.resume();
|
.resume();
|
||||||
}
|
}
|
||||||
|
if (_latestHeight == null) {
|
||||||
|
//Get the chain height
|
||||||
|
return _manageChainHeightSubscription();
|
||||||
|
}
|
||||||
|
|
||||||
// Causes synchronization to stall.
|
// Causes synchronization to stall.
|
||||||
// // Check if the stream subscription is active by pinging it.
|
// // Check if the stream subscription is active by pinging it.
|
||||||
|
@ -883,6 +856,42 @@ mixin ElectrumXInterface<T extends Bip39HDCurrency> on Bip39HDWallet<T> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Future<int> _manageChainHeightSubscription() async {
|
||||||
|
final Completer<int> completer = Completer<int>();
|
||||||
|
// Make sure we only complete once.
|
||||||
|
final isFirstResponse = _latestHeight == null;
|
||||||
|
|
||||||
|
// 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;
|
||||||
|
|
||||||
|
if (isFirstResponse && !completer.isCompleted) {
|
||||||
|
// Return the chain height.
|
||||||
|
completer.complete(chainHeight);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Logging.instance.log(
|
||||||
|
"blockchain.headers.subscribe returned malformed response\n"
|
||||||
|
"Response: $response",
|
||||||
|
level: LogLevel.Error);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return _latestHeight ?? await completer.future;
|
||||||
|
}
|
||||||
|
|
||||||
Future<int> fetchTxCount({required String addressScriptHash}) async {
|
Future<int> fetchTxCount({required String addressScriptHash}) async {
|
||||||
final transactions =
|
final transactions =
|
||||||
await electrumXClient.getHistory(scripthash: addressScriptHash);
|
await electrumXClient.getHistory(scripthash: addressScriptHash);
|
||||||
|
|
Loading…
Reference in a new issue