use crate::{RequestBuilder, SafeVk};
use std::{
future::{Future, IntoFuture},
pin::Pin,
sync::Arc,
task::{Context, Poll},
};
pub struct Polling {
request: Arc<RequestBuilder>,
safevk: SafeVk,
}
pub fn start_polling(token: &str, group_id: u32, safevk: SafeVk) -> Polling {
let request = Arc::new(RequestBuilder::new(token, group_id));
Polling { request, safevk }
}
impl IntoFuture for Polling {
type Output = std::io::Result<()>;
type IntoFuture = PollFuture;
fn into_future(self) -> Self::IntoFuture {
PollFuture(Box::pin(async move {
let Self { request, safevk } = self;
loop {
let request = Arc::clone(&request);
match request.build_long_poll_request().await {
Ok(res) => {
safevk.call_with_state(res, (), request);
}
Err(_err) => {}
}
}
}))
}
}
pub struct PollFuture(
pub(super) Pin<Box<dyn Future<Output = std::io::Result<()>> + Send + 'static>>,
);
impl Future for PollFuture {
type Output = std::io::Result<()>;
#[inline]
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
self.0.as_mut().poll(cx)
}
}