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());
assert_eq!(client.count_to(3).take(3).collect::<Vec<_>>().await, vec![1, 2, 3]);
}