use futures::channel::mpsc::{UnboundedSender, unbounded};
use futures::{Future, SinkExt, StreamExt};
use wasm_bindgen::JsValue;
use wasm_bindgen_futures::spawn_local;
#[derive(Clone)]
pub struct LocalPollLoop<R: Send + Sync + 'static>(UnboundedSender<R>);
impl<R: Send + Sync + 'static> LocalPollLoop<R> {
pub fn new<F: Fn(R) -> Result<JsValue, JsValue> + 'static>(send: F) -> Self {
let (emit, mut receive) = unbounded::<R>();
spawn_local(async move {
while let Some(resp) = receive.next().await {
let resp = send(resp);
if let Err(err) = resp {
web_sys::console::error_2(&"Failed to serialize".into(), &err);
}
}
});
Self(emit)
}
pub fn new_async<F: Fn(R) -> FUT + 'static, FUT: Future<Output = Result<JsValue, JsValue>>>(
send: F,
) -> Self {
let (emit, mut receive) = unbounded::<R>();
spawn_local(async move {
while let Some(resp) = receive.next().await {
let resp = send(resp).await;
if let Err(err) = resp {
web_sys::console::error_2(&"Failed to serialize".into(), &err);
}
}
});
Self(emit)
}
pub fn poll(&self, msg: R) -> impl Future<Output = ()> + Send + Sync + 'static + use<R> {
let mut emit = self.0.clone();
async move { emit.send(msg).await.unwrap() }
}
}