use std::pin::Pin;
use std::task::{Context, Poll};
use futures::stream::Stream;
use futures::channel::mpsc;
use wasm_bindgen::prelude::*;
pub struct ClosureStream {
receiver: mpsc::UnboundedReceiver<JsValue>,
}
impl ClosureStream {
pub fn new() -> (Closure<dyn FnMut(JsValue)>, Self) {
let (sender, receiver) = mpsc::unbounded();
let closure = Closure::new(move |data| {
sender.unbounded_send(data).unwrap_throw();
});
(closure, Self { receiver })
}
}
impl Stream for ClosureStream {
type Item = JsValue;
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Option<Self::Item>> {
Pin::new(&mut self.receiver).poll_next(cx)
}
}