return new client to CachedElectrumXClient from callback in interface

This commit is contained in:
sneurlax 2024-02-15 16:33:02 -06:00
parent a52f45a4ae
commit 3d942f3e0b
2 changed files with 19 additions and 7 deletions

View file

@ -22,12 +22,12 @@ import 'package:string_validator/string_validator.dart';
class CachedElectrumXClient {
final ElectrumXClient electrumXClient;
final ElectrumClient electrumAdapterClient;
final Future<void> Function() electrumAdapterUpdateCallback;
ElectrumClient electrumAdapterClient;
final Future<ElectrumClient> Function() electrumAdapterUpdateCallback;
static const minCacheConfirms = 30;
const CachedElectrumXClient({
CachedElectrumXClient({
required this.electrumXClient,
required this.electrumAdapterClient,
required this.electrumAdapterUpdateCallback,
@ -36,7 +36,7 @@ class CachedElectrumXClient {
factory CachedElectrumXClient.from({
required ElectrumXClient electrumXClient,
required ElectrumClient electrumAdapterClient,
required Future<void> Function() electrumAdapterUpdateCallback,
required Future<ElectrumClient> Function() electrumAdapterUpdateCallback,
}) =>
CachedElectrumXClient(
electrumXClient: electrumXClient,
@ -47,8 +47,13 @@ class CachedElectrumXClient {
/// If the client is closed, use the callback to update it.
_checkElectrumAdapterClient() async {
if (electrumAdapterClient.peer.isClosed) {
await electrumAdapterUpdateCallback?.call();
// throw Exception("ElectrumAdapterClient is closed");
ElectrumClient? _electrumAdapterClient =
await electrumAdapterUpdateCallback?.call();
if (_electrumAdapterClient != null) {
electrumAdapterClient = _electrumAdapterClient;
} else {
throw Exception("ElectrumAdapterClient is closed");
}
}
}
@ -215,6 +220,8 @@ class CachedElectrumXClient {
if (cachedTx == null) {
await _checkElectrumAdapterClient();
print(121212);
print(electrumAdapterClient.peer.isClosed);
final Map<String, dynamic> result =
await electrumAdapterClient.getTransaction(txHash);

View file

@ -983,7 +983,7 @@ mixin ElectrumXInterface<T extends Bip39HDCurrency> on Bip39HDWallet<T> {
electrumXCachedClient = CachedElectrumXClient.from(
electrumXClient: electrumXClient,
electrumAdapterClient: electrumAdapterClient,
electrumAdapterUpdateCallback: updateNode,
electrumAdapterUpdateCallback: updateClient,
);
subscribableElectrumXClient = SubscribableElectrumXClient.from(
node: newNode,
@ -1295,6 +1295,11 @@ mixin ElectrumXInterface<T extends Bip39HDCurrency> on Bip39HDWallet<T> {
await updateElectrumX(newNode: node);
}
Future<ElectrumClient> updateClient() async {
await updateNode();
return electrumAdapterClient;
}
FeeObject? _cachedFees;
@override