use crate::ProviderError;
use ethers_core::types::U256;
use futures_timer::Delay;
use futures_util::{stream, FutureExt, StreamExt};
use std::{future::Future, pin::Pin};
pub type EscalationPolicy = Box<dyn Fn(U256, usize) -> U256 + Send + Sync>;
#[cfg(target_arch = "wasm32")]
pub(crate) type PinBoxFut<'a, T> = Pin<Box<dyn Future<Output = Result<T, ProviderError>> + 'a>>;
#[cfg(not(target_arch = "wasm32"))]
pub(crate) type PinBoxFut<'a, T> =
Pin<Box<dyn Future<Output = Result<T, ProviderError>> + Send + 'a>>;
pub async fn maybe<F, T, E>(item: Option<T>, f: F) -> Result<T, E>
where
F: Future<Output = Result<T, E>>,
{
if let Some(item) = item {
futures_util::future::ok(item).await
} else {
f.await
}
}
pub fn interval(
duration: instant::Duration,
) -> impl futures_core::stream::Stream<Item = ()> + Send + Unpin {
stream::unfold((), move |_| Delay::new(duration).map(|_| Some(((), ())))).map(drop)
}