use std::future::Future;
use std::pin::Pin;
use std::task::Context;
use std::task::Poll;
use std::time::Duration;
pub struct Timer {
#[cfg(not(target_arch = "wasm32"))]
inner: async_io::Timer,
#[cfg(target_arch = "wasm32")]
inner: gloo_timers::future::TimeoutFuture,
}
impl Timer {
pub fn after(duration: Duration) -> Self {
#[cfg(not(target_arch = "wasm32"))]
{
Self {
inner: async_io::Timer::after(duration),
}
}
#[cfg(target_arch = "wasm32")]
{
Self {
inner: gloo_timers::future::sleep(duration),
}
}
}
}
impl Future for Timer {
type Output = ();
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<()> {
#[cfg(not(target_arch = "wasm32"))]
{
Pin::new(&mut self.inner).poll(cx).map(|_| ())
}
#[cfg(target_arch = "wasm32")]
{
Pin::new(&mut self.inner).poll(cx)
}
}
}