| 1 |
//! A version of `Delay` that works on wasm. |
| 2 |
|
| 3 |
use gloo_timers::future::TimeoutFuture; |
| 4 |
use send_wrapper::SendWrapper; |
| 5 |
use std::{ |
| 6 |
future::Future, |
| 7 |
pin::Pin, |
| 8 |
task::{Context, Poll}, |
| 9 |
time::Duration, |
| 10 |
}; |
| 11 |
|
| 12 |
/// A version of `Delay` that works on wasm. |
| 13 |
#[derive(Debug)] |
| 14 |
pub struct Delay(SendWrapper<TimeoutFuture>); |
| 15 |
|
| 16 |
impl Delay { |
| 17 |
/// Creates a new future which will fire at `dur` time into the future. |
| 18 |
#[inline] |
| 19 |
pub fn new(dur: Duration) -> Delay { |
| 20 |
Self(SendWrapper::new(TimeoutFuture::new(dur.as_millis() as u32))) |
| 21 |
} |
| 22 |
|
| 23 |
/// Resets the timeout. |
| 24 |
#[inline] |
| 25 |
pub fn reset(&mut self, dur: Duration) { |
| 26 |
*self = Delay::new(dur); |
| 27 |
} |
| 28 |
} |
| 29 |
|
| 30 |
impl Future for Delay { |
| 31 |
type Output = (); |
| 32 |
|
| 33 |
fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> { |
| 34 |
Pin::new(&mut *Pin::into_inner(self).0).poll(cx) |
| 35 |
} |
| 36 |
} |