Test a callback

This commit is contained in:
artfuldev 2021-10-15 01:54:32 +05:30
parent 47651f3681
commit 41afeafcd3
2 changed files with 39 additions and 0 deletions

View File

@ -91,8 +91,14 @@ interface BlockchainConfig {
Esplora(EsploraConfig config);
};
callback interface BdkProgress {
void update(f32 progress, string? message);
};
interface OnlineWallet {
[Throws=BdkError]
constructor(string descriptor, Network network, DatabaseConfig database_config, BlockchainConfig blockchain_config);
Network get_network();
[Throws=BdkError]
void sync(BdkProgress progress_update, u32? max_address_param);
};

View File

@ -1,5 +1,6 @@
use bdk::bitcoin::Network;
use bdk::blockchain::any::{AnyBlockchain, AnyBlockchainConfig};
use bdk::blockchain::Progress;
use bdk::blockchain::{
electrum::ElectrumBlockchainConfig, esplora::EsploraBlockchainConfig, ConfigurableBlockchain,
};
@ -75,6 +76,24 @@ struct OnlineWallet {
wallet: Mutex<Wallet<AnyBlockchain, AnyDatabase>>,
}
pub trait BdkProgress: Send {
fn update(&self, progress: f32, message: Option<String>);
}
struct BdkProgressHolder {
progress_update: Mutex<Box<dyn BdkProgress>>,
}
impl Progress for BdkProgressHolder {
fn update(&self, progress: f32, message: Option<String>) -> Result<(), Error> {
self.progress_update
.lock()
.unwrap()
.update(progress, message);
Ok(())
}
}
impl OnlineWallet {
fn new(
descriptor: String,
@ -121,6 +140,20 @@ impl OnlineWallet {
fn get_network(&self) -> Network {
self.wallet.lock().unwrap().network()
}
fn sync(
&self,
progress_update: Box<dyn BdkProgress>,
max_address_param: Option<u32>,
) -> Result<(), BdkError> {
progress_update.update(21.0, Some("message".to_string()));
self.wallet.lock().unwrap().sync(
BdkProgressHolder {
progress_update: Mutex::new(progress_update),
},
max_address_param,
)
}
}
uniffi::deps::static_assertions::assert_impl_all!(OfflineWallet: Sync, Send);