subxt: Remove tokio as dependency, use it only for testing

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>
This commit is contained in:
Alexandru Vasile
2024-03-05 18:00:21 +02:00
parent 4c3226e70a
commit fbe2a7498b
3 changed files with 39 additions and 22 deletions
+36 -20
View File
@@ -94,27 +94,43 @@ impl RawLightClient {
let raw_rpc = self.raw_rpc.for_chain(chain_id);
let rpc_client = RpcClient::new(raw_rpc.clone());
use crate::backend::unstable::UnstableBackend;
use std::sync::Arc;
let (backend, mut driver) = UnstableBackend::builder().build(rpc_client);
let future = async move {
use futures::StreamExt;
while let Some(val) = driver.next().await {
if let Err(e) = val {
// This is a test; bail if something does wrong and try to
// ensure that the message makes it to some logs.
panic!("Error driving unstable backend in tests: {e}");
// Production code should use the legacy backend.
#[cfg(not(test))]
async fn build_online_client<TChainConfig: Config>(
rpc_client: RpcClient,
) -> Result<OnlineClient<TChainConfig>, crate::Error> {
OnlineClient::<TChainConfig>::from_rpc_client(rpc_client).await
}
// For testing we are only interested in using the unstable-backend with
// the lightclient.
#[cfg(test)]
async fn build_online_client<TChainConfig: Config>(
rpc_client: RpcClient,
) -> Result<OnlineClient<TChainConfig>, crate::Error> {
use crate::backend::unstable::UnstableBackend;
use std::sync::Arc;
let (backend, mut driver) = UnstableBackend::builder().build(rpc_client);
let future = async move {
use futures::StreamExt;
while let Some(val) = driver.next().await {
if let Err(e) = val {
// This is a test; bail if something does wrong and try to
// ensure that the message makes it to some logs.
panic!("Error driving unstable backend in tests: {e}");
}
}
}
};
// The unstable backend needs driving:
#[cfg(feature = "native")]
tokio::spawn(future);
#[cfg(feature = "web")]
wasm_bindgen_futures::spawn_local(future);
let client = OnlineClient::from_backend(Arc::new(backend))
.await
.map_err(|e| format!("Cannot construct OnlineClient from backend: {e}"))?;
};
// The unstable backend needs driving:
#[cfg(feature = "native")]
tokio::spawn(future);
#[cfg(feature = "web")]
wasm_bindgen_futures::spawn_local(future);
OnlineClient::from_backend(Arc::new(backend)).await
}
let client = build_online_client(rpc_client).await?;
Ok(LightClient { client, chain_id })
}