-
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
Stream from AsyncIterator in wasm-bindgen-futures #2399
Comments
This would be awesome! Would it be possible to have the opposite too i.e. subscribe to a Rust I know already that you can call pre-existing global/static JS functions from Rust via an use std::collections::HashMap;
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
#[derive(Default)]
pub struct Subscribable<'a> { // compiler error that lifetimes aren't available
xs: Vec<u32>,
subscribers: HashMap<u32, &'a js_sys::Function>,
next_id: u32,
}
#[wasm_bindgen]
impl Subscribable {
pub fn new() -> Self {
Self {
xs: vec![1, 2, 3],
subscribers: HashMap::new(),
next_id: 0,
}
}
pub fn each(&self, callback: &js_sys::Function) {
let this = JsValue::null();
for &x in &self.xs {
let x = JsValue::from(x);
callback.call1(&this, &x);
}
}
// subscriber can't survive past the end of this method
pub fn subscribe(&mut self, subscriber: &js_sys::Function) -> u32 {
let id = self.next_id;
self.subscribers.insert(id, subscriber);
self.next_id += 1;
id
}
pub fn unsubscribe(&self, subscription: u32) {
self.subscribers.remove(&subscription);
}
} Any pointers would be hugely appreciated! |
@mattgibb Hi! Did you ever figure out how to do this? I'm at the same point now, having streams in Rust that I want to react to in JS, without a separate global listener function import. Either with callbacks like your code above, or by converting to an AsyncIterator. |
+1 |
AsyncIterator
in JS is the native equivalent of Promise to represent multiple future values instead of one just as in Rust we haveFuture
andStream
(soon in the standard library). Instead of creating a separate utility crate that converts async iterators to Rust streams I propose having that in function(e.g.iter_to_stream
) in thewasm-bindgen-futures
crate since the feature(can be behind a flag) is very "futures" related.The text was updated successfully, but these errors were encountered: