mirror of
https://github.com/adoyle0/leptos-use.git
synced 2025-01-23 00:59:22 -05:00
use_webtransport compiles
This commit is contained in:
parent
720af1423e
commit
5ac69356a4
3 changed files with 42 additions and 13 deletions
|
@ -4,7 +4,7 @@ version = "0.1.0"
|
|||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
leptos = { version = "0.4", features = ["nightly", "csr"] }
|
||||
leptos = { version = "0.5.0-beta2", features = ["nightly", "csr"] }
|
||||
console_error_panic_hook = "0.1"
|
||||
console_log = "1"
|
||||
log = "0.4"
|
||||
|
|
|
@ -4,10 +4,22 @@ use leptos_use::use_webtransport;
|
|||
|
||||
#[component]
|
||||
fn Demo() -> impl IntoView {
|
||||
let transport = use_webtransport("https://echo.webtransport.day");
|
||||
|
||||
use_webtransport();
|
||||
let (text, set_text) = create_signal("".to_string());
|
||||
|
||||
view! { }
|
||||
let on_send = {
|
||||
let transport = transport.clone();
|
||||
|
||||
move |e| {
|
||||
transport.send_datagrams(text().as_bytes());
|
||||
}
|
||||
};
|
||||
|
||||
view! {
|
||||
<textarea on:change=move |e| set_text(event_target_value(&e))/>
|
||||
<button on:click=on_send>"Send"</button>
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
|
|
|
@ -149,6 +149,20 @@ pub fn use_webtransport_with_options(
|
|||
}
|
||||
};
|
||||
|
||||
let on_closed = {
|
||||
let reconnect = Rc::clone(&reconnect);
|
||||
let unmounted = Rc::clone(&unmounted);
|
||||
|
||||
move || {
|
||||
if unmounted.get() {
|
||||
return;
|
||||
}
|
||||
|
||||
// TODO
|
||||
// reconnect();
|
||||
}
|
||||
};
|
||||
|
||||
let close = {
|
||||
let transport = Rc::clone(&transport);
|
||||
let reconnect_count = Rc::clone(&reconnect_count);
|
||||
|
@ -206,10 +220,14 @@ pub fn use_webtransport_with_options(
|
|||
|
||||
// TODO : reliable streams
|
||||
|
||||
on_cleanup(move || {
|
||||
unmounted.set(true);
|
||||
close();
|
||||
});
|
||||
{
|
||||
let unmounted = Rc::clone(&unmounted);
|
||||
|
||||
on_cleanup(move || {
|
||||
unmounted.set(true);
|
||||
close();
|
||||
});
|
||||
}
|
||||
|
||||
UseWebTransportReturn {
|
||||
transport,
|
||||
|
@ -263,19 +281,17 @@ fn lazy_initialize_reader(
|
|||
if !initialized.get() {
|
||||
initialized.set(true);
|
||||
|
||||
listen_to_stream(
|
||||
get_readable_stream(),
|
||||
move || initialized.set(false),
|
||||
on_value,
|
||||
);
|
||||
listen_to_stream(get_readable_stream(), on_value, move || {
|
||||
initialized.set(false)
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn listen_to_stream(
|
||||
readable_stream: web_sys::ReadableStream,
|
||||
on_done: impl Fn() + 'static,
|
||||
on_value: impl Fn(JsValue) + 'static,
|
||||
on_done: impl Fn() + 'static,
|
||||
) {
|
||||
let mut reader_options = web_sys::ReadableStreamGetReaderOptions::new();
|
||||
reader_options.mode(web_sys::ReadableStreamReaderMode::Byob);
|
||||
|
@ -478,6 +494,7 @@ impl CloseableStream for BidirStream {
|
|||
}
|
||||
|
||||
/// Return type of [`use_webtransport`].
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct UseWebTransportReturn {
|
||||
transport: Rc<RefCell<Option<web_sys::WebTransport>>>,
|
||||
datagrams_writer: Rc<RefCell<Option<web_sys::WritableStreamDefaultWriter>>>,
|
||||
|
|
Loading…
Add table
Reference in a new issue