use crate::callback::{Timeout, Interval};
use wasm_bindgen::prelude::*;
use futures_channel::{oneshot, mpsc};
use futures_core::stream::Stream;
use std::future::Future;
use std::pin::Pin;
use std::task::{Poll, Context};
#[derive(Debug)]
#[must_use = "futures do nothing unless polled or spawned"]
pub struct TimeoutFuture {
inner: Timeout,
rx: oneshot::Receiver<()>,
}
impl TimeoutFuture {
pub fn new(millis: u32) -> TimeoutFuture {
let (tx, rx) = oneshot::channel();
let inner = Timeout::new(millis, move || {
tx.send(()).unwrap_throw();
});
TimeoutFuture { inner, rx }
}
}
impl Future for TimeoutFuture {
type Output = ();
fn poll(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
Future::poll(Pin::new(&mut self.rx), cx).map(|t| t.unwrap_throw())
}
}
#[derive(Debug)]
#[must_use = "streams do nothing unless polled or spawned"]
pub struct IntervalStream {
receiver: mpsc::UnboundedReceiver<()>,
inner: Interval,
}
impl IntervalStream {
pub fn new(millis: u32) -> IntervalStream {
let (sender, receiver) = mpsc::unbounded();
let inner = Interval::new(millis, move || {
sender.unbounded_send(()).unwrap_throw();
});
IntervalStream { receiver, inner }
}
}
impl Stream for IntervalStream {
type Item = ();
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Option<Self::Item>> {
Stream::poll_next(Pin::new(&mut self.receiver), cx)
}
}