-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Using streams as event listener handlers #1158
Comments
@chpio Try this: use wasm_bindgen::{JsValue, JsCast};
use wasm_bindgen::closure::Closure;
use web_sys::EventTarget;
use futures::Poll;
use futures::stream::Stream;
use futures::sync::mpsc::{unbounded, UnboundedReceiver};
pub struct EventStream<'a, A> {
node: EventTarget,
kind: &'a str,
callback: Closure<Fn(JsValue)>,
receiver: UnboundedReceiver<A>,
}
impl<'a, A> EventStream<'a, A> where A: JsCast + 'static {
pub fn new(node: &EventTarget, kind: &'a str) -> Self {
let (sender, receiver) = unbounded();
let callback = Closure::wrap(Box::new(move |event: JsValue| {
sender.unbounded_send(event.dyn_into().unwrap()).unwrap();
}) as Box<Fn(JsValue)>);
node.add_event_listener_with_callback(kind, callback.as_ref().unchecked_ref()).unwrap();
Self {
node: node.clone(),
kind,
callback,
receiver,
}
}
}
impl<'a, A> Stream for EventStream<'a, A> {
type Item = A;
type Error = ();
#[inline]
fn poll(&mut self) -> Poll<Option<Self::Item>, Self::Error> {
self.receiver.poll()
}
}
impl<'a, A> Drop for EventStream<'a, A> {
#[inline]
fn drop(&mut self) {
self.node.remove_event_listener_with_callback(self.kind, self.callback.as_ref().unchecked_ref()).unwrap();
}
} This lets you easily create Streams from events: EventStream::new(&some_element, "click")
.map(|event: MouseEvent| {
...
}) Also, you should check out my Futures Signals crate. As its name suggests, it is based on top of Futures. It doesn't work for events (you should use Stream for that), but it's useful for other things, like dominator. |
To answer your questions:
You would generally do that by creating a pub trait StreamExt: Stream {
// define default methods in here
}
impl<T: ?Sized> StreamExt for T where T: Stream {} Here's a more detailed example. It's for Futures 0.3, but the same principles apply to Futures 0.1 Of course the best option is to contribute directly to the Futures crate, rather than creating a separate
You just impl |
I haven't tested it, so i might be wrong, but i would pass the future to the |
Oh, that's what you meant. You can use You could also use You can also use However, wasm-bindgen currently uses Futures 0.1, so you'll have to wait for wasm-bindgen to upgrade to 0.3 so you can use |
(Or you could write your own system for aborting Futures, which is what I did for |
I think @Pauan seems to have answered this question here (thanks!), so I'm gonna close. If there's more though please let me know and I'll reopen! |
Hi,
i want to use streams for event handling.
Stream
by adding additional adapters for all kinds of functionality (eg debounce as shown on the ReactiveX website)The text was updated successfully, but these errors were encountered: