comlink 0.0.1

Ergonomic Comlink RPC for Rust and WebAssembly
Documentation
use comlink::futures::StreamExt;
use wasm_bindgen_test::*;

#[comlink::service]
pub trait Counter {
    fn count_to(&self, last: u32) -> impl Stream<Item = u32>;
}

#[derive(Clone)]
struct CounterServiceImpl;

impl Counter for CounterServiceImpl {
    fn count_to(&self, last: u32) -> impl comlink::futures::Stream<Item = u32> {
        comlink::futures::stream::iter(1..=last)
    }
}

#[wasm_bindgen_test]
async fn streaming() {
    console_error_panic_hook::set_once();
    let channel = web_sys::MessageChannel::new().unwrap();
    CounterService::expose(CounterServiceImpl, &channel.port1());
    let client = CounterClient::wrap(&channel.port2());
    /* the stream has no terminator, so the take bounds it */
    assert_eq!(client.count_to(3).take(3).collect::<Vec<_>>().await, vec![1, 2, 3]);
}