use futures::future::LocalBoxFuture;
use crate::prelude::*;
pub struct AsyncCallback<In, Out> {
inner: Rc<dyn Fn(In) -> LocalBoxFuture<'static, Out>>,
}
impl<In, Out> Clone for AsyncCallback<In, Out> {
fn clone(&self) -> Self {
Self {
inner: self.inner.clone(),
}
}
}
impl<In, Out> AsyncCallback<In, Out> {
pub fn new<F, Fut>(f: F) -> Self
where
F: Fn(In) -> Fut + 'static,
Fut: core::future::Future<Output = Out> + 'static,
{
Self {
inner: Rc::new(move |arg| Box::pin(f(arg))),
}
}
pub async fn call(&self, arg: In) -> Out {
(self.inner)(arg).await
}
}